如何在程序中以编程方式设置ImageView的圆角?

huangapple go评论84阅读模式
英文:

How to set ImageView rounded corners programmatically?

问题

我是Android Studio的新手。我想问一下,我是否可以通过编程方式创建具有圆角的ImageView?因为我想使用它来从数据库加载数据。所以,用户的个人资料图片将显示为圆角。

我的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F3FFFC">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/imgView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/profilepicture"
                />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

谢谢您抽出时间阅读我的问题,希望我能解决这个问题:D

英文:

i'm new in Android Studio. I want to ask, can i make ImageView with round corners programmatically? because i want use it to load data from database. So, user profile picture will be appear with rounded corner

如何在程序中以编程方式设置ImageView的圆角?

my XML is below :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:background=&quot;#F3FFFC&quot;&gt;
    &lt;ScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;
        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;&gt;
            &lt;ImageView
                android:id=&quot;@+id/imgView&quot;
                android:layout_width=&quot;100dp&quot;
                android:layout_height=&quot;100dp&quot;
                android:src=&quot;@drawable/profilepicture&quot;
                /&gt;
        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;
&lt;/LinearLayout&gt;

Thank you for your time to read my question, hope i can solve this problem 如何在程序中以编程方式设置ImageView的圆角?

答案1

得分: 0

<androidx.cardview.widget.CardView
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:cardCornerRadius="20dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher_background" />

</androidx.cardview.widget.CardView>

使用以下依赖项:implementation 'androidx.cardview:cardview:1.0.0'

英文:
&lt;androidx.cardview.widget.CardView
    android:layout_width=&quot;150dp&quot;
    android:layout_height=&quot;150dp&quot;
    app:cardCornerRadius=&quot;20dp&quot;&gt;

    &lt;ImageView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:src=&quot;@drawable/ic_launcher_background&quot; /&gt;

&lt;/androidx.cardview.widget.CardView&gt;

by using this dependency implementation &#39;androidx.cardview:cardview:1.0.0&#39;

huangapple
  • 本文由 发表于 2020年7月27日 18:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63113612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定