初学颜色矩阵(修改图像的色调,饱和度,亮度)

从drawable文件夹获得Bitmap

// 获得bitmap
        bm =  BitmapFactory.decodeResource(((Activity)this).getResources(), R.drawable.ic_launcher);

MainActivity.java

package com.example.day0118;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.os.RecoverySystem.ProgressListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnSeekBarChangeListener{

    private ImageView img;
    private float hue,saturation,lum;
    private Bitmap bm;
    private final float  MID_VALIE =  50;//猜测:代表进度为50的时候是原始状态
    private SeekBar seekBarHue, seekBarSaturation, seekBarLum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.img);
        hue = 0;
        saturation = 0;
        lum = 0;

        // 获得bitmap
        bm =  BitmapFactory.decodeResource(((Activity)this).getResources(), R.drawable.ic_launcher);

        seekBarHue = (SeekBar) findViewById(R.id.seekbarHue);
        seekBarHue.setOnSeekBarChangeListener(this);
        seekBarSaturation = (SeekBar) findViewById(R.id.seekbarSaturation);
        seekBarSaturation.setOnSeekBarChangeListener(this);
        seekBarLum = (SeekBar) findViewById(R.id.seekbarLum);
        seekBarLum.setOnSeekBarChangeListener(this);


    }

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

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

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

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    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;

    }   
}

xml

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     xmlns:android="http://schemas.android.com/apk/res/android"
     >

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:id="@+id/img"/>

    <SeekBar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekbarHue"
        android:progress="50"/>

     <SeekBar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekbarSaturation"
         android:progress="50"/>


      <SeekBar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekbarLum"
        android:progress="50"/>

</LinearLayout>

效果
这里写图片描述

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页