canvas.drawBitmap bad Location and size (not placing image correctly)

て烟熏妆下的殇ゞ 提交于 2019-12-12 05:48:11

问题


Hi everyone and beforehand thanks, I hope are well write because I am doing a simple application that should unite more than two bitmaps in only one, the problem is in which position bitmaps and size Side wrong, and the truth will not find the back for logic given to me that 's fine, in fact it is a Tengo Que code already in c # and PASE java obviously is different sin but have the same principle .

I wonder if you have the way to make the position and size of these images out as this saying in the code,

Sorry for my bad English

CODIGO JAVA

Paint mPaint;

    Bitmap image1=BitmapUtils.decodeBase64(Lie.GeFondo().GetImagen());
    Bitmap image2=BitmapUtils.decodeBase64(Utilidades.getImagenTomadabase64());
    Bitmap image3=BitmapUtils.decodeBase64(Lie.GetBanner().GetImagen());
    Bitmap result = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Rect srcRect = new Rect(0, 0, image1.getWidth(), image1.getHeight());
    Rect dstRect = new Rect(srcRect);
    Rect srcRect1 = new Rect( Foto.GetPosicionDeItems().Getx(),Foto.GetPosicionDeItems().Gety(),Foto.GetTamano().GetWidth(), Foto.GetTamano().GeHeight());
    Rect srcRect3 = new Rect( Lie.GetBanner().GetPosicionDeItems().Getx(), Lie.GetBanner().GetPosicionDeItems().Gety() ,Lie.GetBanner().GetTamano().GetWidth(), Lie.GetBanner().GetTamano().GeHeight());


    Rect srcRect2 = new Rect(0,0,image2.getWidth(), image2.getHeight());
    Rect srcRect4 = new Rect(0,0,image3.getWidth(), image3.getHeight());

    dstRect.offset(0, 0);
    canvas.drawBitmap(image1, srcRect, dstRect, null);
    dstRect.offset(image1.getWidth(), 0);
    srcRect1.offset(0, 0);
    canvas.drawBitmap(image2,srcRect2 ,srcRect1 , null);

    srcRect1.offset(image2.getWidth(), 0);
    srcRect3.offset(0, 0);
    canvas.drawBitmap(image3,srcRect4 ,srcRect3 , null);
    srcRect3.offset(image3.getWidth(), 0);
    myImage = (ImageView) findViewById(R.id.imageView);
    myImage.setImageBitmap(result);

in Java see java picture http://i58.tinypic.com/1zywm5u.jpg

C# Code

Ignore the foreach.

System.Drawing.Bitmap Bac = (System.Drawing.Bitmap)Lienzo.Fondo.Imagen; System.Drawing.Graphics r = System.Drawing.Graphics.FromImage(Bac);

        if (Lienzo.Fotos != null)
        {
            if (Lienzo.Fotos.Count > 0)
            {
                int i = 0;
                foreach (RADMLIB.Items item in Lienzo.Fotos)
                {

                    System.Drawing.Bitmap img = (System.Drawing.Bitmap)Lista[i];
                    r.DrawImage(img, item.PosicionDeItems.X, item.PosicionDeItems.Y, item.Tamano.Width, item.Tamano.Height);
                    i++;
                }
            }
        }
        if (Lienzo.Banner != null)
        {
            r.DrawImage((System.Drawing.Bitmap)Lienzo.Banner.Imagen, Lienzo.Banner.PosicionDeItems.X, Lienzo.Banner.PosicionDeItems.Y, Lienzo.Banner.Tamano.Width, Lienzo.Banner.Tamano.Height);
        }

        return Bac;

see c# picture http://i61.tinypic.com/s61wlh.jpg


回答1:


I found the solution using Matrix for set location and scale x,y

    Bitmap image1=BitmapUtils.decodeBase64(Lie.GeFondo().GetImagen());
    Bitmap image2=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Utilidades.getImagenTomadabase64()),Foto.GetTamano().GetWidth(),Foto.GetTamano().GeHeight());
    Bitmap image3=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Lie.GetBanner().GetImagen()),Lie.GetBanner().GetTamano().GetWidth(),Lie.GetBanner().GetTamano().GeHeight());
    Bitmap result = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);//Create the canvas to your image
    Rect srcRect = new Rect(0, 0, image1.getWidth(), image1.getHeight());
    Rect dstRect = new Rect(srcRect);
    Matrix matrix = new Matrix ();
    Matrix matrix2 = new Matrix ();
    matrix.postTranslate( Foto.GetPosicionDeItems().Getx(),Foto.GetPosicionDeItems().Gety());
    matrix2.postTranslate( Lie.GetBanner().GetPosicionDeItems().Getx(),Lie.GetBanner().GetPosicionDeItems().Gety());
    canvas.drawBitmap(image1, srcRect, dstRect, null);
    dstRect.offset(image1.getWidth(), 0);
    canvas.drawBitmap(image2,matrix , null);
    canvas.drawBitmap(image3,matrix2 , null);

getResizedBitmap Method

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;



    Matrix matrix = new Matrix();



    matrix.postScale(scaleWidth, scaleHeight);



    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

}

see the image

https://lh4.ggpht.com/LXW8kVc3U8qQUHnORI-3H4H-A2hjq92y_oEDsKIs-iBDkVBFTgjGP03xFReCeuyLlg=h900-rw



来源:https://stackoverflow.com/questions/27202233/canvas-drawbitmap-bad-location-and-size-not-placing-image-correctly

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