android ColorMatrix 颜色矩阵的使用

 效果图

 

效果是不是很炫酷呢

那我们来看看我们要怎么实现它们吧

首先

我们来看一下我们的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.30"
        android:gravity="center"
        android:orientation="horizontal" >
        
            <ImageView
        android:id="@+id/mImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/letme" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >
              <SeekBar
        android:id="@+id/sb_Hue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
 />
            
            
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >
              
              <SeekBar
        android:id="@+id/sb_Sat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <SeekBar
                android:id="@+id/sb_Lum"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

话不多说上源码

 private Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
        /**
         * Android系统不允许直接修改原图,
         * 必须通过原图创建一个同样大小的bitmap,
         * 并将原图绘制到该Bitmap中,
         * 以一个副本的形式来修改图像
         */
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Paint p = new Paint();

        //色调矩阵
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);//红
        hueMatrix.setRotate(1, hue);//绿
        hueMatrix.setRotate(2, hue);//蓝

        //饱和度矩阵
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        //亮度矩阵
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);


        //图片矩阵
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        p.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        c.drawBitmap(bm, 0, 0,p);

        return bmp;

    }   

看懂了上面的接下来的操作就简单了(看代码吧)

 public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {

        switch (seekBar.getId()) {
        case R.id.sb_Hue:
            hue = (progress - MID_VALIE) * 1.0F / MID_VALIE * 180;
            break;
        case R.id.sb_Lum:
            saturation = progress * 1.0F / MID_VALIE;
            break;
        case R.id.sb_Sat:
            lum = progress * 1.0F / MID_VALIE;
            break;
        }

        img.setImageBitmap(handleImageEffect(bm, hue, saturation, lum));
    }

好了到这里就大功告成了,还有些初始化的代码我就不一一罗列了,写出核心代码足矣。

有兴趣的可以点击这里查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值