机器人编程ImageView的变色 [英] android imageview programatically change color

查看:292
本文介绍了机器人编程ImageView的变色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有在ImageView的显示灰度图像,可以编程方式改变其颜色?如果它的事项,图像有背景透明度这将需要保持透明。所以,我只是想改变实际图像部分的颜色。

If I have a grayscale image displayed in an imageview, can I programatically change its color? If it matters, the image has background transparency which would need to remain transparent. So I only want to change the color of the actual image part.

推荐答案

我以前写的

下面是code,仅供参考:

below is code for reference only:

public class ColorImageView extends ImageView
{
private Context context;
private boolean showColor;
private Paint mPaint;
private ColorMatrix cm;
private Bitmap mBitmap;

private float[] color = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0 };
private float[] normal = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
        0, 0, 1, 0 };

public ColorImageView(Context context)
{
    super(context);
    init(context);
}

public ColorImageView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    init(context);
}

public ColorImageView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context)
{
    this.context = context;
    showColor = false;
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    cm = new ColorMatrix();
}

@Override
public void setImageResource(int resId)
{
    // TODO Auto-generated method stub
    super.setImageResource(resId);
    mBitmap = BitmapFactory.decodeResource(context.getResources(), resId);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas)
{
    // super.onDraw(canvas);
    Paint paint = mPaint;
    paint.setColorFilter(null);
    canvas.drawBitmap(mBitmap, 0, 0, paint);
    if (isShowColor())
    {
        cm.set(color);
    }
    else
    {
        cm.set(normal);
    }
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    canvas.drawBitmap(mBitmap, 0, 0, paint);
}

public void setColor(int color)
{
    float red = Color.red(color);
    float green = Color.green(color);
    float blue = Color.blue(color);
    float alpha = Color.alpha(color);
    // 0,6,12,18
    this.color[0] = red / 0xFF;
    this.color[6] = green / 0xFF;
    this.color[12] = blue / 0xFF;
    this.color[18] = alpha / 0xFF;
    setShowColor(true);
}

public boolean isShowColor()
{
    return showColor;
}

//set true to show custom color
public void setShowColor(boolean showColor)
{
    this.showColor = showColor;
}
}

用法:

1.put ColorImageView在XML

1.put ColorImageView in xml

2.设置的ImageView在code SRC

2.set ImageView's src in code

3.setColor($ {}颜色)为您的自定义颜色

3.setColor(${color}) for your custom color

4.setShowColor(真),如果你想显示的颜色。

4.setShowColor(true) if your want to show the color.

5.ColorImageView.invalidate()。(如果忘记了这是必要的)

5.ColorImageView.invalidate().(forget if this is necessary)

那么颜色偏移的ImageView就会显示。

then the color offset ImageView will show.

这篇关于机器人编程ImageView的变色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆