Android生成帶圓角的Bitmap圖片
更新時間:2015年07月14日 16:49:47 作者:鑒客
這篇文章主要介紹了Android生成帶圓角的Bitmap圖片,涉及Android通過Canvas實現(xiàn)繪制帶圓角的圖片相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android生成帶圓角的Bitmap圖片。分享給大家供大家參考。具體如下:
有時候我們在開發(fā)Android應(yīng)用時,會遇到圓角圖片的問題,那么,我們?nèi)绾卧贏ndroid中用代碼來生成圓角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程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
究其為啥需要多線程的本質(zhì)就是異步處理,直觀一點說就是不要讓用戶感覺到“很卡”為了提高用戶體驗?zāi)鞘潜仨氁褂玫?/div> 2013-06-06
Android自定義RecyclerView Item頭部懸浮吸頂
這篇文章主要為大家詳細(xì)介紹了Android自定義RecyclerView Item頭部懸浮吸頂,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Android中RecycleView與ViewPager沖突的解決方法及原理
這篇文章主要給大家介紹了關(guān)于Android中RecycleView與ViewPager沖突的解決方法及原理的相關(guān)資料,以及ViewPager嵌套RecycleView卡頓問題的處理方法,文中通過示例代碼介紹的非常狎昵,需要的朋友可以參考下2018-07-07最新評論

