Android如何設(shè)置圓角圖片
在開發(fā)過程中有時(shí)需要將圖片顯示成圓角圖片,一般我們可以通過在xml中設(shè)置drawable shape即可,但今天我給出另一種方法,用java代碼動(dòng)態(tài)去設(shè)置圓角,順便做個(gè)簡(jiǎn)單的筆記。
主要原理是使用系統(tǒng)自帶api:
RoundedBitmapDrawableFactory
先上效果圖:
由于比較簡(jiǎn)單,直接給出實(shí)現(xiàn)方式:
public class MainActivity extends AppCompatActivity { private ImageView mImgRectRound; private ImageView mImgRound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImgRectRound = (ImageView) findViewById(R.id.img_rect_rounded); mImgRound = (ImageView) findViewById(R.id.img_rounded); rectRoundBitmap(); roundBitmap(); } private void rectRoundBitmap(){ //得到資源文件的BitMap Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.dog); //創(chuàng)建RoundedBitmapDrawable對(duì)象 RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image); //抗鋸齒 roundImg.setAntiAlias(true); //設(shè)置圓角半徑 roundImg.setCornerRadius(30); //設(shè)置顯示圖片 mImgRectRound.setImageDrawable(roundImg); } private void roundBitmap(){ //如果是圓的時(shí)候,我們應(yīng)該把bitmap圖片進(jìn)行剪切成正方形, 然后再設(shè)置圓角半徑為正方形邊長(zhǎng)的一半即可 Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dog); Bitmap bitmap = null; //將長(zhǎng)方形圖片裁剪成正方形圖片 if (image.getWidth() == image.getHeight()) { bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight()); } else { bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth()); } RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); //圓角半徑為正方形邊長(zhǎng)的一半 roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2); //抗鋸齒 roundedBitmapDrawable.setAntiAlias(true); mImgRound.setImageDrawable(roundedBitmapDrawable); } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.cjl.roundedbitmap.MainActivity"> <ImageView android:id="@+id/img_rect_rounded" android:layout_width="200dp" android:layout_height="300dp" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal"/> <ImageView android:id="@+id/img_rounded" android:layout_marginTop="20dp" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center_horizontal"/> </LinearLayout>
如有問題,歡迎指正,謝謝。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android判斷應(yīng)用程序退到后臺(tái)的方法(示例代碼)
判斷手機(jī)是否退到后臺(tái),這是我們?cè)贏ndroid開發(fā)中實(shí)現(xiàn)一些功能時(shí),經(jīng)常會(huì)考慮的問題,這篇文章主要介紹了android判斷應(yīng)用程序退到后臺(tái)的方法,需要的朋友可以參考下2023-03-03Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè)
這篇文章主要為大家詳細(xì)介紹了使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法,實(shí)例分析了Android針對(duì)HttpPost類的操作技巧,需要的朋友可以參考下2015-12-12android實(shí)現(xiàn)百度地圖自定義彈出窗口功能
這篇文章主要介紹了android實(shí)現(xiàn)百度地圖自定義彈出窗口的功能,大家參考使用吧2013-11-11Android編程實(shí)現(xiàn)下載時(shí)主界面與詳細(xì)界面一致更新的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)下載時(shí)主界面與詳細(xì)界面一致更新的方法,涉及Android事件監(jiān)聽及界面動(dòng)態(tài)更新相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android實(shí)現(xiàn)水波紋效果實(shí)例代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)水波紋效果實(shí)例代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Android使用setCustomTitle()方法自定義對(duì)話框標(biāo)題
Android有自帶的對(duì)話框標(biāo)題,但是不太美觀,如果要給彈出的對(duì)話框設(shè)置一個(gè)自定義的標(biāo)題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來通過本文給大家介紹Android使用setCustomTitle()方法自定義對(duì)話框標(biāo)題,感興趣的朋友一起學(xué)習(xí)吧2016-02-02