gpt4 book ai didi

android - setColorFilter 只影响 src,还是影响 src 和背景?

转载 作者:行者123 更新时间:2023-11-29 16:01:24 28 4
gpt4 key购买 nike

注意 - 这个问题涉及 android:background 属性。

令人困惑的是,“背景”也经常被用来表示“用于 Android: src 的照片的‘背景颜色’”。

说我有。

<ImageView
android:src="@drawable/white_foreground_thing"
android:background="@drawable/some_bg_thing"
/>

我要更改setColorFilter

事实上,这是否会影响仅“src”——也就是说前景

或者它是否也会影响背景。?

android:background="@drawable/some_bg_thing"

在我的示例中,前景(即 src)是一个 40 像素的白点(因此,我将制作“不同颜色的点”)。

但是背景只是一个普通的按钮背景,实际上还带有一些透明度等等。

所以我不想影响背景;我将只更改不同按钮的“大点的颜色”。

请注意,(a) 很难通过测试来确定,因为 setColorFilter 的微妙之处,以及 (b) 我真的无法在 doco 中找到它(对于你们这些 Android 家伙来说可能是显而易见的!)

此外 - PosterDuff.Mode 与此相关吗?或者这完全无关紧要?

(例如,https://stackoverflow.com/a/18954101/294884)

顺便说一句,在许多情况下,这是一个很棒的解决方案。 https://stackoverflow.com/a/17756634/294884

(注意底部的彩色圆点)

但在这里,我只想知道到底 setColorFilter 是影响 .background 还是只影响 .src

最佳答案

不,只有您应用滤镜的图像。即:源代码。


现在这就是我如何叠加几张图片并更改其中一张的颜色。
您可以尝试将 @drawable/head_xxh 设置为 imgHairbackground(因此,摆脱一个 ImageView)并查看它是否像预期。

结果引用两个独立且重叠的 ImageView。

只是为了好玩和好奇,我试着自己实现你的想法。
准备好以下两张 xxhdpi 图像后(480 dpi,以便使它们能够很好地缩放 - 然后我将它们放在 /res/drawable-xxhdpi 文件夹中)

当然,我必须仔细调整图像的大小以使其完美匹配和重叠。

enter image description here和一头白发(你的副本,制作成“发白”-去饱和+玩亮度/对比度)enter image description here

我做了这个布局,其中头发图像与头部重叠:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f000"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/head_xxh"
/>
<ImageView
android:id="@+id/imgHair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/hair_wht_xxh"
/>
<Button
android:id="@+id/btnColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Random hair color"
android:onClick="clickHandler"
/>
</RelativeLayout>

这是我使用的代码:

package com.dergolem.abc_2;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class Generic
extends Activity
{
Random rnd = new Random();

Button btn = null;
ImageView img = null;

@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.hair);

img = (ImageView) findViewById(R.id.imgHair);
btn = (Button) findViewById(R.id.btnColor);
}

public void clickHandler(final View v)
{
colorize(rnd.nextInt(7));
}

private void colorize(final int num)
{
int clr = Color.WHITE;
switch (num)
{
case 0:
{
clr = Color.RED;
break;
}
case 1:
{
clr = Color.GREEN;
break;
}
case 2:
{
clr = Color.BLUE;
break;
}
case 3:
{
clr = Color.BLACK;
break;
}
case 4:
{
clr = Color.CYAN;
break;
}
case 5:
{
clr = Color.YELLOW;
break;
}
case 6:
{
clr = Color.parseColor("#ff888800");
break;
}
}

img.setColorFilter(clr, PorterDuff.Mode.MULTIPLY);
}
}

以及我得到的一些结果:

enter image description here enter image description here enter image description here enter image description here

即使这幅作品看起来像 Andy Wharol 的照片,但事实并非如此。这是我的。 :)
看起来就是您要找的结果。

[编辑]

我没有尝试这个新想法,但是(通过一些额外的工作)您甚至可以更改其他颜色:

  • 眼睛
  • 皮肤
  • 口红
  • 眼妆(这需要一些耐心)

关于android - setColorFilter 只影响 src,还是影响 src 和背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466997/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com