论坛首页 移动开发技术论坛

利用Camera实现图片叠加效果

浏览 6736 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-28   最后修改:2012-02-28
先看看效果:


主要是利用了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);
	 }

  • 大小: 74.8 KB
  • 大小: 53.3 KB
   发表时间:2012-02-29  
感谢楼主的共享精神啊!!!  我昨天开始研究,今天就看到有代码了 ,谢谢~
0 请登录后投票
   发表时间:2012-02-29  
17548030 写道
感谢楼主的共享精神啊!!!  我昨天开始研究,今天就看到有代码了 ,谢谢~

嘿嘿,不用客气,你有好效果也可以分享给大家!
0 请登录后投票
   发表时间:2012-03-01  
楼主,灰常感谢!
0 请登录后投票
   发表时间:2012-03-02  
不错....感谢lz的无私
0 请登录后投票
   发表时间:2012-03-02  
+1024
0 请登录后投票
   发表时间:2012-03-02  
楼主,麻烦共享下工程撒,好人做到底
0 请登录后投票
   发表时间:2012-03-05  
效果看起来不错哈,感谢分享。
0 请登录后投票
   发表时间:2012-03-05  
2006her 写道
效果看起来不错哈,感谢分享。

这个效果什么时候会用到?
0 请登录后投票
   发表时间:2012-03-05  
helloandroid 写道
先看看效果:


主要是利用了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);
	 }


这个效果什么时候会用到呢?解释一下。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics