我有个问题.我需要合并两个不同大小的图片(drawables).这个想法是为某人(动态加载)提供100x100px的图片并且具有更大的透明背景(例如100x120).在最后20个像素中,我有一个箭头,应该指向一个人在地图上的位置.然后我想我可以这样做:
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.background_img);
layers[1] = res.getDrawable(R.drawable.icon);
LayerDrawable layerDrawable = new LayerDrawable(layers);
Run Code Online (Sandbox Code Playgroud)
但这只是将一个图像叠加到另一个图像上而忽略了它们的边界.
感谢Vaidas
- 更新:最后解决了问题.奇迹般有效 :)
private Drawable createPersonDrawable(Bitmap personImage)
{
Bitmap resultingBitmap = Bitmap.createBitmap(drawableWidth,
drawableHeight, Bitmap.Config.ARGB_8888);
Canvas comboCanvas = new Canvas(resultingBitmap);
comboCanvas.drawBitmap(personImage, 0, 0, null);
// Get the bottom part of the image from resources
Bitmap bottomPart = BitmapFactory.decodeResource(getResources(),
R.drawable.person_map_icon_bottom);
comboCanvas.drawBitmap(bottomPart, 0, drawablePersonImageHeight, null);
comboCanvas.save();
return new BitmapDrawable(resultingBitmap);
}
Run Code Online (Sandbox Code Playgroud)
我在这里找到了描述:http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas