我希望将Bitmap
重叠到另一个Bitmap
上。你可以说,我想在另一个Bitmap
上绘制bitmap
。
假设我有一个Bitmap
:
我想在这个位图“保持冷静并检查你的工作”中绘制另一个bitmap
!这将是位图1
这将是位图2
我想将位图2 叠加到位图1 上,说位图2 应该高于位图1 。< / p>
位图2 应该获取位图1 的宽度和高度以覆盖所有内容。
位图1 将具有任何宽度和高度,但是,我希望位图2 应该获得位图1的宽度和高度无论如何。
我的代码问题是庄稼 位图1 ,然后将位图2 覆盖到它上面。我的意思是代码获得 Bitmap 2 宽度和高度!
public Bitmap overlay123(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1new, left ,0 , null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
以上代码的问题在于裁剪或调整位图1 的宽度和高度!
private Bitmap overlayer(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
代码问题是它总是在左上角显示位图2 ,尺寸较小!
我只需要知道如何将位图2 放在位图1 上,使其宽度和高度与位图1 相同。< / p>
我是Bitmap代码的新手,所以我不知道它
先谢谢!
答案 0 :(得分:2)
MainActivity Output.
string str = @"a\b";
var result = str.Split('\\'); // work (case 1)
//var result = str.Split(new char['\\']); not work as what I want and will print a\b (case 2)
foreach (var r in result)
Console.WriteLine(r);
<强> activity_main.xml中强>
public class MainActivity extends Activity {
private ImageView Img;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Img = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.top);
Bitmap bmt = BitmapFactory.decodeResource(getResources(), R.drawable.back);
Img.setImageBitmap(overlayer(bm, bmt));
}
});
}
private Bitmap overlayer(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
}
<强>输出:强>
答案 1 :(得分:0)
首先,我建议您使用 Try2 作为进一步工作的基础。 但我会略微修改代码:
private Bitmap overlayer(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getWidth(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, null, new Rect(0,0,bmp1.getWidth(),bmp1.getHeight()), new Paint());
canvas.drawBitmap(bmp2, null, new Rect(0,0,bmp1.getWidth(),bmp1.getHeight()), new Paint());
return bmOverlay;
}
我使用了android developer reference中提到的构造函数来绘制位图。您定义了一个新的Recatangle,该服务器作为用于缩放/绘制位图到画布的形状。在您的情况下,我定义的矩形与您的bitmap1大小相同。如果将此重新控制框交给绘制画布的方法,画布会将位图缩放到该框大小并在画布上绘制。