Android 图片特效处理的方法实例
程序员文章站
2023-12-12 19:19:52
1. 图片放缩
复制代码 代码如下:// zoom 放缩 public static bitmap zoombitmap(bitmap bitmap, int...
1. 图片放缩
复制代码 代码如下:
// zoom 放缩
public static bitmap zoombitmap(bitmap bitmap, int w, int h) {
int width = bitmap.getwidth();
int height = bitmap.getheight();
matrix matrix = new matrix();
float scalewidth = w / (float) width;
float scaleheight = h / (float) height;
matrix.postscale(scalewidth, scaleheight);
bitmap bitmap2 = bitmap.createbitmap(bitmap, 0, 0, width, height,
matrix, true);
return bitmap2;
}
2. 获得圆角图片
复制代码 代码如下:
// round corner bitmap 获得圆角图片
public static bitmap getroundcornerbitmap(bitmap bitmap, float roundpx /*圆角的半径*/) {
int width = bitmap.getwidth();
int height = bitmap.getheight();
bitmap bitmap2 = bitmap.createbitmap(width, height, config.argb_8888);
canvas canvas = new canvas(bitmap2);
final int color = 0xff424242;
final paint paint = new paint();
final rect rect = new rect(0, 0, width, height);
final rectf rectf = new rectf(rect);
paint.setcolor(color);
paint.setantialias(true);
canvas.drawargb(0, 0, 0, 0);
canvas.drawroundrect(rectf, roundpx, roundpx, paint);
paint.setxfermode(new porterduffxfermode(mode.src_in));
canvas.drawbitmap(bitmap, rect, rect, paint);
return bitmap2;
}
3. 获得带倒影的图片
复制代码 代码如下:
// reflect bitmap 获得带倒影的图片
public static bitmap createreflectedbitmap(bitmap bitmap) {
//倒影和图片本身的间距
final int reflectedgap = 4;
int width = bitmap.getwidth();
int height = bitmap.getheight();
matrix matrix = new matrix();
matrix.prescale(1, -1);
bitmap reflectedimage = bitmap.createbitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
bitmap reflectedbitmap = bitmap.createbitmap(width,
(height + height / 2), config.argb_8888);
canvas canvas = new canvas(reflectedbitmap);
canvas.drawbitmap(bitmap, 0, 0, null);
paint defaultpaint = new paint();
canvas.drawrect(0, height, width, height + reflectedgap, defaultpaint);
canvas.drawbitmap(reflectedimage, 0, height + reflectedgap, null);
paint paint = new paint();
lineargradient shader = new lineargradient(0, bitmap.getheight(), 0,
reflectedbitmap.getheight() + reflectedgap, 0x70ffffff,
0x00ffffff, tilemode.clamp);
paint.setshader(shader);
paint.setxfermode(new porterduffxfermode(mode.dst_in));
canvas.drawrect(0, height, width, reflectedbitmap.getheight()
+ reflectedgap, paint);
return reflectedbitmap;
}