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);
//应用setScale(float sx,float sy)方法缩放图像
Matrix matrix = new Matrix();
matrix.setScale(2f, 2f);//以(0,0)点为轴心将图像在X轴和Y轴均缩放200%
canvas.drawBitmap(bitmap_rabbit, matrix, paint); // 绘制图像并应用matrix的变换
// 应用setScale(float sx, float sy, float px, float py) 方法缩放图像
Matrix m=new Matrix();
m.setScale(0.8f,0.8f,156,156); // 以(156,156)点为轴心将图像在X轴和Y轴均缩放80%
canvas.drawBitmap(bitmap_rabbit, m, paint); // 绘制图像并应用matrix的变换
canvas.drawBitmap(bitmap_rabbit, 0, 0, paint); // 绘制原图
super.onDraw(canvas);
}
}
}