【问题标题】:Convert drawable to bitmap and modify its color将drawable转换为位图并修改其颜色
【发布时间】:2015-09-22 07:33:32
【问题描述】:

因为我需要我的drawable 作为位图描述符,所以我将我的资源转换为Drawable,然后转换为Bitmap

但我想改变它的颜色,所以我使用了以下代码:

private static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

    int iColor = Color.parseColor("#006f00");

    drawable.setColorFilter(iColor, PorterDuff.Mode.SRC_ATOP);

    drawable.draw(canvas);
    return bitmap;
}

但是drawable的颜色仍然是黑色。 你能告诉我为什么以及如何让它工作吗?

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    这会将 BitmapDrawable 转换为 Bitmap。

    Drawable d = ImagesArrayList.get(0);  
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    

    要更改位图的颜色,请参见下面的链接

    How to change Bitmap image color in android?

    【讨论】:

    • BitmapDrawable 中的类转换异常。
    • @Ramani :请提供更多详细信息。你试过什么?
    • 我将使用 FontIconDrawable.inflate(context, R.xml.ic_red_marker) 并创建可绘制但当我运行应用程序时它会在 BitmapDrawable 行中崩溃说 classcastexception
    • 该代码仅在 d 已经是 BitmapDrawable 时才有效,在这种情况下,将其作为位图检索很简单......在所有其他情况下都会因 ClassCastException 而崩溃。
    • 你知道我的代码吗?如何获取位图。实际上我必须设置地图标记位图或资源等。我想在标记中设置 thi"FontIconDrawable.inflate(context, R.xml.ic_red_marker)"
    【解决方案2】:

    试试这个,

    将资源转换为可绘制对象。

    Drawable myIcon = getResources().getDrawable( R.drawable.icon );
    

    将drawable转换为位图。

    Bitmap myLogo = ((BitmapDrawable) myIcon).getBitmap();
    

    改变位图的颜色。

    Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
        float[] colorTransform = {
                0, 1f, 0, 0, 0, 
                0, 0, 0f, 0, 0,
                0, 0, 0, 0f, 0, 
                0, 0, 0, 1f, 0};
    
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); //Remove Colour 
        colorMatrix.set(colorTransform); //Apply the Red
    
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);   
    
        Display display = getWindowManager().getDefaultDisplay(); 
    
        Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));            
    
        image.setImageBitmap(resultBitmap);
    
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawBitmap(resultBitmap, 0, 0, paint);
    

    很高兴为您提供帮助。

    【讨论】:

      【解决方案3】:
      Paint p = new Paint();
      ColorFilter filter = new LightingColorFilter(Color.RED, 0);
      p.setColorFilter(filter);
      canvas.drawBitmap(bitmap, 0, 0, p);
      

      【讨论】:

        【解决方案4】:

        要将drawable转换为位图,请使用 -

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                               R.drawable.icon_resource);
        

        你可以通过这个来改变位图像素的颜色-

        int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
        myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
        for (int i=0; i<myBitmap.getWidth()*5; i++)
            pixels[i] = Color.BLUE;
        myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
        

        【讨论】:

          【解决方案5】:

          我认为问题可能出在PorterDuff.Mode.SRC_ATOP,您用于应用过滤器的方法。我不知道具体的 SRC_ATOP,但这个 link 解释了所有这些。

          我不知道你的图片是如何组成的,但我会使用DST_ATOP 而不是SRC_ATOP

          编辑:我假设在您正在分析的情况下此代码不会返回,因为如果它返回过滤器显然永远不会到达。

          if (drawable instanceof BitmapDrawable) 
              BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
              if(bitmapDrawable.getBitmap() != null) {
                  return bitmapDrawable.getBitmap();
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-27
            • 2016-01-22
            • 2019-12-17
            • 2011-03-03
            • 1970-01-01
            • 2011-08-23
            相关资源
            最近更新 更多