图像处理详解之图像旋转

图像旋转的基本思想是通过Matrix类的setRotate方法设置旋转的角度,然后使用Bitmap.createBitmap方法创建一个已经旋转了的图像。除此之外,还可以使用Canvas.setMatrix方法设置,并直接使用drawBitmap绘制。

下面来实现一个旋转动画:



实现方法如下:

public class MainActivity extends Activity{
	
	public static int alpha=100;
	private View myView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

	private class MyView extends View{
		//十字扳手图像
		private Bitmap bitmap1;
		//小球图像
		private Bitmap bitmap2;
		//十字扳手当前角度
		private int digree1 = 0;
		//小球当前角度
		private int digree2 = 360;

		public MyView(Context context)
		{
			super(context);
			setBackgroundColor(color.white);
			InputStream is = getResources().openRawResource(R.drawable.cross);
			bitmap1 = BitmapFactory.decodeStream(is);
			is = getResources().openRawResource(R.drawable.ball);
			bitmap2 = BitmapFactory.decodeStream(is);
		}

		@Override
		protected void onDraw(Canvas canvas)
		{
			Matrix matrix = new Matrix();
			//讲旋转角度控制在0-360
			if (digree1 > 360)
				digree1 = 0;
			if(digree2 < 0)
				digree2 = 360;
			//设置旋转角度和旋转中心点
			matrix.setRotate(digree1++, 160, 240);						
			canvas.setMatrix(matrix);
			//绘制图像
			canvas.drawBitmap(bitmap1, 88, 169, null);
			//设置旋转角度和旋转中心点
			matrix.setRotate(digree2--,160 , 240);
			canvas.setMatrix(matrix);  
			//绘制图像
			canvas.drawBitmap(bitmap2, 35, 115, null);
			//在onDrow中调用invalidate方法,表示不断重绘,即实现动画效果
			invalidate();
		}
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值