实现应用Matrix旋转图像

1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
</FrameLayout>

2、MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout ll = (FrameLayout)findViewById(R.id.frameLayout1);
        ll.addView(new MyView(this));
    }

    public class MyView extends View{

		public MyView(Context context) {
			super(context);
		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();//定义一个画笔
			Bitmap bitmap_gb = BitmapFactory.decodeResource(MainActivity.this.getResources(), 
					R.drawable.background);
			canvas.drawBitmap(bitmap_gb, 0, 0,paint);//绘制背景图像
			Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),
					R.drawable.rabbit);
			canvas.drawBitmap(bitmap_rabbit, 0, 0, paint);//绘制原图
			//应用setRotate(float degrees)方法旋转图像
			Matrix matrix = new Matrix();
			matrix.setRotate(30);//以(0,0)点为轴心旋转30°
			canvas.drawBitmap(bitmap_rabbit, matrix, paint);//绘制图像并应用matrix的变换
			//应用setRotate(float degrees,float px,float py)方法旋转图像
			Matrix m = new Matrix();
			m.setRotate(90, 87, 87);//以(87,87)点为轴心旋转90°
			canvas.drawBitmap(bitmap_rabbit, m, paint);//绘制图像并应用matrix的变换
		}
    	
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值