【问题标题】:What is the correct way to horizontally add some spaced image into a Canvas?将一些间隔图像水平添加到画布中的正确方法是什么?
【发布时间】:2016-07-02 16:36:09
【问题描述】:

我是 Android 开发的新手,我有以下疑问。

我必须在一个 Canvas 对象中绘制一个并排的图像。

举个例子:我有这个图标(它非常大,我必须调整它的大小):

所以我必须将这些图标中的 3 个并排放置(在图像和下一个图像之间添加一些空白)。

所以我做了这样的事情:

// Load the 2 images for the creation of the "difficulty graphic":
Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);

// Where the previus image will be drawn:
Canvas canvas = new Canvas();

所以我认为我可以将上一张图片添加到 Canvas 中,如下所示:

canvas.drawBitmap(smallImage, 0f, 0f, null); 

我认为第一个 0f 值代表插入图像之前的水平空间(偏移量),如果我做错了断言,请纠正我。

那么,我怎样才能将其中的 3 张图像彼此相邻添加,在图像和下一张图像之间留下一些空白?

【问题讨论】:

    标签: java android android-canvas android-image android-bitmap


    【解决方案1】:

    这样的事情应该可以工作:

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    
    int space = 10; // the space between images
    
    for(int i = 0; i < 3; i++) {
        canvas.drawBitmap(smallImage, i * (smallImage.getWidth() + space), 0, null);
    }
    
    // do whatever you want with output
    

    【讨论】:

      【解决方案2】:

      这应该可以完成工作,例如对于两个具有水平空间的图像

      public static mergeImages(Bitmap firstImage, Bitmap secondImage)
      {
       Bitmap cs;
       int width, height;
       float space = 60f; // space between image horizontal
      
       if(firstImage.getWidth() > secondImage.getWidth()) {
       width = firstImage.getWidth() + secondImage.getWidth();
       } else {
       width = s.getWidth() + secondImage.getWidth();
       }
       height = firstImage.getHeight();
      
       cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      
       Canvas comboImage = new Canvas(cs);
       comboImage.drawBitmap(firstImage, 0f, 0f, null);
       comboImage.drawBitmap(secondImage, firstImage.getWidth()+space, 0f, null);
      
       return cs; // result image
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-16
        • 2014-03-25
        • 2010-11-07
        • 1970-01-01
        • 1970-01-01
        • 2021-08-13
        • 2021-03-23
        • 2016-11-09
        相关资源
        最近更新 更多