主要以SimpleFilter的源码为例子详解,app可在百度应用商店下载
SimpleFilter源代码下载
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_change"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="220dp"
android:maxWidth="1000dp"
android:maxHeight="1000dp"
android:src="@drawable/origin"
/>
<SeekBar
android:id="@+id/sb_alpha"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dp"
/>
<SeekBar
android:id="@+id/sb_red"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dp"
/>
<SeekBar
android:id="@+id/sb_green"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dp"/>
<SeekBar
android:id="@+id/sb_blue"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/tv_display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:textSize="16sp"
android:textColor="#333333"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_photo"
android:layout_height="40dp"
android:layout_width="0dp"
android:layout_marginLeft="4dp"
android:text="拍 照"
android:textColor="#fff"
android:background="@xml/btn_blue"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_picture"
android:layout_height="40dp"
android:layout_width="0dp"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:textColor="#fff"
android:layout_marginRight="4dp"
android:background="@xml/btn_blue"
android:text="相 册"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_save"
android:layout_height="40dp"
android:layout_width="0dp"
android:textColor="#fff"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:background="@xml/btn_blue"
android:text="保 存"/>
<Button
android:id="@+id/btn_origin"
android:layout_height="40dp"
android:layout_width="0dp"
android:textColor="#fff"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:background="@xml/btn_blue"
android:text="初始化"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_delete"
android:layout_height="40dp"
android:layout_width="0dp"
android:layout_marginLeft="4dp"
android:text="清除保存文件"
android:textColor="#fff"
android:background="@xml/btn_blue"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_see"
android:layout_height="40dp"
android:layout_width="0dp"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:textColor="#fff"
android:layout_marginRight="4dp"
android:background="@xml/btn_blue"
android:text="查看保存文件"/>
</LinearLayout>
</LinearLayout>
java
package com.example.mpaint;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnSeekBarChangeListener,
OnClickListener {
private ImageView iv_change;
private SeekBar sb_alpha, sb_red, sb_green, sb_blue;
private TextView tv_dispaly;
private float a = 0f, r = 0f, g = 0f, b = 0f;
private String Path;
private Button btn_photo, btn_picture, btn_save, btn_origin,btn_delete,btn_see;
private Bitmap photo = null,photoorigin;
private File mCurrentPhotoFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//新建图片存储的目录
Path = Environment.getExternalStorageDirectory().toString()
+ "/mFilter";
File file = new File(Path);
if (!file.exists()) {
file.mkdir();
}
iv_change = (ImageView) findViewById(R.id.iv_change);
tv_dispaly = (TextView) findViewById(R.id.tv_display);
tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: "
+ String.valueOf((r)) + " " + "g: " + String.valueOf((g))
+ " " + "b: " + String.valueOf((b)));
//从资源文件中获取图片
photoorigin=BitmapFactory.decodeResource(getResources(), R.drawable.origin);
//初始化进度条
initSeek();
//初始化按钮
initButton();
}
private void initButton() {
btn_save = (Button) findViewById(R.id.btn_save);
btn_save.setOnClickListener(this);
btn_photo = (Button) findViewById(R.id.btn_photo);
btn_photo.setOnClickListener(this);
btn_picture = (Button) findViewById(R.id.btn_picture);
btn_picture.setOnClickListener(this);
btn_origin = (Button) findViewById(R.id.btn_origin);
btn_origin.setOnClickListener(this);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_delete.setOnClickListener(this);
btn_see = (Button) findViewById(R.id.btn_see);
btn_see.setOnClickListener(this);
}
private void initSeek() {
sb_alpha = (SeekBar) findViewById(R.id.sb_alpha);
sb_alpha.setOnSeekBarChangeListener(this);
sb_red = (SeekBar) findViewById(R.id.sb_red);
sb_red.setOnSeekBarChangeListener(this);
//设置进度条默认位置
sb_red.setProgress(50);
sb_green = (SeekBar) findViewById(R.id.sb_green);
sb_green.setOnSeekBarChangeListener(this);
sb_green.setProgress(50);
sb_blue = (SeekBar) findViewById(R.id.sb_blue);
sb_blue.setOnSeekBarChangeListener(this);
sb_blue.setProgress(50);
}
//重写OnSeekBarChangeListener
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sb_alpha:
a = (float) (progress / 100.0);
break;
case R.id.sb_red:
r = (float) ((progress - 50) / 5.0);
break;
case R.id.sb_green:
g = (float) ((progress - 50) / 5.0);
break;
case R.id.sb_blue:
b = (float) ((progress - 50) / 5.0);
break;
default:
break;
}
setArgb(1 - a, r + 1, g + 1, b + 1);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void setArgb(float alpha, float red, float green, float blue) {
tv_dispaly.setText("a: " + String.valueOf((a * 100)) + "% " + "r: "
+ String.valueOf((r)) + " " + "g: " + String.valueOf((g))
+ " " + "b: " + String.valueOf((b)));
//新建颜色矩阵
ColorMatrix mColorMatrix = new ColorMatrix(new float[] { red, 0, 0, 0,
0, 0, green, 0, 0, 0, 0, 0, blue, 0, 0, 0, 0, 0, alpha, 0 });
//使用颜色矩阵过滤图片
iv_change.getDrawable().setColorFilter(
new ColorMatrixColorFilter(mColorMatrix));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_origin:
a = 0f;
r = 0f;
g = 0f;
b = 0f;
tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: "
+ String.valueOf((r)) + " " + "g: " + String.valueOf((g))
+ " " + "b: " + String.valueOf((b)));
sb_alpha.setProgress(0);
sb_red.setProgress(50);
sb_green.setProgress(50);
sb_blue.setProgress(50);
//初始化图片
if(photo!=null) {
iv_change.setBackgroundColor(Color.parseColor("#00000000"));
iv_change.setImageBitmap(photo);}
else{
iv_change.setBackgroundColor(Color.parseColor("#00000000"));
iv_change.setImageBitmap(photoorigin);
}
break;
case R.id.btn_save:
saveImageView();
break;
case R.id.btn_photo:
takephoto();
break;
case R.id.btn_picture:
takepicture();
break;
case R.id.btn_see:
see();
break;
case R.id.btn_delete:
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示信息")
.setMessage("是否删除路径"+Path+"下的所有文件"+"?")
.setNegativeButton("确认",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
delete();
dialog.dismiss();
//判断是否完全删除
File file = new File(Path);
File[] childFile = file.listFiles();
if (childFile.length==0) {
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_LONG).show();
}
}
})
.setPositiveButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
break;
default:
break;
}
}
//查看文件
private void see() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
File file = new File(Path);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivityForResult(intent, 2);
}
private void delete() {
File file = new File(Path);
File[] childFile = file.listFiles();
for (File file2 : childFile) {
file2.delete();
}
}
private void takepicture() {
//相册
Intent intentPick = new Intent(Intent.ACTION_PICK, null);
intentPick.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intentPick, 0);
}
private void takephoto() {
//照相
mCurrentPhotoFile= new File(Path,
"temp.jpg");
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(mCurrentPhotoFile));
startActivityForResult(intent, 1);
}
private void saveImageView() {
//自定义文件名
final String path = Path +"/" +getNowDateTime("yyyyMMddHHmmss")
+ ".jpg";
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示信息")
.setMessage("是否将文件保存到"+Path+"?")
.setNegativeButton("确认",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//设置view可缓存.然后以bitmap获取view的缓存
iv_change.setDrawingCacheEnabled(true);
Bitmap photofinally = iv_change.getDrawingCache();
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(path));
//将bitmap压缩存储
photofinally.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
bos = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
iv_change.setDrawingCacheEnabled(false);
File file = new File(path);
dialog.dismiss();
if (file.exists()) {
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_LONG).show();
}
}
})
.setPositiveButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
public static String getNowDateTime(String format) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);// "yyyyMMdd"
String dateString = formatter.format(currentTime);
return dateString;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if (data != null) {
//相册uri获取
Uri uri = data.getData();
//共享相机获取的数据
ContentResolver cr = this.getContentResolver();
try {
//回收bitmap
if (photo != null && !photo.isRecycled()) {
photo.recycle();
System.gc();
}
//将好;获取的数据编码成bitmap
photo = BitmapFactory.decodeStream(cr.openInputStream(uri));
} catch (Exception e) {
e.printStackTrace();
}
iv_change.setBackgroundColor(Color.parseColor("#00000000"));
iv_change.setImageBitmap(photo);
}
break;
case 1:
//resultCode=-1为成功
if (resultCode==-1) {
ContentResolver cr = getContentResolver();
Uri fileUri = Uri.fromFile(mCurrentPhotoFile);
//刷新单个文件
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,fileUri));
//刷新sd卡
//sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, dirUri));
try {
if (photo != null && !photo.isRecycled()) {
photo.recycle();
System.gc();
}
photo = BitmapFactory.decodeStream(cr.openInputStream(fileUri));
} catch (Exception e) {
e.printStackTrace();
}
iv_change.setBackgroundColor(Color.parseColor("#00000000"));
iv_change.setImageBitmap(photo);
}
break;
case 2:
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}