有兩種做法:
1.以Canvas畫在指定位址與縮放,再將Canvas所畫的Bitmap拿來用
2.以Layout檔Inflate,再將View轉為Bitmap拿來用
結論:
第二種方法較快且容易擴充與調整
方法一:
Bitmap org = BitmapFactory.decodeResource(getResources(), R.drawable.empty_pinpoint);
Bitmap pinPoint = Bitmap.createScaledBitmap(org, dp2px(52), dp2px(68), true);
Canvas pinPointCanvas = new Canvas(pinPoint);
Bitmap head = BitmapFactory.decodeResource(getResources(), R.drawable.test_head);
pinPointCanvas.drawBitmap(head, null, new Rect(dp2px(6), dp2px(3), dp2px(46), dp2px(43)), null);
ImageView view = (ImageView) findViewById(R.id.imgResult);
view.setImageBitmap(pinPoint);
方法二:
//1. Make a bitmap to draw to.
Bitmap bmp = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//2. We need a Canvas to handle drawing to the bitmap. Bitmap has no draw methods of its own.
Canvas canvas = new Canvas(bmp);
//3. Get a reference to the LayoutInflater.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//4. Inflate the layout we want to draw.
View layout = inflater.inflate(R.layout.layout_to_draw, null);
//5. Call setDrawingCacheEnabled to true, since the default is false.
layout.setDrawingCacheEnabled(true);
//6. Tell the layout how big it should be. In this case we are using the full size of the canvas.
layout.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
//7. Now we apply the measures so that it embiggens to the correct size before drawing.
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
//8. Finally, we draw the bitmap. Mission accomplished. But of course we still can't see the bitmap. So...
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, null);
//9. Get reference to an ImageView that you want to hold your bitmap.
ImageView view = (ImageView) findViewById(R.id.image_to_show);
//10. Set the bitmap as the ImageView's Image Bitmap.
view.setImageBitmap(bmp);
dp<=>px轉換
private int dp2px(int dp) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
return (int)((dp * displayMetrics.density) + 0.5);
}
1.以Canvas畫在指定位址與縮放,再將Canvas所畫的Bitmap拿來用
2.以Layout檔Inflate,再將View轉為Bitmap拿來用
結論:
第二種方法較快且容易擴充與調整
方法一:
Bitmap org = BitmapFactory.decodeResource(getResources(), R.drawable.empty_pinpoint);
Bitmap pinPoint = Bitmap.createScaledBitmap(org, dp2px(52), dp2px(68), true);
Canvas pinPointCanvas = new Canvas(pinPoint);
Bitmap head = BitmapFactory.decodeResource(getResources(), R.drawable.test_head);
pinPointCanvas.drawBitmap(head, null, new Rect(dp2px(6), dp2px(3), dp2px(46), dp2px(43)), null);
ImageView view = (ImageView) findViewById(R.id.imgResult);
view.setImageBitmap(pinPoint);
方法二:
//1. Make a bitmap to draw to.
Bitmap bmp = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//2. We need a Canvas to handle drawing to the bitmap. Bitmap has no draw methods of its own.
Canvas canvas = new Canvas(bmp);
//3. Get a reference to the LayoutInflater.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//4. Inflate the layout we want to draw.
View layout = inflater.inflate(R.layout.layout_to_draw, null);
//5. Call setDrawingCacheEnabled to true, since the default is false.
layout.setDrawingCacheEnabled(true);
//6. Tell the layout how big it should be. In this case we are using the full size of the canvas.
layout.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
//7. Now we apply the measures so that it embiggens to the correct size before drawing.
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
//8. Finally, we draw the bitmap. Mission accomplished. But of course we still can't see the bitmap. So...
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, null);
//9. Get reference to an ImageView that you want to hold your bitmap.
ImageView view = (ImageView) findViewById(R.id.image_to_show);
//10. Set the bitmap as the ImageView's Image Bitmap.
view.setImageBitmap(bmp);
dp<=>px轉換
private int dp2px(int dp) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
return (int)((dp * displayMetrics.density) + 0.5);
}
留言
張貼留言