Android 使用ColorMatrix颜色矩阵改变图片风格

本文作为动画引文,希望大家能够阅读下一篇:

Android 动画介绍及自定义3D动画效果的基本使用


导语


    本文主要是写出一些简单的修图功能,用到关键技术是颜色矩阵 ColorMatrix类。老规矩,就先来看美丽的效果图吧!

咳咳。。。。虽然有些粗糙,但不妨碍了解技术。接下来,看代码如何实现。


实现效果图

    


代码实现

(1)XML代码参考:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.aiyang.animationdemo.MainActivity"
    android:padding="50dp"
    android:orientation="vertical">


    <TextView
        android:id="@+id/hello"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:textColor="#fff"
        android:text="切换" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/qq"/>

</LinearLayout>

qq图片附上,欢迎加群。


MainActivity代码参考:

public class MainActivity extends AppCompatActivity {
    TextView textView;
    int num = 0;//修图
    ImageView image;
    Bitmap mBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.hello);
        image  = findViewById(R.id.image);
        mBitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();//Drawble转bitmap
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (num<5){
                    num++;
                    switch (num){
                        case 0:
                            btnGray();
                            break;
                        case 1:
                            btnBlack();
                            break;
                        case 2:
                            btnRemeiber();
                            break;
                        case 3:
                            btnWhite();
                            break;
                        case 4:
                            btnBaohe();
                            break;
                    }
                    setImageMatrix(mBitmap,image);
                }else{
                    num = 0 ;
                }


            }
        });
    }


    private  float[] mColorMatrix=new float[20]; //设置20个颜色数值
    /**
     * 设置图片进行矩阵转换
     * @param bitmap
     */
    private void setImageMatrix(Bitmap bitmap, ImageView mImageView) {
        //创建一个 Bitmap
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
        //设置颜色矩阵
        ColorMatrix colorMatrix=new ColorMatrix();
        colorMatrix.set(mColorMatrix);
        //将Bitmap放在画板中,用画笔根据颜色过滤器重新绘制
        Canvas canvas = new Canvas(bmp);
        Paint paint=new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap,0,0, paint);
        //重新展示
        mImageView.setImageBitmap(bmp);
    }

    //灰色
    public void btnGray(){
        mColorMatrix[0]=0.33F;
        mColorMatrix[1]=0.59F;
        mColorMatrix[2]=0.11F;
        mColorMatrix[3]=0;
        mColorMatrix[4]=0;
        mColorMatrix[5]=0.33F;
        mColorMatrix[6]=0.59F;
        mColorMatrix[7]=0.11F;
        mColorMatrix[8]=0;
        mColorMatrix[9]=0;
        mColorMatrix[10]=0.33F;
        mColorMatrix[11]=0.59F;
        mColorMatrix[12]=0.11F;
        mColorMatrix[13]=0;
        mColorMatrix[14]=0;
        mColorMatrix[15]=0;
        mColorMatrix[16]=0;
        mColorMatrix[17]=0;
        mColorMatrix[18]=1;
        mColorMatrix[19]=0;
    }
    //反差
    public void btnBlack(){
        mColorMatrix[0]=-1;
        mColorMatrix[1]=0;
        mColorMatrix[2]=0;
        mColorMatrix[3]=1;
        mColorMatrix[4]=1;
        mColorMatrix[5]=0;
        mColorMatrix[6]=-1;
        mColorMatrix[7]=0;
        mColorMatrix[8]=1;
        mColorMatrix[9]=1;
        mColorMatrix[10]=0;
        mColorMatrix[11]=0;
        mColorMatrix[12]=-1;
        mColorMatrix[13]=1;
        mColorMatrix[14]=1;
        mColorMatrix[15]=0;
        mColorMatrix[16]=0;
        mColorMatrix[17]=0;
        mColorMatrix[18]=-1;
        mColorMatrix[19]=0;
    }
    //怀旧
    public void btnRemeiber(){
        mColorMatrix[0]=0.393F;
        mColorMatrix[1]=0.769F;
        mColorMatrix[2]=0.189F;
        mColorMatrix[3]=0;
        mColorMatrix[4]=0;
        mColorMatrix[5]=0.349F;
        mColorMatrix[6]=0.686F;
        mColorMatrix[7]=0.168F;
        mColorMatrix[8]=0;
        mColorMatrix[9]=0;
        mColorMatrix[10]=0.272F;
        mColorMatrix[11]=0.534F;
        mColorMatrix[12]=0.131F;
        mColorMatrix[13]=0;
        mColorMatrix[14]=0;
        mColorMatrix[15]=0;
        mColorMatrix[16]=0;
        mColorMatrix[17]=0;
        mColorMatrix[18]=1;
        mColorMatrix[19]=0;
    }
    //去色
    public void btnWhite(){
        mColorMatrix[0]=1.5F;
        mColorMatrix[1]=1.5F;
        mColorMatrix[2]=1.5F;
        mColorMatrix[3]=0;
        mColorMatrix[4]=-1;
        mColorMatrix[5]=1.5F;
        mColorMatrix[6]=1.5F;
        mColorMatrix[7]=1.5F;
        mColorMatrix[8]=0;
        mColorMatrix[9]=-1;
        mColorMatrix[10]=1.5F;
        mColorMatrix[11]=1.5F;
        mColorMatrix[12]=1.5F;
        mColorMatrix[13]=0;
        mColorMatrix[14]=-1;
        mColorMatrix[15]=0;
        mColorMatrix[16]=0;
        mColorMatrix[17]=0;
        mColorMatrix[18]=1;
        mColorMatrix[19]=0;
    }
    //饱和
    public void btnBaohe(){
        mColorMatrix[0]=1.438F;
        mColorMatrix[1]=-0.122F;
        mColorMatrix[2]=-0.016F;
        mColorMatrix[3]=0;
        mColorMatrix[4]=-0.03F;
        mColorMatrix[5]=-0.062F;
        mColorMatrix[6]=1.378F;
        mColorMatrix[7]=-0.016F;
        mColorMatrix[8]=0;
        mColorMatrix[9]=0.05F;
        mColorMatrix[10]=-0.062F;
        mColorMatrix[11]=-0.122F;
        mColorMatrix[12]=1.483F;
        mColorMatrix[13]=0;
        mColorMatrix[14]=-0.02F;
        mColorMatrix[15]=0;
        mColorMatrix[16]=0;
        mColorMatrix[17]=0;
        mColorMatrix[18]=1;
        mColorMatrix[19]=0;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾阳Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值