Android实现圆形图片或者圆角图片

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

Android圆形图片或者圆角图片的快速实现,具体内容如下

话不多说直接上code

xml文件布局

<LinearLayout
 android:id="@+id/ll_headpict"
 android:layout_width="match_parent"
 android:layout_height="97dp"
 android:layout_margin="13dp"
 android:background="@drawable/shape_white_radius10_solid"
 android:gravity="center_vertical"
 android:orientation="horizontal"
 android:paddingLeft="25dp">

 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="头像"
 android:textColor="@color/color4A4A4A"
 android:textSize="14sp"
 android:textStyle="bold" />

 <ImageView
 android:id="@+id/iv_headpict"
 android:layout_width="60dp"
 android:layout_height="60dp"
 android:layout_marginRight="37dp"
 android:scaleType="fitXY"
 android:src="@mipmap/ic_headview_demo" />
</LinearLayout>

初始化控件之后用工具类加载
//第一个参数上下文,第二个控件名称,第三个图片url地址,第四个参数圆角大小
ViewUtils.loadImageRadius(this, mIvpict, stringUrl, 15);//头像

ViewUtils.java工具类

/**
 * Created by wjw on 2016/11/28
 * 倒圆角工具类
 */

public class ViewUtils {

 /**
 * 图片加载
 * @param context
 * @param iv
 * @param url
 */
 public static void loadImage(Context context, ImageView iv, String url) {
 if(null ==context || null==iv){
 return;
 }
 if(Utils.isTxtEmpty(url)){
 try {
 Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);
 }catch (Exception e){

 }
 }else {
 try {
 Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.placeholder_icon).into(iv);
 } catch (Exception e) {
 }
 }
 }


 public static void loadImage(Context context, ImageView iv, int id) {
 if(null ==context || null==iv){
 return;
 }
 try {
 Glide.with(context).load(id) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.placeholder_icon).into(iv);
 }catch (Exception e){
 }
 }


 /**
 * 本地图片
 * @param context
 * @param iv
 * @param id
 * @param radius
 */
 public static void loadImage(Context context, ImageView iv, int id,int radius) {
 if(null ==context || null==iv){
 return;
 }
 try {
 Glide.with(context).load(id) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
  transform(new GlideRoundTransform(context, radius)).into(iv);
 }catch (Exception e){

 }

 }

 public static void setImageResource(ImageView iv, int id) {
 if(null!=iv){
 iv.setImageResource(id);
 }
 }


 /**
 * 加载网络图片(带圆角)
 * @param context
 * @param iv
 * @param url
 * @param radius
 */
 public static void loadImageRadius(Context context, ImageView iv, String url, int radius) {
 if(null ==context || null==iv){
 return;
 }
 if(Utils.isTxtEmpty(url)){
 try {
 Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
  transform(new GlideRoundTransform(context, radius)).into(iv);
 }catch (Exception e){
 }
 }else{
 try {
 Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
  transform(new GlideRoundTransform(context, radius)).placeholder(R.mipmap.placeholder_icon).into(iv);
 }catch (Exception e){

 }
 }

 }

 /**
 * 加载网络图片(圆形)
 * @param context
 * @param iv
 * @param url
 */
 public static void loadImageCircle(Context context, ImageView iv, String url) {
 if(null ==context || null==iv){
 return;
 }
 if (Utils.isTxtEmpty(url)) {
 try {
 Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
  transform(new GlideCircleTransform(context)).into(iv);
 }catch (Exception e){
 }
 } else {
 try {
 Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).transform(new GlideCircleTransform(context)).
  placeholder(R.mipmap.placeholder_icon).into(iv);
 }catch (Exception e){
 }
 }
 }
 }

效果如图圆角图片

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

相关文章

  • ExpandableListView实现手风琴效果

    ExpandableListView实现手风琴效果

    这篇文章主要为大家详细介绍了ExpandableListView实现手风琴效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android开发之在程序中时时获取logcat日志信息的方法(附demo源码下载)

    Android开发之在程序中时时获取logcat日志信息的方法(附demo源码下载)

    这篇文章主要介绍了Android开发之在程序中时时获取logcat日志信息的方法,结合实例形式较为详细的分析了实时获取logcat日志的原理、步骤与相关实现技巧,并附带相应的demo源码供读者下载参考,需要的朋友可以参考下
    2016-02-02
  • Android自定义View实现五子棋小游戏

    Android自定义View实现五子棋小游戏

    这篇文章主要为大家详细介绍了Android自定义View实现五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android Lock锁实现原理详细分析

    Android Lock锁实现原理详细分析

    这篇文章主要介绍了Android Lock锁实现原理,Lock接口的实现类提供了比使用synchronized关键字更加灵活和广泛的锁定对象操作,而且是以面向对象的方式进行对象加锁
    2023-02-02
  • Android 8.0 慢充和快充提示语的实现原理

    Android 8.0 慢充和快充提示语的实现原理

    这篇文章主要介绍了Android 8.0 慢充和快充提示语的实现原理,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-05-05
  • android监听安装和卸载示例

    android监听安装和卸载示例

    Android应用程序的安装和卸载事件,是由系统进行监听并全局广播的,支持1.5(android 3)以上,因此,如果想要监听获取应用的安装和卸载事件,只需要自定义一个BroadcastReceiver,来对系统广播进行监听和处理
    2014-02-02
  • Android Vibrator调节震动代码实例

    Android Vibrator调节震动代码实例

    这篇文章主要介绍了Android Vibrator调节震动代码实例,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下
    2015-05-05
  • Android实现沉浸式状态栏

    Android实现沉浸式状态栏

    这篇文章主要为大家详细介绍了Android沉浸式状态栏的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 五分了解Android Progress Bar进度条加载

    五分了解Android Progress Bar进度条加载

    这篇文章主要为大家介绍了Android Progress Bar进度条加载的实现及属性示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android ExpandableListView使用方法案例详解

    Android ExpandableListView使用方法案例详解

    这篇文章主要介绍了Android ExpandableListView使用方法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08

最新评论