我一直在测试一个想法,但偶然发现了一个问题。我做了一个自定义的 ImageView。我称它为 LayeredView,我的想法是我可以向它提供一些位图层,并将这些层添加到 src 可绘制对象的顶部,以制作框架、棒和类似的东西。我设法使图像叠加并正确显示,但是现在,当我尝试获取结果位图时,它超出了比例。在我的测试中,我试图获取绘图缓存并将其放在另一个 ImageView 上。结果在这些链接中(由于缺乏声誉)
LayeredView 成功显示: https ://www.dropbox.com/s/34dcktew05yc1hg/Screenshot_2015-06-17-00-30-34.png?dl=0 结果层错位 https://www.dropbox.com/s /pveeotksxss2oos/Screenshot_2015-06-17-00-30-53.png?dl=0 我的代码是这样的:
public class LayeredView extends ImageView
{
private List<Layer> layers;
public LayeredView(Context context){
this(context, null);
}
public LayeredView(Context context, AttributeSet attrs){
super(context, attrs);
layers = new ArrayList<Layer>();
addBitmap(((BitmapDrawable) context.getResources().getDrawable(R.drawable.frame1)).getBitmap());
}
public void addBitmap(Bitmap bitmap){
layers.add(new Layer(bitmap, true));
invalidate();
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO: Implement this method
super.onDraw(canvas);
Rect rect = new Rect(0, 0, getWidth(), getHeight());
// using get(0) to test
canvas.drawBitmap(layers.get(0).bitmap, rect, rect, null);
}
private class Layer{
public Bitmap bitmap;
public boolean show;
public Layer(Bitmap b, boolean s){
this.bitmap = b;
this.show = s;
}
}
}
并在按钮 OnClick
public void copy(View v){
((LayeredView)findViewById(R.id.lv)).buildDrawingCache();
((ImageView)findViewById(R.id.iv)).setImageBitmap(((LayeredView)findViewById(R.id.lv)).getDrawingCache(true));
}
提前感谢任何线索。