您的位置:首页 > 移动开发 > Android开发

【Android】【Drawable】图片圆形化(RoundedBitmapDrawable)

2015-01-28 18:06 501 查看
使用到的类:

android.support.v4.graphics.drawable.RoundedBitmapDrawable;

android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;

对于不带图片,仅仅只是包含字母或者文字的圆形头像,联系人里也提供了一个
com.android.contacts.common.lettertiles.LetterTileDrawable

示例代码:

Bitmap
src =BitmapFactory.decodeResource(getResources(),R.drawable.temp_pic);

RoundedBitmapDrawable
roundedBitmapDrawable =RoundedBitmapDrawableFactory.create(context.getResources(),src);

//设置圆角半径

roundedBitmapDrawable.setCornerRadius(Math.max(src.getWidth(),src.getHeight())/2.0f);

imageView.setImageDrawable(roundedBitmapDrawable);

一、Drawable to Bitmap

1.

Drawable d
= xxx;
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();

2.

public static
Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) {

Bitmap bitmap = Bitmap

.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

// canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

二、Bitmap to Drawable

Bitmap bm =
xxx;
BitmapDrawable bd = new BitmapDrawable(bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

caed
相关文章:

http://www.csdn123.com/html/topnews201408/63/8763.htm

http://www.pocketdigi.com/20141101/1389.html

http://blog.sina.com.cn/s/blog_4607d856010125sl.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: