Pon*_*lar 7 android android-listview android-xml android-imageview
我在网站上搜索了更多,并得到了如下的许多建议
使用自定义样式更改背景以设置角半径和填充(将图像设置为矩形,将背景设置为圆角)
通过传递此位图和宽度(它需要更多时间加载)来解码图像和裁剪功能,从而将给定图像更改为位图
我查过了WhatsApp Chat history list and it has rounded corner images but its loaded with minimum time.
你能否建议我用WhatsApp创建带有圆形图像的listview模板的最佳方法?
我希望通过在xml页面中设置样式详细信息来更改带圆角的图像.(图像宽度和高度是60 dp,我的listview也有大约60项)
参考文献:
RES/mylayout.xml:
<ImageView
android:id="@+id/contactssnap"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/contactssnap"
android:background="@drawable/roundimage" />
Run Code Online (Sandbox Code Playgroud)
RES /绘制/ roundimage.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
<stroke
android:width="2dp"
android:color="#90a4ae" />
<size
android:height="50dp"
android:width="50dp" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<corners android:radius="100dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
注意:支持的Android版本从14到22
Gab*_*tti 21
材料组件库提供了新的ShapeableImageView.
只需使用如下布局:
<com.google.android.material.imageview.ShapeableImageView
...
app:shapeAppearanceOverlay="@style/roundedCorners"
app:srcCompat="@drawable/...." />
Run Code Online (Sandbox Code Playgroud)
使用该shapeAppearanceOverlay属性,您可以应用自定义形状,在本例中为圆角:
<style name="roundedCorners" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">xxdp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
*注意:它至少需要版本1.2.0-alpha03.
Cuo*_* Vo 11
sats的答案是有效的.但是RoundedBitmapDrawable似乎无法应用于scaleType为centerCrop的ImageView.
更新.
如果您在21级使用Android API.我们在这里有一个简单的解决方案.
1,创建drawable作为imageview的背景
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@color/colorWhite" />
</shape>
Run Code Online (Sandbox Code Playgroud)
接下来,将ImageView的属性setClipToOutline更改为true.并将roundled drawable设置为ImageView的背景.
而已.
在我的演示中,我使用此方法进行圆形imageview.可以在cuberto的对话框中找到它
将imageView放入CardView并为CardView设置CornerRadius
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/imgTourismHr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
现在设置带有圆角边框的圆角矩形图像。
您可以使用app:cardCornerRadius="30dp"、app:cardUseCompatPadding="true"、app:cardElevation="0dp"在 CardView 中简单地声明 CardView 。
请参阅下面的 XML 代码
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:cardCornerRadius="30dp"
app:cardUseCompatPadding="true"
app:cardElevation="0dp"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/temp"
android:scaleType="fitXY"
android:layout_gravity="center"
/>
</androidx.cardview.widget.CardView>
</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
您可以使用Google提供的标准API创建圆形ImageView。
ImageView imageView = (ImageView) findViewById(R.id.circleImageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);
imageView.setImageDrawable(roundedBitmapDrawable);
Run Code Online (Sandbox Code Playgroud)
另请参阅https://www.youtube.com/watch?v=zVC-YAnDlgk
| 归档时间: |
|
| 查看次数: |
24372 次 |
| 最近记录: |