簡體   English   中英

如何使 ImageView 在 Android (Java) 的 GridView 中四舍五入?

[英]How to make the ImageView rounded in GridView of Android (Java)?

我試圖讓 Gridview 內部的圖像變圓,但它看起來如下所示。 請幫我解決這個問題

在此處輸入圖像描述

這是我嘗試過的代碼:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;

            if(convertView == null){
                imageView = new ImageView(mContext);
                imageView.setBackgroundResource(R.drawable.shape);
                GridLayout.LayoutParams param =new GridLayout.LayoutParams();
                param.height = 300;
                param.width = 300;
                imageView.setLayoutParams(param);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setPadding(16,16,16,16);
            }else{
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(image[position]);
            return imageView;
        }

下面是我添加的 shape.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Background Color -->
    <solid android:color="#ffffff" />

    <!-- Border Color -->
    <stroke android:width="1dp" android:color="#000000" />

    <!-- Round Corners -->
    <corners android:radius="50dp" />
</shape>

您可以使用CircleImageView庫輕松完成,您將在自述文件中找到示例,或者如果您想創建自己的版本,您可以閱讀庫代碼並從中學習以改進您自己的版本

我總是將這兩個庫用於圓形圖像視圖

    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.makeramen:roundedimageview:2.3.0'

圓形圖像視圖示例

<de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/imageProfile"
                android:layout_width="@dimen/_80sdp"
                android:layout_height="@dimen/_80sdp"
                app:civ_border_color="@color/textPrimary"
                app:civ_border_width="@dimen/_1sdp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textChangeBio" />

RoundedImageView 示例

<com.makeramen.roundedimageview.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imageView1"
        android:src="@drawable/photo1"
        android:scaleType="fitCenter"
        app:riv_corner_radius="30dip"
        app:riv_border_width="2dip"
        app:riv_border_color="#333333"
        app:riv_mutate_background="true"
        app:riv_tile_mode="repeat"
        app:riv_oval="true" />

您還可以在 RoundImageView 中設置自定義半徑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM