通过四个seekbar分别改变RBG和亮度,末尾想实现从图库加载图像,但是暂时不知道怎么实现;
package com.example.z_95_imageprocess;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private ImageView imageView;
private Paint paint;
private Bitmap bitmap,bitmapAlter;
private Canvas canvas;
private ColorMatrix cm;
private SeekBar seekBar1,seekBar2,seekBar3,seekBar4;
private Matrix matrix;
private Button button;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button_Secect);
imageView = (ImageView) findViewById(R.id.image);
seekBar1 = (SeekBar) findViewById(R.id.seekbar1);
seekBar1.setProgress(128);
seekBar2 = (SeekBar) findViewById(R.id.seekbar2);
seekBar2.setProgress(128);
seekBar3 = (SeekBar) findViewById(R.id.seekbar3);
seekBar3.setProgress(128);
seekBar4 = (SeekBar) findViewById(R.id.seekbar4);
seekBar4.setProgress(128);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lee);
bitmapAlter = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
matrix = new Matrix();
paint = new Paint();
paint.setColor(Color.BLACK);
cm = new ColorMatrix();
canvas = new Canvas(bitmapAlter);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(bitmapAlter);
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// int progress = seekBar.getProgress();
cm.set(new float[] {
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(bitmapAlter);
}
});
seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[] {
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(bitmapAlter);
}
});
seekBar3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[] {
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(bitmapAlter);
}
});
seekBar4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[] {
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(bitmapAlter);
}
});
}
//从图库选择照片
//TODO 怎么实现
public void Select(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data!=null) {
uri = data.getData();
imageView.setImageURI(uri);//暂时将选择的图片加载到imageview;
}
super.onActivityResult(requestCode, resultCode, data);
}
}