Android如何设置圆角图片

所属分类: 软件编程 / Android 阅读数: 59
收藏 0 赞 0 分享

在开发过程中有时需要将图片显示成圆角图片,一般我们可以通过在xml中设置drawable shape即可,但今天我给出另一种方法,用java代码动态去设置圆角,顺便做个简单的笔记。

主要原理是使用系统自带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);
  //创建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>

如有问题,欢迎指正,谢谢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android 实现圆角图片的简单实例

这篇文章主要介绍了Android 实现圆角图片的简单实例的相关资料,Android 圆角图片的实现形式,包括用第三方、也有系统,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现底部半透明弹出框PopUpWindow效果

这篇文章主要为大家详细介绍了Android实现底部半透明弹出框PopUpWindow效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android二级缓存加载图片实现照片墙功能

这篇文章主要为大家详细介绍了Android二级缓存加载图片实现照片墙功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿Iphone屏幕底部弹出半透明PopupWindow效果

这篇文章主要为大家详细介绍了Android仿Iphone屏幕底部弹出半透明PopupWindow效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android编程实现的微信支付功能详解【附Demo源码下载】

这篇文章主要介绍了Android编程实现的微信支付功能,结合实例形式详细分析了Android微信支付功能的实现步骤与具体操作技巧,并附带了Demo源码供读者下载参考,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之APP安装后在桌面上不显示应用图标的解决方法

这篇文章主要介绍了Android开发之APP安装后在桌面上不显示应用图标的解决方法,涉及Android activity相关属性设置技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android RecyclerView实现数据列表展示效果

这篇文章主要为大家详细介绍了Android RecyclerView实现数据列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android Uri和文件路径互相转换的实例代码

在项目中需要用到将Uri转换为绝对路径,下面小编把Android Uri和文件路径互相转换的实例代码分享到脚本之家平台,需要的的朋友参考下吧
收藏 0 赞 0 分享

Android直播app送礼物连击动画效果(实例代码)

最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件,下面给大家分享实例代码,需要的的朋友参考下吧
收藏 0 赞 0 分享

Android文件选择器ExFilePicker的使用方法

这篇文章主要为大家详细介绍了Android文件选择器ExFilePicker的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多