setColorFilter 在 Android 上不起作用2.2
我在可绘制对象上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道是否还有其他方法可以解决此问题,但我发现使用
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 ofimageView.setImageDrawable()
resolves this issue on < 2.2.是将相同的 colorFilter 应用于 Drawable 和 ImageView(不幸的是 Drawable.getColorFilter() 仅从 API 21 开始可用):
根据 @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);
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):
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);