我想为imageview添加一个颜色过滤器

时间:2011-11-19 10:35:57

标签: android android-imageview porter-duff

我想将ColorFilter添加到ImageView

目前我正在使用:

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

我已在PotterDuff中检查了多种模式,例如SRC_INSRC等,但我在任何模式下都没有任何差异......所有模式都转整个ImageView为完美的红色。

我需要在现有图像中混合使用RED颜色,这样图像才能呈现出REDDISH色调....

5 个答案:

答案 0 :(得分:31)

正确的方法是使用PorterDuff.Mode.LIGHTEN

因此更新的代码将如下:

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);

答案 1 :(得分:7)

这对我有用:

res / colors.xml中的

<color name="highlight_color_filter">#A5FF0000</color>

在您的Activity中初始化过滤器并突出显示paint:

int highlightColor = context.getResources().getColor(R.color.highlight_color_filter);
PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(highlightColor, PorterDuff.Mode.SRC_ATOP);

Paint redHighLight = new Paint();
redHighLight.setColorFilter(targetHitFilter);
redHighLight.setAlpha(190);

然后将过滤器应用于ImageView:

ImageView iv=(ImageView)findViewById(ResIdOfImageToFilter);
iv.setColorFilter(redHighLight);

如果不起作用,请尝试应用于ImageView drawable:

iv.getDrawable().setColorFilter(redHighLight);
希望有所帮助。

答案 2 :(得分:7)

您可以在xml文件中使用 android:tint link)。示例:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_drawable"
    android:tint="@color/your_color" />

答案 3 :(得分:4)

其他解决方案,你可以保持PorterDuff.Mode.SRC_ATOP模式并使用另一个alpha来获得透明色。

我使用155作为Alpha值:

  final int semiTransparentGrey = Color.argb(155, 185, 185, 185);
  drawable.setColorFilter(semiTransparentGrey, PorterDuff.Mode.SRC_ATOP);

答案 4 :(得分:1)

在您的xml文件中,您可以使用 色调 例如

        <ImageView
            android:id="@+id/scrachImage_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:tint="@color/colorAccent"
            android:src="@drawable/eagle" />

如果您想以编程方式添加颜色过滤器,请使用

scratchImage_2.setColorFilter(Color.BLACK);

您也可以使用以下代码删除此滤镜:

scratchImage_2.setColorFilter(null);