国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android如何設置圓角圖片

2019-10-23 19:45:06
字體:
來源:轉載
供稿:網友

在開發過程中有時需要將圖片顯示成圓角圖片,一般我們可以通過在xml中設置drawable shape即可,但今天我給出另一種方法,用java代碼動態去設置圓角,順便做個簡單的筆記。

主要原理是使用系統自帶api:

RoundedBitmapDrawableFactory

先上效果圖:

Android,圓角圖片

由于比較簡單,直接給出實現方式:

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);  //創建RoundedBitmapDrawable對象  RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image);  //抗鋸齒  roundImg.setAntiAlias(true);  //設置圓角半徑  roundImg.setCornerRadius(30);  //設置顯示圖片  mImgRectRound.setImageDrawable(roundImg); } private void roundBitmap(){  //如果是圓的時候,我們應該把bitmap圖片進行剪切成正方形, 然后再設置圓角半徑為正方形邊長的一半即可  Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dog);  Bitmap bitmap = null;  //將長方形圖片裁剪成正方形圖片  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);  //圓角半徑為正方形邊長的一半  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>

如有問題,歡迎指正,謝謝。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林口县| 玉龙| 台北市| 达拉特旗| 绥中县| 盐山县| 赞皇县| 依兰县| 黑龙江省| 高密市| 黄大仙区| 常山县| 台安县| 崇明县| 浑源县| 随州市| 雷波县| 兴山县| 瑞金市| 谷城县| 本溪| 紫云| 双牌县| 车险| 长沙县| 靖州| 咸宁市| 连江县| 城口县| 夹江县| 黎平县| 平塘县| 云阳县| 磴口县| 宝应县| 子洲县| 东港市| 瑞金市| 漯河市| 海盐县| 牙克石市|