实现应用Matrix旋转图像

来源:互联网 发布:阿里云ssh登录 编辑:程序博客网 时间:2024/06/07 16:16

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);}@Overrideprotected 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