canvas.drawBitmap坏位置和大小(没有正确放置图像)

时间:2014-11-29 12:28:57

标签: java c# android bitmap android-canvas

大家好,事先谢谢,我希望写得好,因为我正在做一个简单的应用程序,应该只在一个中联合两个以上的位图,问题是在哪个位置位图和大小Side错误,而且事实不会发现给我的逻辑背面很好,事实上它已经是c#中的Tengo Que代码而PASE java显然是不同的罪,但原则相同。

我想知道你是否有办法在代码中说出这些图像的位置和大小,

抱歉我的英文不好

CODIGO JAVA

绘制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);
在Java中

见java picture http://i58.tinypic.com/1zywm5u.jpg

C#代码

忽略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;

请参阅c#picture http://i61.tinypic.com/s61wlh.jpg

1 个答案:

答案 0 :(得分:2)

我找到了解决方案 使用矩阵设置位置和比例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方法

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;

}

查看图片

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