demo地址:
图片平移、镜面、倒影:http://download.csdn.net/detail/baopengjian/9811718
旋转:http://download.csdn.net/detail/baopengjian/9811723
颜色变化:http://download.csdn.net/detail/baopengjian/9811725
#1 缩放
1) 缩放到指定宽高:
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
/**
* @param defaultBitmap 要对其进行操作的图片
* @param width 要缩放的效果宽度
* @return 缩放后的图片
*/
public static Bitmap resizeImageByWidth(Bitmap defaultBitmap,
int targetWidth) {
int rawWidth = defaultBitmap.getWidth();
int rawHeight = defaultBitmap.getHeight();
float targetHeight = targetWidth * (float) rawHeight / (float) rawWidth;
float scaleWidth = targetWidth / (float) rawWidth;
float scaleHeight = targetHeight / (float) rawHeight;
Matrix localMatrix = new Matrix();
localMatrix.postScale(scaleHeight, scaleWidth);
return Bitmap.createBitmap(defaultBitmap, 0, 0, rawWidth, rawHeight, localMatrix, true);
}
#2 平移、镜面、倒影
平移:
镜面:
倒影:
demo:demo004. 图片的平移镜面倒影效果
public class MainActivity extends Activity {
private ImageView iv1,iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
iv1.setImageBitmap(bitmap1);
Bitmap alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Matrix matrix = new Matrix();
//平移
//
matrix.postTranslate(10, 10);
//镜面效果
// matrix.setScale(-1, 1);
// matrix.postTranslate(bitmap1.getWidth(), 0);
//倒影效果
matrix.setScale(1, -1);
matrix.postTranslate(0, bitmap1.getHeight());
canvas.drawBitmap(bitmap1, matrix, paint);
iv2.setImageBitmap(alterBitmap);
}
}
#3 旋转
注意:#1 对图片进行旋转等修改操作时需要重新创建一个Bitmap;
#2 对图片进行旋转操作时,图片会失帧,需要设置抗锯齿
private ImageView iv1,iv2;
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
iv1.setImageBitmap(bitmap1);
Bitmap alBitmap = Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(),bitmap1.getConfig());
Canvas canvas = new Canvas(alBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Matrix metrics = new Matrix();
metrics.setRotate(15);
paint.setAntiAlias(true);
canvas.drawBitmap(
bitmap1, metrics, paint);
iv2.setImageBitmap(alBitmap);
#4 模糊效果
/** 模糊效果方法一
* 大图片先要进行压缩
* @param bmp
* @return
*/
private Bitmap blurImage(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixColor = 0; int newR = 0; int newG = 0; int newB = 0; int newColor = 0;
int[][] colors = new int[9][3];
for (int i = 1, length = width - 1; i < length; i++) {
for (int k = 1, len = height - 1; k < len; k++) {
for (int m = 0; m < 9; m++) {
int s = 0; int p = 0;
switch(m){
case 0:
s = i - 1;
p = k - 1;
break;
case 1:
s = i;
p = k - 1;
break;
case 2:
s = i + 1;
p = k - 1;
break;
case 3:
s = i + 1;
p = k;
break;
case 4:
s = i + 1;
p = k + 1;
break;
case 5:
s = i;
p = k + 1;
break;
case 6:
s = i - 1;
p = k + 1;
break;
case 7:
s = i - 1;
p = k;
break;
case 8:
s = i;
p = k;
}
pixColor = bmp.getPixel(s, p);
colors[m][0] = Color.red(pixColor);
colors[m][1] = Color.green(pixColor);
colors[m][2] = Color.blue(pixColor);
}
for (int m = 0; m < 9; m++) {
newR += colors[m][0];
newG += colors[m][1];
newB += colors[m][2];
}
newR = (int) (newR / 9F);
newG = (int) (newG / 9F);
newB = (int) (newB / 9F);
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
newColor = Color.argb(255, newR, newG, newB);
bitmap.setPixel(i, k, newColor);
newR = 0;
newG = 0;
newB = 0;
}
}
return bitmap;
}
/**模糊效果方法二
* 大图片先要进行压缩
* 柔化效果(高斯模糊)(优化后比上面快三倍)
* @param bmp
* @return
*/
public static Bitmap blurImageAmeliorate(Bitmap bmp)
{
long start = System.currentTimeMillis();
// 高斯矩阵
int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int delta = 16; // 值越小图片会越亮,越大则越暗
int idx = 0;
int[] pixels = new int[width * height];
bmp.getPixels(pixels,0 , width,0 , 0, width, height);
for (int i = 1, length = height - 1; i < length; i++)
{
for (int k = 1, len = width - 1; k < len; k++)
{
idx = 0;
for (int m = -1; m <= 1; m++)
{
for (int n = -1; n <= 1; n++)
{
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + (int) (pixR * gauss[idx]);
newG = newG + (int) (pixG * gauss[idx]);
newB = newB + (int) (pixB * gauss[idx]);
idx++;
}
}
newR /= delta;
newG /= delta;
newB /= delta;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0 ;
newG = 0;
newB = 0;
}
}
bitmap.setPixels(pixels,0 , width,0 ,0 , width, height);
long end = System.currentTimeMillis();
return bitmap;
}
#5 颜色处理
Demo:demo004b. 图形的颜色处理
public class MainActivity extends Activity {
private ImageView iv;
private SeekBar seekBar1,seekBar2,seekBar3;
private Bitmap bitmap1;
private Canvas canvas;
private Bitmap alterBitmap;
private Matrix matrix;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
seekBar3 = (SeekBar) findViewById(R.id.seekBar3);
bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setColor(Color.BLACK);
matrix = new Matrix();
final ColorMatrix cm = new ColorMatrix();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap1, matrix, paint);
iv.setImageBitmap(alterBitmap);
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[]{
progress/128f,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(bitmap1, matrix, paint);
iv.setImageBitmap(alterBitmap);
}
});
seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[]{
1,0,0,0,0,
0,progress/128f,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap1, matrix, paint);
iv.setImageBitmap(alterBitmap);
}
});
seekBar3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
cm.set(new float[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,progress/128f,0,0,
0,0,0,1,0,
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap1, matrix, paint);
iv.setImageBitmap(alterBitmap);
}
});
}
}