Overlay Bitmap over another Android

雨燕双飞 提交于 2019-12-10 10:49:34

问题


hello guys i'm trying to get an image(Frame from resources) to overlay it over the original bitmap. so far i couldn't make my Bitmap goes into the frame as the frame always empty. the original bitmap is now showing inside the frame. here is my code that i'm using to accomplish this.

  Canvas canvas = new Canvas();
    Bitmap border = null;
    Bitmap scaledBorder = null;
    border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
    canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
    view.setImageBitmap(scaledBorder);

bmp as my original Bitmap from Gallery or Camera. i can't find away to put them together. only the frame will appear but not the bmp. thanks in advance.


回答1:


thanks man i figured it out by own. using this

void hm1(){
    Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    change = Bitmap.createScaledBitmap(change, width, height, false);
    Canvas canvas = new Canvas(change);
    Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
    canvas.drawBitmap(scaledBorder, 0, 0,null);
    //canvas.drawBitmap(k, 0, 0, null);
    view.setImageBitmap(change);
    }

by adding this method on any click button , menu etc you can draw two bitmaps over each other.
P.S : Bitmap change is another bitmap from the original one as i don't want the user to apply the Overlay on the original method but on the changed one. hope the answer helps someone. thanks




回答2:


Bottom line, first you need to add your original image to the canvas, then the border, then place the canvas on the view. Your best bet is to do this in an onDraw() method. Something like this should work:

@Override
void onDraw (Canvas canvas)
{
    canvas.drawBitmap(bmp,0,0,new Paint())
    Bitmap border = null;
    Bitmap scaledBorder = null;
    border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
    canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
}

Alternatively, you could call the draw() function from the view.

canvas.drawBitmap(bmp,0,0,new Paint())
Bitmap border = null;
Bitmap scaledBorder = null;
border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
int width = bmp.getWidth();
int height = bmp.getHeight();
scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
view.draw(canvas);


来源:https://stackoverflow.com/questions/14564100/overlay-bitmap-over-another-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!