gpt4 book ai didi

Android:以编程方式突出显示图像效果

转载 作者:行者123 更新时间:2023-11-29 22:03:27 24 4
gpt4 key购买 nike

有没有办法在不使用单独的可绘制资源的情况下以编程方式突出显示选项卡图标图像?

我尝试使用 PorterDuffColorFilter 但它看起来不太好:

// apply a color mask to the icon to mark it as selected
int selectedIconMaskColor = view.getResources().getColor(R.color.tab_icon_selected);
PorterDuffColorFilter selectedIconFilter = new PorterDuffColorFilter(selectedIconMaskColor,
PorterDuff.Mode.SRC_ATOP);
copyIconDrawable.setColorFilter(selectedIconFilter);

还有其他选择吗?

最佳答案

我最终使用了简单的图像处理类:

import android.graphics.Bitmap;
import android.graphics.Color;

/**
* Image with support for filtering.
*/
public class FilteredImage {

private Bitmap image;

private int width;

private int height;

private int[] colorArray;

/**
* Constructor.
*
* @param img the original image
*/
public FilteredImage(Bitmap img) {
this.image = img;
width = img.getWidth();
height = img.getHeight();

colorArray = new int[width * height];
image.getPixels(colorArray, 0, width, 0, 0, width, height);

applyHighlightFilter();
}

/**
* Get the color for a specified pixel.
*
* @param x x
* @param y y
* @return color
*/
public int getPixelColor(int x, int y) {
return colorArray[y * width + x];
}

/**
* Gets the image.
*
* @return Returns the image.
*/
public Bitmap getImage() {
return image;
}

/**
* Applies green highlight filter to the image.
*/
private void applyHighlightFilter() {
int a;
int r;
int g;
int b;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {

int c = getPixelColor(x, y);

a = Color.alpha(c);
r = Color.red(c);
g = Color.green(c);
b = Color.blue(c);

r = (int) (r * 0.8);
g = (int) (g * 1.6);
b = (int) (b * 0.8);

if (r > 255) {
r = 255;
}
if (r < 0) {
r = 0;
}
if (g > 255) {
g = 255;
}
if (g < 0) {
g = 0;
}
if (b > 255) {
b = 255;
}
if (b < 0) {
b = 0;
}

int resultColor = Color.argb(a, r, g, b);
image.setPixel(x, y, resultColor);
}
}
}

}

关于Android:以编程方式突出显示图像效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395329/

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