最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android如何設置圓角圖片

 更新時間:2021年04月22日 14:42:13   作者:小花貓cat  
這篇文章主要為大家詳細介紹了Android如何設置圓角圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

主要原理是使用系統(tǒng)自帶api:

RoundedBitmapDrawableFactory

先上效果圖:

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

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對象
  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>

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

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

相關文章

最新評論

隆昌县| 凌源市| 兰溪市| 合水县| 左权县| 禹州市| 彩票| 江油市| 息烽县| 宁乡县| 莎车县| 林周县| 湖北省| 岳阳市| 张家港市| 溆浦县| 樟树市| 宜黄县| 平原县| 临湘市| 泰和县| 卢龙县| 奈曼旗| 河源市| 平定县| 威信县| 来安县| 华亭县| 江阴市| 南部县| 手游| 天长市| 灵台县| 河间市| 汨罗市| 观塘区| 长海县| 河南省| 安乡县| 洪雅县| 扶绥县|