Java 如何设置图像大小为正方形。。所有手机屏幕都支持哪种功能?

Java 如何设置图像大小为正方形。。所有手机屏幕都支持哪种功能?,java,android,android-layout,user-interface,Java,Android,Android Layout,User Interface,项目布局 <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/top_panel" android:background="@drawable/transback" app:cardCor

项目布局

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_panel"
        android:background="@drawable/transback"
        app:cardCornerRadius="10dp"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp">

    <ImageView
        android:id="@+id/categoryImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

    </android.support.v7.widget.CardView>

我想将图像大小设置为正方形 屏幕宽度=图像高度

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_panel"
        android:background="@drawable/transback"
        app:cardCornerRadius="10dp"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp">

    <ImageView
        android:id="@+id/categoryImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

    </android.support.v7.widget.CardView>
这是我的项目,我用回收查看


如何可能?

使用约束布局 应用:布局约束尺寸比=“1:1”

  • 阅读

  • 您可以将图像纵横比设置为1:1,以获得如下方形图像:

       <ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@tools:sample/avatars[1]" />
    
    
    
    如果您希望您的屏幕能够响应您可以使用的所有屏幕大小,并将屏幕的某些百分比设置为如下所示:

      <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline3"
            tools:src="@tools:sample/avatars[1]" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.3" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.6" />
    
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    您可以自定义
    ImageView
    来创建
    SquareImageView

    public class SquareImageView  extends ImageView {
    
      public SquareImageView(Context context) {
        super(context);
    }
    
    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
      }
    
    }
    

    在Android Mani fest上添加这些行

    <supports-screens
        android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true"
        />
    
    
    
    这就是我的观点

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
    
    
    

    您可以使用此方形图像视图

    自定义图像视图:

    XML

    
    
    您只是告诉他如何制作图像的形状-这不会对所有屏幕做出响应。如果在相对布局中可能的话?这是可能的,请检查,但最好使用constraintLayout
     <yourpackagename.SquareImageView
                    android:id="@+id/item_iv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop" />