Android画图之Bitmap(一)
要
public class MyView extends View { private Bitmap mBitmap; public MyView(Context context) { super(context); initialize(); } private void initialize() { Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap(); mBitmap = bmp; } @Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); //当然,如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。 canvas.drawBitmap(mBitmap, 0, 0, null); } } 结果:
原图:
虽然这仅仅只是第一步,但是很鼓舞人心呢,呵呵。
上面是直接将原图画在屏幕上,没有任何处理,因为图片比屏幕大,所以超出的部分看不到。我们试着将图片拉伸以填满整个屏幕。
public class MyView extends View { private Bitmap mBitmap; private Matrix mMatrix = new Matrix(); private static int mScreenWidth; private static int mScreenHeight; public MyView(Context context) { super(context); initialize(); } private void initialize() { DisplayMetrics dm = getResources().getDisplayMetrics(); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels; Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap(); mBitmap = Bitmap.createScaledBitmap(bmp, mScreenWidth, mScreenHeight, true); } @Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); //当然,如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。 canvas.drawBitmap(mBitmap, 0, 0, null); } }
显示结果:
下面一篇将介绍对Bitmap的一些操作。
Android画图之Bitmap(二)
mBitmap = Bitmap.createBitmap(bmp, 100, 100, 120, 120);
缩放
Bitmap mBitmap = Bitmap.createScaledBitmap(bmp, mScreenWidth, mScreenHeight, true); canvas.drawBitmap(mBitmap, null, new Rect(0, 0, 200, 200), null);
这
canvas.drawBitmap(mBitmap, new Rect(100, 100, 300, 300), new Rect(100, 100, 200, 200), null);