【问题标题】:Imageview set color filter to gradientImageview 将滤色器设置为渐变
【发布时间】:2016-06-12 14:59:29
【问题描述】:

我有一张白色图像,我想用渐变着色。我不想生成一堆图像,每个图像都有特定的渐变色,我想在代码(不是 xml)中执行此操作。

要更改图像的颜色,我使用

imageView.setColorFilter(Color.GREEN);

这很好用。但是如何应用渐变色而不是纯色? LinearGradient 没有帮助,因为 setColorFilter 不能应用于 Shader 对象。

编辑:这是我的图片:

这就是我想要的:

这就是我得到的:

【问题讨论】:

  • 你是在xml中使用ImageView来绘制图像的吗?
  • @SQLiteNoob 不,我在代码中动态创建它们。

标签: android imageview gradient


【解决方案1】:

你必须得到你的ImageView 中的Bitmap 并用Shader 重绘相同的Bitmap

public void clickButton(View v){
    Bitmap myBitmap = ((BitmapDrawable)myImageView.getDrawable()).getBitmap();

    Bitmap newBitmap = addGradient(myBitmap);
    myImageView.setImageDrawable(new BitmapDrawable(getResources(), newBitmap));
}


public Bitmap addGradient(Bitmap originalBitmap) {
    int width = originalBitmap.getWidth();
    int height = originalBitmap.getHeight();
    Bitmap updatedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(updatedBitmap);

    canvas.drawBitmap(originalBitmap, 0, 0, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, 0, 0, height, 0xFFF0D252, 0xFFF07305, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawRect(0, 0, width, height, paint);

    return updatedBitmap;
}

更新 3 我更改了:渐变颜色、LinearGradient 宽度 = 0 和 PorterDuffXfermode。 这里有一张好图了解 PorterDuffXfermode:

【讨论】:

  • 这如何与图像视图一起使用?
  • @Malfunction 对你有用吗?
  • 这不起作用 - 渐变设置为矩形,而不是形状本身。
  • @Malfunction,您的问题是关于 ImageView 中的白色图像而不是形状。你是什​​么形状的?您是否必须在 ImageView 中检测形状?
  • 没错,我有一个白色图像,我想用渐变着色。我得到的是沿着整个 imageView 的宽度和高度(它的矩形)的渐变,而不是图像本身。
【解决方案2】:

你可以使用选择器

main_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout         xmlns:android="http://schemas.android.com/apk/res/.    android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:orientation="horizontal"
    android:background="@drawable/main_header_selector"> 
</LinearLayout>

main_header_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector     xmlns:android="http://schemas.android.com/apk/res/.     android">
<item>
    <shape>
        <gradient
        android:angle="90"
        android:startColor="#FFFF0000"
        android:endColor="#FF00FF00"
        android:type="linear" />
    </shape>
</item>
</selector>

相同的背景可以应用于 ImageView。

要动态定义和使用选择器,请参考此链接: Dynamically defining and using selectors

【讨论】:

  • 这是为了应用背景。我需要改变图像本身的东西。
【解决方案3】:

创建一个 XML 文件,并将其放置在 drawable 文件夹中。

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#CCb1e7fa"
    android:centerColor="#B3ffffff"
    android:endColor="#CCb1e7fa"
    android:angle="180" />
<corners android:radius="5dp" />

</shape>

接下来将其作为背景添加到您的图像视图中

    <ImageView
            android:id="@+id/umageview1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/gradient"
            android:layout_centerHorizontal="true"
            />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2021-08-04
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多