图片的缩放
public class MainActivity extends Activity {
private ImageView iv1;
private ImageView iv2;
private ImageView iv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv1 = (ImageView) this.findViewById(R.id.iv1);
this.iv2 = (ImageView) this.findViewById(R.id.iv2);
this.iv3 = (ImageView) this.findViewById(R.id.iv3);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
this.iv1.setImageBitmap(bitmap1);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap1.getWidth(),
bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(bitmap2);
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
0.5f, 0, 0,
0, 0.5f, 0,
0, 0, 1
});
canvas.drawBitmap(bitmap1, matrix, new Paint());
iv2.setImageBitmap(bitmap2);
Bitmap bitmap3 = Bitmap.createBitmap(bitmap1.getWidth() * 2,
bitmap1.getHeight() * 2, bitmap1.getConfig());
canvas = new Canvas(bitmap3);
matrix.setScale(2, 2);
canvas.drawBitmap(bitmap1, matrix, new Paint());
iv3.setImageBitmap(bitmap3);
}
}
图片的旋转
public class MainActivity extends Activity {
private ImageView iv1;
private ImageView iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv1 = (ImageView) this.findViewById(R.id.iv1);
this.iv2 = (ImageView) this.findViewById(R.id.iv2);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
this.iv1.setImageBitmap(bitmap1);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap1.getWidth(),
bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(bitmap2);
Matrix matrix = new Matrix();
// 以图片中心为圆心旋转180度
matrix.setRotate(180, bitmap1.getWidth() / 2, bitmap1.getHeight() / 2);
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap1, matrix, paint);
iv2.setImageBitmap(bitmap2);
}
}
图片的平移&镜面&倒影效果public class MainActivity extends Activity {
private ImageView iv1;
private ImageView iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv1 = (ImageView) this.findViewById(R.id.iv1);
this.iv2 = (ImageView) this.findViewById(R.id.iv2);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
R.drawable.emo_im_cool);
this.iv1.setImageBitmap(bitmap1);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap1.getWidth(),
bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(bitmap2);
Matrix matrix = new Matrix();
// 镜子效果
// matrix.setScale(-1, 1);
// matrix.postTranslate(bitmap1.getWidth(), 0);
// 倒影效果
matrix.setScale(1, -1);
matrix.postTranslate(0, bitmap1.getHeight());
canvas.drawBitmap(bitmap1, matrix, new Paint());
iv2.setImageBitmap(bitmap2);
}
}
图片的颜色处理
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private ImageView iv;
private SeekBar sb_red;
private SeekBar sb_green;
private SeekBar sb_blue;
private SeekBar sb_brightness; //亮度
private SeekBar sb_saturation; //饱和度
private Bitmap preBitmap;
private Bitmap afterBitmap;
private Canvas canvas;
ColorMatrix matrix;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initLintener();
}
private void initData() {
this.iv = (ImageView) this.findViewById(R.id.iv);
this.sb_red = (SeekBar) this.findViewById(R.id.sb_red);
this.sb_green = (SeekBar) this.findViewById(R.id.sb_green);
this.sb_blue = (SeekBar) this.findViewById(R.id.sb_blue);
this.sb_brightness = (SeekBar) this.findViewById(R.id.sb_brightness);
this.sb_saturation = (SeekBar) this.findViewById(R.id.sb_saturation);
Options opts = new Options();
opts.inSampleSize = 2;
// 从资源文件获取一张不可修改的图片
this.preBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.after,opts );
// 创建一张可以被修改的空白图片
this.afterBitmap = Bitmap.createBitmap(
this.preBitmap.getWidth(), this.preBitmap.getHeight(),
this.preBitmap.getConfig());
// 以空白图片为模版创建一张画布
this.canvas = new Canvas(this.afterBitmap);
// 创建画笔
this.paint = new Paint();
// 创建颜色矩阵
this.matrix = new ColorMatrix();
this.canvas.drawBitmap(this.preBitmap, new Matrix(), this.paint);
this.iv.setImageBitmap(afterBitmap);
}
private void initLintener() {
this.sb_red.setOnSeekBarChangeListener(this);
this.sb_green.setOnSeekBarChangeListener(this);
this.sb_blue.setOnSeekBarChangeListener(this);
this.sb_brightness.setOnSeekBarChangeListener(this);
this.sb_saturation.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sb_red:
this.matrix.set(new float[]{
progress/128f,0,0,0,0,//红色
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0
});
break;
case R.id.sb_green:
this.matrix.set(new float[]{
1,0,0,0,0,
0,progress/128f,0,0,0,//绿色
0,0,1,0,0,
0,0,0,1,0
});
break;
case R.id.sb_blue:
this.matrix.set(new float[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,progress/128f,0,0, // 蓝色
0,0,0,1,0
});
break;
case R.id.sb_brightness:
this.matrix.set(new float[]{
progress/128f,0,0,0,0,
0,progress/128f,0,0,0,
0,0,progress/128f,0,0,
0,0,0,1,0
});
break;
case R.id.sb_saturation:
this.matrix.setSaturation(progress/128f);
break;
}
this.paint.setColorFilter(new ColorMatrixColorFilter(matrix));
this.canvas.drawBitmap(this.preBitmap, new Matrix(), this.paint);
this.iv.setImageBitmap(afterBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
?<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/sb_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="128" />
<SeekBar
android:id="@+id/sb_green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="128" />
<SeekBar
android:id="@+id/sb_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="128" />
<SeekBar
android:id="@+id/sb_brightness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="128" />
<SeekBar
android:id="@+id/sb_saturation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="128" />
</LinearLayout>
图片的合成
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) this.findViewById(R.id.iv);
Bitmap beauty = BitmapFactory.decodeResource(getResources(), R.drawable.after);
Bitmap after = Bitmap.createBitmap(beauty.getWidth(),beauty.getHeight(), beauty.getConfig());
Canvas canvas = new Canvas(after);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(beauty, new Matrix(), paint );
Bitmap bt = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bt, new Matrix(), paint );
iv.setImageBitmap(after);
}
}