将 canvas 图像与另一图像 android 叠加
overlay canvas image with another image android
我在屏幕上显示了平面图的图像,我的问题是如何在上面叠加另一个图像。
查看我在另一个线程中询问如何 here
的图片
/**
* floor plan drawing.
*
* @param canvas the canvas on which the background will be drawn
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image= Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
如何在第一个图像上添加第二个图像?
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
Bitmap over = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);
image = Bitmap.createScaledBitmap(over, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
这是我的方法,将两个图像叠加到一个 ImageView 中:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
使用此方法:
public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {
int bitmap1Width = bitmapBackground.getWidth();
int bitmap1Height = bitmapBackground.getHeight();
int bitmap2Width = bitmapImage.getWidth();
int bitmap2Height = bitmapImage.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmapBackground.getConfig());
Canvas canvas = new Canvas(overlayBitmap);
canvas.drawBitmap(bitmapBackground, new Matrix(), null);
canvas.drawBitmap(bitmapImage, marginLeft, marginTop, null);
return overlayBitmap;
}
获取参考和位图以进行叠加!
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.androide);
Bitmap bmpImages = overlayBitmap(background, image);
imageView.setImageBitmap(bmpImages);
结果是:
我在屏幕上显示了平面图的图像,我的问题是如何在上面叠加另一个图像。
查看我在另一个线程中询问如何 here
的图片/**
* floor plan drawing.
*
* @param canvas the canvas on which the background will be drawn
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image= Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
如何在第一个图像上添加第二个图像?
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
Bitmap over = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);
image = Bitmap.createScaledBitmap(over, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
这是我的方法,将两个图像叠加到一个 ImageView 中:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
使用此方法:
public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {
int bitmap1Width = bitmapBackground.getWidth();
int bitmap1Height = bitmapBackground.getHeight();
int bitmap2Width = bitmapImage.getWidth();
int bitmap2Height = bitmapImage.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmapBackground.getConfig());
Canvas canvas = new Canvas(overlayBitmap);
canvas.drawBitmap(bitmapBackground, new Matrix(), null);
canvas.drawBitmap(bitmapImage, marginLeft, marginTop, null);
return overlayBitmap;
}
获取参考和位图以进行叠加!
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.androide);
Bitmap bmpImages = overlayBitmap(background, image);
imageView.setImageBitmap(bmpImages);
结果是: