【问题标题】:setColorFilter doesn't work on Android < 2.2setColorFilter 在 Android < 2.2 上不起作用
【发布时间】:2011-08-10 15:43:24
【问题描述】:

我对可绘制对象的 setColorFilter-Method 有问题。

它在 Android 2.2 上运行良好,但在低于该版本的版本上运行良好。

我的问题与此处描述的 Drawable.setColorFilter() not working on Android 2.1 类似,但这对我不起作用...

我使用的这段代码在 Android 2.2 上运行良好,但在低于该版本的任何版本上都没有。

ImageView imageView = (ImageView) findViewById( R.id.imageView1 );        
Bitmap immutableBitmap = BitmapFactory.decodeResource( getResources(), R.drawable.mybitmap );
Bitmap mutableBitmap = immutableBitmap.copy( Bitmap.Config.ARGB_8888, true );
immutableBitmap.recycle();
immutableBitmap = null;
Drawable d1 = new BitmapDrawable( mutableBitmap );
d1.setColorFilter( 0xff00ffff, PorterDuff.Mode.MULTIPLY );
imageView.setImageDrawable( d1 );

非常感谢任何让它工作的线索:)

【问题讨论】:

    标签: android bitmap drawable


    【解决方案1】:

    我不知道是否有其他解决方法,但我发现使用 imageView.setBackgroundDrawable() 而不是 imageView.setImageDrawable()

    【讨论】:

    • 太好了,这似乎有效。谢谢!你有什么解释为什么 setImageDrawable() 不起作用?
    • 我在将 2.1 src 与 2.2 进行比较时无法克隆,但我想它与 ImageView applyColor 方法或类似方法有关......因为 setBackgroundDrawable 是 View 的超类方法,并且是处理方式不同,这个错误可能不会影响它。
    • @Joe 是对的,ImageView.setImageDrawable() > updateDrawable() > applyColorMod()。 Android 2.1 中的最后一个将始终应用分配给 ImageView 的颜色过滤器(可能为 null),而在以后的版本中将仅在专门设置时应用它(新的 mColorMod 变量)。见grepcode.com/file/repository.grepcode.com/java/ext/…grepcode.com/file/repository.grepcode.com/java/ext/…
    【解决方案2】:

    根据@Joe 的 cmets 和我从上面对 Android 2.1 的发现,我认为更好的解决方法是将相同的 colorFilter 应用于 Drawable 和 ImageView(不幸的是 Drawable.getColorFilter() 仅从 API 21 开始可用):

    d1.setColorFilter         ( 0xff00ffff, PorterDuff.Mode.MULTIPLY );
    imageView.setColorFilter  ( 0xff00ffff, PorterDuff.Mode.MULTIPLY );
    imageView.setImageDrawable( d1 );
    

    ImageView.setBackgroundDrawable() 反对的一点是它不尊重 ScaleType。

    如果您已经将 ImageView 扩展用于其他目的,则可能更好的解决方案是在专门针对 Android 2.1 的 setImageDrawable() 中修复它,它会通过 mBitmapState.mPaint.getColorFilter() 的反射获取 colorFilter 并应用它到图像视图。

    或者使用下面的 ImageViewCompat 类 - 它需要 Apache Commons Lang,您可以从 http://search.maven.org 下载 JAR(和源代码),或者如果您使用 Maven 或 Gradle:org.apache.commons / commons-lang3。我发现最后一个最适合 Android 2.1 / Java 5 的版本是commons-lang3 v3.1

    ImageViewCompat.setImageDrawable(imageView, d1);

    package org.yourapp.widget;
    
    import org.apache.commons.lang3.reflect.FieldUtils;
    
    import android.graphics.Paint;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Build;
    import android.util.Log;
    import android.widget.ImageView;
    
    public final class ImageViewCompat {
        /**
         * Members
         */
        private static final String TAG = ImageViewCompat.class.getSimpleName();
    
        /**
         * ImageView.setImageDrawable() backward compatible version.
         * 
         * @param p_imageView
         * @param p_drawable
         */
        public static final void setImageDrawable(ImageView p_imageView, Drawable p_drawable) {
            /*
             * API 2.1 workaround - apply Drawable color filter to ImageView. 
             * @see http://stackoverflow.com/a/28108208/308836
             */
            if (Build.VERSION.SDK_INT <= 7) {
                if (p_drawable instanceof BitmapDrawable) {
                    try {
                        Object mBitmapState =         FieldUtils.readDeclaredField(p_drawable,   "mBitmapState", true);
                        Paint mPaint        = (Paint) FieldUtils.readDeclaredField(mBitmapState, "mPaint",       true);
                        p_imageView.setColorFilter(mPaint.getColorFilter());
                    }
                    catch (Exception e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
            }
    
            /*
             * Set image drawable.
             */
            p_imageView.setImageDrawable(p_drawable);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多