Android生成帶圓角的Bitmap圖片
更新時間:2015年07月14日 16:49:47 作者:鑒客
這篇文章主要介紹了Android生成帶圓角的Bitmap圖片,涉及Android通過Canvas實現(xiàn)繪制帶圓角的圖片相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android生成帶圓角的Bitmap圖片。分享給大家供大家參考。具體如下:
有時候我們在開發(fā)Android應用時,會遇到圓角圖片的問題,那么,我們如何在Android中用代碼來生成圓角Bitmap圖片呢?下面這段代碼也許能夠幫到你。
該方法主要用到了drawRoundRect來畫圓角矩形,然后通過drawBitmap來畫圖片。
//生成圓角圖片
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight()));
final float roundPx = 14;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.BLACK);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, src, rect, paint);
return output;
} catch (Exception e) {
return bitmap;
}
}
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android自定義RecyclerView Item頭部懸浮吸頂
這篇文章主要為大家詳細介紹了Android自定義RecyclerView Item頭部懸浮吸頂,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Android中RecycleView與ViewPager沖突的解決方法及原理
這篇文章主要給大家介紹了關于Android中RecycleView與ViewPager沖突的解決方法及原理的相關資料,以及ViewPager嵌套RecycleView卡頓問題的處理方法,文中通過示例代碼介紹的非常狎昵,需要的朋友可以參考下2018-07-07

