使用RoundedBitmapDrawable生成圓角圖片的方法

Bitmap src = BitmapFactory.decodeResource(getResources(), imageId); //獲取Bitmap圖片 RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), src); //創(chuàng)建RoundedBitmapDrawable對象 roundedBitmapDrawable.setCornerRadius(100); //設(shè)置圓角半徑(根據(jù)實(shí)際需求) roundedBitmapDrawable.setAntiAlias(true); //設(shè)置反走樣 image.setImageDrawable(roundedBitmapDrawable); //顯示圓角圖片
動態(tài)
生成圓形圖片
由于RoundedBitmapDrawable類沒有直接提供生成圓形圖片的方法,所以生成圓形圖片首先需要對原始圖片進(jìn)行裁剪,將圖片裁剪成正方形,最后再生成圓形圖片,具體實(shí)現(xiàn)如下:
Bitmap src = BitmapFactory.decodeResource(getResources(), imageId);
Bitmap dst;
//將長方形圖片裁剪成正方形圖片
if (src.getWidth() >= src.getHeight()){
dst = Bitmap.createBitmap(src, src.getWidth()/2 - src.getHeight()/2, 0, src.getHeight(), src.getHeight()
);
}else{
dst = Bitmap.createBitmap(src, 0, src.getHeight()/2 - src.getWidth()/2, src.getWidth(), src.getWidth()
);
}
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), dst);
roundedBitmapDrawable.setCornerRadius(dst.getWidth() / 2); //設(shè)置圓角半徑為正方形邊長的一半
roundedBitmapDrawable.setAntiAlias(true);
image.setImageDrawable(roundedBitmapDrawable);
以上所述是小編給大家介紹的使用RoundedBitmapDrawable生成圓角圖片的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Kotlin示例講解標(biāo)準(zhǔn)函數(shù)with與run和apply的使用
Kotlin的標(biāo)準(zhǔn)函數(shù)是指 Standard.kt 文件中定義的函數(shù),任何Kotlin代碼都可以自由地調(diào)用所有的標(biāo)準(zhǔn)函數(shù)。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-08-08
Android中隱藏狀態(tài)欄和標(biāo)題欄的方法匯總(隱藏狀態(tài)欄、標(biāo)題欄的五種方法)
這篇文章主要介紹了Android中隱藏狀態(tài)欄和標(biāo)題欄的方法匯總(隱藏狀態(tài)欄、標(biāo)題欄的五種方法),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Android開發(fā)實(shí)現(xiàn)的內(nèi)存管理工具類
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的內(nèi)存管理工具類,可實(shí)現(xiàn)計(jì)算手機(jī)內(nèi)部與外部的總存儲空間、可用存儲空間等功能,需要的朋友可以參考下2017-11-11
Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動自動播放
這篇文章主要介紹了Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動自動播放的相關(guān)資料,非常不錯,具有參考解決價值,需要的朋友可以參考下2016-08-08
Android中ListView的幾種常見的優(yōu)化方法總結(jié)
Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。2017-02-02
flutter 動手?jǐn)]一個城市選擇citypicker功能
在一些項(xiàng)目開發(fā)中經(jīng)常會用到城市選擇器功能,今天小編動手?jǐn)]一個基于flutter 城市選擇citypicker功能,具體實(shí)現(xiàn)過程跟隨小編一起看看吧2021-08-08

