Gustafah88
Gustafah88

Reputation: 53

Get overlayed bitmap from Custom ImageView

I've been testing on an idea and I've stumbled upon a problem. I made a custom ImageView. I called it LayeredView, and the idea is that I can feed some layers of bitmap to it and it adds these layers on top of the src drawable, in order to make frames, sticks and stuff like that. I managed to make the images to overlay and show properly, but now, when i try to get the resulted bitmap, it gets out of scale. On my test I tried to get the Drawing Cache and place it on another ImageView. The result is in these links (for the lack of reputation)

LayeredView successfully shown: https://www.dropbox.com/s/34dcktew05yc1hg/Screenshot_2015-06-17-00-30-34.png?dl=0 Result with layers misplaced https://www.dropbox.com/s/pveeotksxss2oos/Screenshot_2015-06-17-00-30-53.png?dl=0 My code is like:

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;
        }

    }

}

And on a Button 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));
    }

Thanks in advance for any clues.

Upvotes: 0

Views: 192

Answers (0)

Related Questions