个人练习代码库/ColorMatrix/色调、饱和度、亮度调整

个人练习代码库/ColorMatrix/色调、饱和度、亮度调整_第1张图片个人练习代码库/ColorMatrix/色调、饱和度、亮度调整_第2张图片

1.创建实例化Bitmap Bitmap.config.ARGB_8888->每个像素存储在4字节上,每通道8位精度

2.设置偏移量,颜色参数和色光属性,通过postConcat混合属性

tips:在设置好处理的颜色矩阵后,用SetColorFilter方法new一个ColorMatrixColorFilter

3.在主方法中,设置图片,实例化,设置初始点progress/Seekbar     监听事件中添加逻辑

代码:

ImageHelper
public class ImageHelper {
    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();

        ColorMatrix mHueMatrix=new ColorMatrix();
        mHueMatrix.setRotate(0,hue);
        mHueMatrix.setRotate(1,hue);
        mHueMatrix.setRotate(2,hue);

        ColorMatrix mSaturation=new ColorMatrix();
        mSaturation.setSaturation(saturation);

        ColorMatrix mLum=new ColorMatrix();
        mLum.setScale(lum,lum,lum,1);

        ColorMatrix mImage=new ColorMatrix();
        mImage.postConcat(mHueMatrix);
        mImage.postConcat(mLum);
        mImage.postConcat(mSaturation);

        paint.setColorFilter(new ColorMatrixColorFilter(mImage));
        canvas.drawBitmap(bm,0,0,paint);
        return bmp;
    }

MainAc:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorMatrix;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;

/**
 * Created by YRC on 2017/10/21.
 */
public class PrimaryColor extends Activity implements SeekBar.OnSeekBarChangeListener {

    private static int MID_VALUE = 127;
    private ImageView mImageView;
    private float mHue, mStauration, mLum;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.primary_color);
        bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.test3);
        mImageView = (ImageView) findViewById(R.id.imageview);
        SeekBar mSeekbarhue = (SeekBar) findViewById(R.id.seekbarHue);
        SeekBar mSeekbarSaturation = (SeekBar) findViewById(R.id.seekbarSaturation);
        SeekBar mSeekbarLum = (SeekBar) findViewById(R.id.seekbatLum);
        mSeekbarhue.setOnSeekBarChangeListener(this);
        mSeekbarSaturation.setOnSeekBarChangeListener(this);
        mSeekbarLum.setOnSeekBarChangeListener(this);
        int MAX_VALUE = 255;
        mSeekbarhue.setMax(MAX_VALUE);
        mSeekbarSaturation.setMax(MAX_VALUE);
        mSeekbarLum.setMax(MAX_VALUE);
        mSeekbarhue.setProgress(MID_VALUE);
        mSeekbarSaturation.setProgress(MID_VALUE);
        mSeekbarLum.setProgress(MID_VALUE);
        mImageView.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar,
                                  int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.seekbarHue:
                mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;
                break;
            case R.id.seekbarSaturation:
                mStauration = progress * 1.0F / MID_VALUE;
                break;
            case R.id.seekbatLum:
                mLum = progress * 1.0F / MID_VALUE;
                break;
        }
        mImageView.setImageBitmap(ImageHelper.handleImageEffect(
                bitmap, mHue, mStauration, mLum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}
个人练习代码库/ColorMatrix/色调、饱和度、亮度调整_第3张图片

你可能感兴趣的:(代码库(个人练习))