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();//定义一个画笔
paint.setAntiAlias(true);//使用抗锯齿功能
Bitmap bitmap_bg = BitmapFactory.decodeResource(MainActivity.this.getResources(),
R.drawable.background);
canvas.drawBitmap(bitmap_bg, 0, 0, paint);//绘制背景
Bitmap bitmap_rabbit = BitmapFactory.decodeResource(MainActivity.this.getResources(),
R.drawable.rabbit);
canvas.drawBitmap(bitmap_rabbit, 0, 0, paint);//绘制原图
Matrix matrix = new Matrix();//创建一个Matrix的对象
matrix.setRotate(30);//将matrix旋转30°
matrix.postTranslate(100, 50);//将matrix平移到(100,50)的位置
canvas.drawBitmap(bitmap_rabbit, matrix, paint);//绘制图像并应用matrix的变换
super.onDraw(canvas);
}
}
}