如何更改位图图像的颜色在android的? [英] How to change Bitmap image color in android?

查看:168
本文介绍了如何更改位图图像的颜色在android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发中,我将图像设置为ImageView的Andr​​oid应用程序。现在的方案我想改变的位图图像的颜色。假设我的形象有红色开始,现在我需要将其更改为橙色。我该怎么办呢?请大家帮帮忙。

下面是我的code。我设法改变的不透明度,但我不知道如何改变颜色。

  @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        ImageView的IV =(ImageView的)findViewById(R.id.img);
        绘制对象D = getResources()getDrawable(R.drawable.pic1)。
        位图mNewBitmap =((BitmapDrawable)D).getBitmap();
        位图nNewBitmap = adjustOpacity(mNewBitmap);
        iv.setImageBitmap(nNewBitmap);
    }

    私人位图adjustOpacity(位图位图){
        INT宽度= bitmap.getWidth();
        INT高= bitmap.getHeight();
        位图DEST = Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
        INT []像素=新INT [宽*高]。
        bitmap.getPixels(像素,0,宽度,0,0,宽度,高度);
        dest.setPixels(像素,0,宽度,0,0,宽度,高度);
        返回DEST;
    }
 

解决方案

我的解决方案的一种。

  sourceBitmap中位图= BitmapFactory.de codeFILE(imgPath);
    浮动[]的ColorTransform = {
            0,1F,0,0,0,
            0,0,0F,0,0,
            0,0,0,0F,0,
            0,0,0,1F,0};

    嘉洛斯嘉洛斯=新嘉洛斯();
    colorMatrix.setSaturation(0F); //删除颜色
    colorMatrix.set(的ColorTransform); //应用红

    ColorMatrixColorFilter colorFilter =新ColorMatrixColorFilter(嘉洛斯);
    涂料粉刷=新的油漆();
    paint.setColorFilter(colorFilter);

    显示显示= getWindowManager()getDefaultDisplay()。

    位图resultBitmap = Bitmap.createBitmap(sourceBitmap中,0,(int)的(display.getHeight()* 0.15),display.getWidth(),(int)的(display.getHeight()* 0.75));

    image.setImageBitmap(resultBitmap);

    帆布油画=新的Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap,0,0,油漆);
 

I am developing an android application in which I set an image to imageview. Now programmatic I want to change the bitmap image color. Suppose my image have red color initially and now I need to change it to orange color. How can I do that? Please help.

Here is my code. I managed to change the opacity but I do not know how to change the color.

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView iv = (ImageView) findViewById(R.id.img);
        Drawable d = getResources().getDrawable(R.drawable.pic1);
        Bitmap mNewBitmap = ((BitmapDrawable)d).getBitmap();
        Bitmap nNewBitmap = adjustOpacity(mNewBitmap);
        iv.setImageBitmap(nNewBitmap);
    }

    private Bitmap adjustOpacity( Bitmap bitmap ) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        dest.setPixels(pixels, 0, width, 0, 0, width, height);
        return dest;
    } 

解决方案

I got kind of solution.

    Bitmap sourceBitmap = BitmapFactory.decodeFile(imgPath);
    float[] colorTransform = {
            0, 1f, 0, 0, 0, 
            0, 0, 0f, 0, 0,
            0, 0, 0, 0f, 0, 
            0, 0, 0, 1f, 0};

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0f); //Remove Colour 
    colorMatrix.set(colorTransform); //Apply the Red

    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    Paint paint = new Paint();
    paint.setColorFilter(colorFilter);   

    Display display = getWindowManager().getDefaultDisplay(); 

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, (int)(display.getHeight() * 0.15), display.getWidth(), (int)(display.getHeight() * 0.75));            

    image.setImageBitmap(resultBitmap);

    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, paint);

这篇关于如何更改位图图像的颜色在android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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