android 圆角图片 处理
2014-12-09 16:41
337 查看
在Activity中调用
[java]
view plaincopyprint?
/* 将头像转为圆形 */
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head);
head.setImageBitmap(ImageHelper.toRoundBitmap(bmp));
[java]
view plaincopyprint?
//处理类
public class ImageHelper {
/**
* 转换图片成圆形
* @param bitmap 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height /
2;
float clip = (width - height) /
2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap. createBitmap(width,
height, Config. ARGB_8888);
Canvas canvas = new Canvas(output);
final int color =
0xff424242;
final Paint paint =
new Paint();
final Rect src =
new Rect((int)left, (
int)top, (int)right, (int)bottom);
final Rect dst =
new Rect((int)dst_left, (
int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF =
new RectF(dst);
paint.setAntiAlias( true);
canvas.drawARGB(0,
0, 0,
0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
// 下载图片转为bitmap
public static Bitmap getHttpBitmap(String url) {
URL u = null;
Bitmap bmp = null;
try {
u = new URL(url);
} catch (Exception e) {
// TODO: handle exception
}
try {
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput( true);
conn.connect();
InputStream ins = conn.getInputStream();
bmp = BitmapFactory. decodeStream(ins);
ins.close();
} catch (Exception e) {
// TODO: handle exception
}
return bmp;
}
}
[java]
view plaincopyprint?
/* 将头像转为圆形 */
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head);
head.setImageBitmap(ImageHelper.toRoundBitmap(bmp));
/* 将头像转为圆形 */ Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.head); head.setImageBitmap(ImageHelper.toRoundBitmap(bmp));
[java]
view plaincopyprint?
//处理类
public class ImageHelper {
/**
* 转换图片成圆形
* @param bitmap 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height /
2;
float clip = (width - height) /
2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap. createBitmap(width,
height, Config. ARGB_8888);
Canvas canvas = new Canvas(output);
final int color =
0xff424242;
final Paint paint =
new Paint();
final Rect src =
new Rect((int)left, (
int)top, (int)right, (int)bottom);
final Rect dst =
new Rect((int)dst_left, (
int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF =
new RectF(dst);
paint.setAntiAlias( true);
canvas.drawARGB(0,
0, 0,
0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
// 下载图片转为bitmap
public static Bitmap getHttpBitmap(String url) {
URL u = null;
Bitmap bmp = null;
try {
u = new URL(url);
} catch (Exception e) {
// TODO: handle exception
}
try {
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput( true);
conn.connect();
InputStream ins = conn.getInputStream();
bmp = BitmapFactory. decodeStream(ins);
ins.close();
} catch (Exception e) {
// TODO: handle exception
}
return bmp;
}
}
//处理类 public class ImageHelper { /** * 转换图片成圆形 * @param bitmap 传入Bitmap对象 * @return */ public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap. createBitmap(width, height, Config. ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int)left, ( int)top, (int)right, (int)bottom); final Rect dst = new Rect((int)dst_left, ( int)dst_top, (int)dst_right, (int)dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias( true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; } // 下载图片转为bitmap public static Bitmap getHttpBitmap(String url) { URL u = null; Bitmap bmp = null; try { u = new URL(url); } catch (Exception e) { // TODO: handle exception } try { HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setConnectTimeout(0); conn.setDoInput( true); conn.connect(); InputStream ins = conn.getInputStream(); bmp = BitmapFactory. decodeStream(ins); ins.close(); } catch (Exception e) { // TODO: handle exception } return bmp; } }
相关文章推荐
- Android图片圆角处理
- Android之图片处理(图片合成、图片圆角、图片翻转、图片缩放)
- android 圆角图片 处理
- android中图片的处理(绘制圆形,圆角||缩略图)
- android中处理图片成圆角
- Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
- android图片圆角处理,重写onDraw方法
- Android 图片加圆角以及时间处理工具类
- Android 利用多线程异步加载图,并把图片处理成圆角,并更新视图
- android 指定角的圆角图片处理
- android 背景圆角以及图片圆角处理
- android图片的缩放、圆角处理
- Android图片缩放、圆角处理以及倒影特效代码
- android 指定组件的圆角图片处理
- Android图片圆角处理
- Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
- android 图片圆角处理
- android 图片倒影 bitmap与drawable的转换 把图片处理成指定大小 图片圆角处理 dip转换成px
- Android:继承ImageView 实现 圆角图片,圆形图片处理。
- Android图片处理——压缩、剪裁、圆角、保存