package com.itheima.pickimg;
import java.io.FileNotFoundException;
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.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private ImageView iv;
private ImageView iv_after;
private Bitmap alterBitmap;
private Canvas canvas;
private Paint paint;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv_after = (ImageView) findViewById(R.id.iv_after);
SeekBar sb_r = (SeekBar) findViewById(R.id.sb_r);
SeekBar sb_b = (SeekBar) findViewById(R.id.sb_b);
SeekBar sb_g = (SeekBar) findViewById(R.id.sb_g);
sb_r.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 0~255
int progress = seekBar.getProgress();
float rate = (progress - 122) / 255.0f; // -0.5~0.5f
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] { 1 + rate, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
paint = new Paint();
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv_after.setImageBitmap(alterBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
sb_b.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 0~255
int progress = seekBar.getProgress();
float rate = (progress - 122) / 255.0f; // -0.5~0.5f
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
1, 0, 0, 0, 0,
0, 1+rate, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
});
paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv_after.setImageBitmap(alterBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
sb_g.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 0~255
int progress = seekBar.getProgress();
float rate = (progress - 122) / 255.0f; // -0.5~0.5f
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1+rate, 0, 0,
0, 0, 0, 1, 0
});
paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv_after.setImageBitmap(alterBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
public void selectImage(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
// 获取要查看的图片的路径
try {
Uri uri = data.getData();
iv.setImageURI(uri);
// 得到原图的bitmap 只读
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
// 空白可可以修改的bitmap
alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
// 创建画布
canvas = new Canvas(alterBitmap);
// 创建画笔
paint = new Paint();
paint.setColor(Color.BLACK);
Matrix matrix = new Matrix();
// matrix.setScale(1, -1);
//
// matrix.postTranslate(0, bitmap.getHeight());
canvas.drawBitmap(bitmap, matrix, paint);
iv_after.setImageBitmap(alterBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}