Android如何設(shè)置圓角圖片
在開(kāi)發(fā)過(guò)程中有時(shí)需要將圖片顯示成圓角圖片,一般我們可以通過(guò)在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>
如有問(wèn)題,歡迎指正,謝謝。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實(shí)現(xiàn)文件瀏覽功能的方法【類似于FileDialog的功能】
這篇文章主要介紹了Android編程實(shí)現(xiàn)文件瀏覽功能的方法,可實(shí)現(xiàn)類似于FileDialog的功能,涉及Android針對(duì)文件與目錄操作的相關(guān)技巧,需要的朋友可以參考下2016-11-11
Android 在 res/layout 文件夾 下創(chuàng)建一個(gè) 子文件夾實(shí)例
這篇文章主要介紹了Android 在 res/layout 文件夾 下創(chuàng)建一個(gè) 子文件夾實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android超詳細(xì)講解彈出多選框的實(shí)現(xiàn)
這篇文章主要介紹了在Android開(kāi)發(fā)中如何實(shí)現(xiàn)彈出多選框的功能,多選框是很常見(jiàn)的操作控件,感興趣的朋友都來(lái)一起看看吧2022-03-03
Android左滑返回功能的實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Android左滑返回的實(shí)現(xiàn)示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
基于Android設(shè)計(jì)模式之--SDK源碼之策略模式的詳解
本篇文章介紹了,基于Android設(shè)計(jì)模式之--SDK源碼之策略模式的詳解。需要的朋友參考下2013-04-04
Android自定義view實(shí)現(xiàn)左滑刪除的RecyclerView詳解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來(lái)講解RecyclerView的用法2022-11-11

