setColorFilter 在 Android 上不起作用2.2

发布于 2024-11-02 22:30:08 字数 829 浏览 0 评论 0原文

我在可绘制对象上使用 setColorFilter-Method 时遇到问题。

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

我的问题与此处描述的类似 Drawable.setColorFilter() 不起作用在 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 );

任何让它发挥作用的线索都非常感谢:)

i have a problem with the setColorFilter-Method on a drawable.

It works fine on Android 2.2 but not on a version lower than that.

My problem is similar to what is described here Drawable.setColorFilter() not working on Android 2.1, but that doesn't work for me...

I use this code which works fine on Android 2.2 but not on anything lower than that.

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 );

Any clues to get it working are much appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-11-09 22:30:08

我不知道是否还有其他方法可以解决此问题,但我发现使用 imageView.setBackgroundDrawable() 而不是 imageView.setImageDrawable() 可以解决 imageView.setImageDrawable() 上的此问题。 2.2.

I don't know if there's another way around this, but i found that using imageView.setBackgroundDrawable() instead of imageView.setImageDrawable() resolves this issue on < 2.2.

冬天旳寂寞 2024-11-09 22:30:08

是将相同的 colorFilter 应用于 Drawable 和 ImageView(不幸的是 Drawable.getColorFilter() 仅从 API 21 开始可用):

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

根据 @Joe 的评论和我对上面 Android 2.1 的发现,我认为更好的解决方法 ImageView.setBackgroundDrawable() 的缺点是它不尊重 ScaleType。

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

或者使用下面的 ImageViewCompat 类 - 它需要 Apache Commons Lang,您可以下载 JAR (和来源)来自 http://search.maven.org 或者如果您使用 Maven 或 Gradle:org.apache .commons/公共语言3。我发现最适合 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);
    }
}

Following on @Joe's comments and my findings for Android 2.1 from above, I think a better workaround is to apply the same colorFilter to both the Drawable as well as ImageView (unfortunatelly Drawable.getColorFilter() is only available from API 21 onwards):

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

One thing against for ImageView.setBackgroundDrawable() is that it won't respect ScaleType.

Probably an even better solution if you have extended ImageView already for other purposes is to fix it in the setImageDrawable() specifically for Android 2.1, where it would take the colorFilter through reflection from mBitmapState.mPaint.getColorFilter() and apply it to ImageView.

Or alternatively use ImageViewCompat class below - it requires Apache Commons Lang, you can download the JAR (and sources) from http://search.maven.org or if you use Maven or Gradle: org.apache.commons / commons-lang3. Last version to work best with Android 2.1 / Java 5 I found to be 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文