欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 布局中设置 圆形或圆角图片

程序员文章站 2023-12-24 12:20:21
一般在做个人信息界面的时候,会添加一张圆形或者圆角的图片,这里我主要是采用Fresco来添加图片的形状,这是其中一张方法,可以去官网学习一下,有很多功能,入门很简单首先在项目的build.gradle文件的dependencies中引入com.facebook.fresco:fresco:0.12.0,像下面这样 dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation...

一般在做个人信息界面的时候,会添加一张圆形或者圆角的图片,这里我主要是采用Fresco来添加图片的形状,这是其中一张方法,可以去官网学习一下,有很多功能,入门很简单

  1. 首先在项目的build.gradle文件的dependencies中引入com.facebook.fresco:fresco:0.12.0,像下面这样
 dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.facebook.fresco:fresco:0.12.0'
}
  1. 然后在想要添加图片的布局中加入如下代码
<com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/profile"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="50dp"
            fresco:placeholderImage="@drawable/touxiang"
            fresco:roundedCornerRadius="10dp"/>

前面四个属性应该好理解,就是图片的长宽和上边距,fresco:roundedCornerRadius表示设置图片的圆角弧度,我这里设置的是10dp,自己看着设置。fresco:placeholderImage设置占位图,主要是从网上下载图片时,设置一张本地占位图。
另外,直接添加上面的XML代码可能会报错,还要在XML中加入xmlns:fresco="http://schemas.android.com/apk/res-auto",这个fresco名字可以随便取,但要与上面的属性一致
另外,圆形图片设置fresco:roundAsCircle="true"即可

  1. 接下来要在对应的Activity.java里面加入如下代码
Url url = Url.parse("res://drawable/"+R.drawable.logo);
        SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.profile);
        draweeView.setImageURI(url);

这个url表示的是图片的地址,这里用的是本地的图片,在drawable/下面有一张叫logo的图片,写代码时要写成R.drawable.XXX的形式,XXX表示图片名字。也可以弄网上的图片,具体方法可以到官网上看,改一下地址就行了。
ps:如果只是用本地图片表示的话,就可以不需要第3步。
接下来就找到这个idprofileSimpleDraweeView,也就是第二步中添加的控件,把url添加进去就行了,最后运行代码,就可以得到如下效果,可以很明显的看到图片呈现圆角形式
Android 布局中设置 圆形或圆角图片

本文地址:https://blog.csdn.net/qq_43454912/article/details/108834356

上一篇:

下一篇: