Android ColorMatrixColorFilter 处理图像

在上一章节中虽然可以实现图片的各种效果,但是,想必,你也输入的比较累了吧,其实还有一种更简单的方式,那就是使用滑块来代替手动输入


  1. 创建一个 空的 Android 项目 cn.twle.android.ColorMatrixSlider

  2. 下载 /static/i/mychild.png 并放到 res/drawable 目录

  3. 创建一个一个图片处理的工具类 ImageHelper ,传入 Bitmap,色相,饱和度以及亮度,处理后,返回 处理后的图片

    package cn.twle.android.colormatrixslider;
    
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.ColorMatrix;
    import android.graphics.ColorMatrixColorFilter;
    import android.graphics.Paint;
    
    public class ImageHelper {
        /**
         * 该方法用来处理图像,根据色调,饱和度,亮度来调节
         *
         * @param bm:要处理的图像
         * @param hue:色调
         * @param saturation:饱和度
         * @param lum:亮度
         *
         */
        public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
            Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
            ColorMatrix hueMatrix = new ColorMatrix();
            hueMatrix.setRotate(0, hue);    //0代表R,红色
            hueMatrix.setRotate(1, hue);    //1代表G,绿色
            hueMatrix.setRotate(2, hue);    //2代表B,蓝色
    
            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);
    
            paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
            canvas.drawBitmap(bm, 0, 0, paint);
    
            return bmp;
        }
    }
    
  4. 修改布局文件 activity_main.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="5dp">
    
        <ImageView
            android:id="@+id/img_meizi"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="24dp"
            android:layout_marginTop="24dp" />
    
        <TextView
            android:id="@+id/txt_hue"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_below="@id/img_meizi"
            android:gravity="center"
            android:text="色调    :"
            android:textSize="18sp" />
    
        <SeekBar
            android:id="@+id/sb_hue"
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:layout_below="@id/img_meizi"
            android:layout_toRightOf="@id/txt_hue" />
    
        <TextView
            android:id="@+id/txt_saturation"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_below="@id/txt_hue"
            android:gravity="center"
            android:text="饱和度:"
            android:textSize="18sp" />
    
        <SeekBar
            android:id="@+id/sb_saturation"
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:layout_below="@id/sb_hue"
            android:layout_toRightOf="@id/txt_saturation" />
    
        <TextView
            android:id="@+id/txt_lun"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_below="@id/txt_saturation"
            android:gravity="center"
            android:text="亮度    :"
            android:textSize="18sp" />
    
        <SeekBar
            android:id="@+id/sb_lum"
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:layout_below="@id/sb_saturation"
            android:layout_toRightOf="@id/txt_lun" />
    
    </RelativeLayout>
    
  5. 修改 MainActivity.java

    package cn.twle.android.colormatrixslider;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.SeekBar;
    
    public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    
        private ImageView img_meizi;
        private SeekBar sb_hue;
        private SeekBar sb_saturation;
        private SeekBar sb_lum;
        private final static int MAX_VALUE = 255;
        private final static int MID_VALUE = 127;
        private float mHue = 0.0f;
        private float mStauration = 1.0f;
        private float mLum = 1.0f;
        private Bitmap mBitmap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mychild);
            bindViews();
        }
    
        private void bindViews() {
            img_meizi = (ImageView) findViewById(R.id.img_meizi);
            sb_hue = (SeekBar) findViewById(R.id.sb_hue);
            sb_saturation = (SeekBar) findViewById(R.id.sb_saturation);
            sb_lum = (SeekBar) findViewById(R.id.sb_lum);
    
            img_meizi.setImageBitmap(mBitmap);
            sb_hue.setMax(MAX_VALUE);
            sb_hue.setProgress(MID_VALUE);
            sb_saturation.setMax(MAX_VALUE);
            sb_saturation.setProgress(MID_VALUE);
            sb_lum.setMax(MAX_VALUE);
            sb_lum.setProgress(MID_VALUE);
    
            sb_hue.setOnSeekBarChangeListener(this);
            sb_saturation.setOnSeekBarChangeListener(this);
            sb_lum.setOnSeekBarChangeListener(this);
    
        }
    
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            switch (seekBar.getId()) {
                case R.id.sb_hue:
                    mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;
                    break;
                case R.id.sb_saturation:
                    mStauration = progress * 1.0F / MID_VALUE;
                    break;
                case R.id.sb_lum:
                    mLum = progress * 1.0F / MID_VALUE;
                    break;
            }
            img_meizi.setImageBitmap(ImageHelper.handleImageEffect(mBitmap, mHue, mStauration, mLum));
        }
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}
    }
    

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.