如何在Android的画布添加3张图片 [英] How to add 3 images in a canvas in android

查看:81
本文介绍了如何在Android的画布添加3张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想等以后添加一个在画布上3张图片。 这是我的code: -

I have 3 images that I want to add one after other on a canvas. This is my code:-

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageButton im1 = (ImageButton)findViewById(R.id.btnPN); 
    im1.setBackgroundDrawable(getImage());       
}

public BitmapDrawable getImage()
{

    Drawable image1 = getResources().getDrawable(R.drawable.imagename);
    Drawable image2 = getResources().getDrawable(R.drawable.imagename);
    Drawable image3 = getResources().getDrawable(R.drawable.imagename);

    Bitmap bitmap = Bitmap.createBitmap(image1.getIntrinsicWidth()            
          +image2.getIntrinsicWidth()+image3.getIntrinsicWidth(),
          image1.getIntrinsicHeight(),Bitmap.Config.ALPHA_8);

    Canvas canvas = new Canvas(bitmap);

    image1.setBounds(0, 0, image1.getIntrinsicWidth(), image1.getIntrinsicHeight());
    image1.draw(canvas);

    image2.setBounds(image1.getIntrinsicWidth(), 0, image2.getIntrinsicWidth(),
            image2.getIntrinsicHeight());
    image2.draw(canvas);

    image3.setBounds(image1.getIntrinsicWidth()+image2.getIntrinsicWidth(),
                      0, image3.getIntrinsicWidth(),
                      image3.getIntrinsicHeight());
    image3.draw(canvas);


    BitmapDrawable bu = new BitmapDrawable(bitmap);
    return bu;    

}

而这是行不通的。

有人能告诉我什么,我做错了。

Can someone please tell me what I am doing wrong here.

谢谢, 法哈

推荐答案

我必须要解决类似的东西不是很久以前,你就几乎没有用您的解决方案。但是,你应该使用矩形对象,以抵消在那里你画出每一次你的位图。假设你已经复制所有的图像转换为位图图像[]数组,并象上面那样您已经创建位图和画布,使用以下命令:

I had to solve something similar not too long ago, and you're nearly there with your solution. However, you should be using Rect objects to offset where you draw your bitmap each time. Assuming you've copied all your images into an array of Bitmaps images[], and you've created your bitmap and canvas as you did above, use the following:

Rect srcRect;
Rect dstRect;

for (int i = 0; i < images.length; i++){
    srcRect = new Rect(0, 0, images[i].getWidth(), images[i].getHeight());
    dstRect = new Rect(srcRect);
    if (i != 0){
        dstRect.offset(images[i-1].getWidht(), 0)
    }
    canvas.drawBitmap(images[i], srcRect, dstRect, null);
}

这将它们复制到所有一行。这不是太难使用两个for循环来适应这4图像复制成方形,或类似的东西。

This will copy them all into one line. It's not too difficult to adapt this to copy 4 images into a square, or something similar, using two for loops.

这篇关于如何在Android的画布添加3张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆