简体   繁体   中英

Android draw the bitmap in specified X,Y positions in the canvas

I have two images , first image is having the transparent area in that.the second image i have to fit the transparent area of the first image . for that i have the first image height, width , x, y values.i am combining those two images by drawing the bitmap on canvas like below.

Bitmap bm = Bitmap.createBitmap(overLay.getWidth(),
            overLay.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas combineImg = new Canvas(bm);

    combineImg.drawBitmap(overLay, 0, 0, null);
    // combineImg.drawbitma
    combineImg.drawBitmap(mask, 61, 111, null);
    ImageView img = (ImageView) findViewById(R.id.image);
    img.setImageBitmap(bm);

but it is not fitting at correct position . any one have the idea please explain here. the out put i am getting as below. 在此处输入图片说明

You are getting the xy position from the top edge...
And when you merge them, there's the error...
get the bitmap xy inside image view, subtract the error and then give those xy positions to merge the bitmaps.

    int ih = backgroundIv.getHeight();//height of imageView
    int iw = backgroundIv.getWidth();//width of imageView
    int iH = backgroundIv.getDrawable().getIntrinsicHeight();//original height of underlying image
    int iW = backgroundIv.getDrawable().getIntrinsicWidth();//original width of underlying image


    if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
    else ih= iH*iw/iW;//rescaled height of image within ImageView

    iv_OverlayImage.requestLayout();
    iv_OverlayImage.getLayoutParams().height = ih;
    iv_OverlayImage.getLayoutParams().width = iw;

    pixelsToRight = (backgroundIv.getWidth() - iw)/2;

I hope this helps... Cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM