Android实现圆角图片

 更新时间:2021年01月26日 12:01:24   作者:tracydragonlxy  
这篇文章主要为大家详细介绍了Android实现圆角图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现圆角图片的具体代码,供大家参考,具体内容如下

效果图:

快速开始

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <ImageView
    android:id="@+id/iv_img"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:layout_marginTop="30dp"
    android:src="@mipmap/image_bg"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

  <ImageView
    android:id="@+id/iv_rect_img"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:layout_marginTop="30dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/iv_img"/>

  <ImageView
    android:id="@+id/iv_circle_img"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="30dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/iv_rect_img"/>

</android.support.constraint.ConstraintLayout>

MainActivity.class文件:

public class MainActivity extends AppCompatActivity {

  private ImageView ivRectImg, ivCircleImg;

  private Bitmap bitmap;
  private int width;
  private int height;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ivRectImg = findViewById(R.id.iv_rect_img);
    ivCircleImg = findViewById(R.id.iv_circle_img);

    bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image_bg);
    width = bitmap.getWidth();
    height = bitmap.getHeight();

    rectRoundBitmap();
    circleBitmap();
  }

 // 圆角矩形
  private void rectRoundBitmap() {
    RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
    bitmapDrawable.setAntiAlias(true);
    bitmapDrawable.setCornerRadius(50);
    ivRectImg.setImageDrawable(bitmapDrawable);
  }

  // 把bitmap图片进行剪切成正方形, 然后再设置圆角半径为正方形边长的一半即可
  private void circleBitmap() {
    Bitmap circle = null;
    int min = Math.min(width, height);
    int max = Math.max(width, height);
    if (width == height) {
      circle = Bitmap.createBitmap(bitmap, 0, 0, width, height);
    } else {
      // 居中裁剪
      if (width > height) {
        circle = Bitmap.createBitmap(bitmap, (max - min) / 2, 0, min, min);
      } else {
        circle = Bitmap.createBitmap(bitmap, 0, (max - min) / 2, min, min);
      }
    }
    RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), circle);
    bitmapDrawable.setCornerRadius(min / 2);
    bitmapDrawable.setAntiAlias(true);
    ivCircleImg.setImageDrawable(bitmapDrawable);
  }
}

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

相关文章

  • Android使用SqLite实现登录注册功能流程详解

    Android使用SqLite实现登录注册功能流程详解

    这篇文章主要介绍了使用Android Studio自带的sqlite数据库实现一个简单的登录注册功能,SQLite是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎,本文给大家介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Android自定义TabLayout效果

    Android自定义TabLayout效果

    这篇文章主要为大家详细介绍了Android自定义TabLayout效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android自定义View实现简单炫酷的球体进度球实例代码

    Android自定义View实现简单炫酷的球体进度球实例代码

    这篇文章主要给大家介绍了关于Android自定义View实现简单炫酷的球体进度球的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • Android中使用Matrix控制图形变换和制作倒影效果的方法

    Android中使用Matrix控制图形变换和制作倒影效果的方法

    这篇文章主要介绍了Android中使用Matrix控制图形变换和制作倒影效果的方法,用Matrix来作矩阵变化十分强大,文中的制作倒影的例子便是一个十分巧妙的运用,需要的朋友可以参考下
    2016-04-04
  • Android实现底部弹出的对话框功能

    Android实现底部弹出的对话框功能

    这篇文章主要介绍了Android实现底部弹出的对话框功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Android Http协议访问网络实例(3种)

    Android Http协议访问网络实例(3种)

    本篇文章主要介绍了Android Http协议访问网络实例(3种),具有一定的参考价值,有兴趣的可以了解一下
    2017-07-07
  • 一款Android APK的结构构成解析

    一款Android APK的结构构成解析

    本篇文章介绍了我在学习过程中对于Android 程序的理解总结,刨析了apk的组成与产生过程,通读本篇对大家的学习或工作具有一定的价值,需要的朋友可以参考下
    2021-10-10
  • 详解Android进程保活的方法

    详解Android进程保活的方法

    本篇文章主要介绍了Android进程保活 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Android使用手势实现翻页效果

    Android使用手势实现翻页效果

    这篇文章主要介绍了Android使用手势实现翻页效果,本程序使用了一个ViewFlipper组件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Android自定义转盘菜单效果

    Android自定义转盘菜单效果

    这篇文章主要为大家详细介绍了Android自定义转盘菜单效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07

最新评论