利用Camera实现图片叠加效果
程序员文章站
2022-05-20 20:48:57
...
先看看效果:
主要是利用了android.graphics.Camera类和android.graphics.Matrix还有处理View的onTouchEvent事件
主要是利用了android.graphics.Camera类和android.graphics.Matrix还有处理View的onTouchEvent事件
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); transformationImage(canvas,list.size()); }
private void transformationImage(Canvas canvas,int size){ Paint paint = new Paint(); //paint.setAlpha(230); paint.setAntiAlias(true); for(int i = 0 ;i<size;i++){ mCamera.save(); mCamera.translate((size-i)*10.0f, 0.0f,-60.0f*(size-i)); mCamera.rotateY(-30.0f); mCamera.getMatrix(matrix); matrix.preScale((float)(i+1)/(float)(size), (float)(i+1)/(float)(size)); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); canvas.drawBitmap(list.get((5-currentIndex+i)%5), matrix, paint); mCamera.restore(); } }
@Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); int dx; int dy; switch(event.getAction()) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: dx = x - mLastMotionX; dy = y - mLastMotionY; mLastMotionX = x; mLastMotionY = y; if(dx<0&&Math.abs(dx)>30){ currentIndex--; if(currentIndex<1){ currentIndex = 5; } }else if(dx>0&&Math.abs(dx)>30){ currentIndex++; if(currentIndex>5){ currentIndex = 1; } } break; case MotionEvent.ACTION_UP: break; } invalidate(); return true; } @Override public void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); }