如何通过 Screen Scale 调整大小和重新定位 'ImageView' 保持其比例

How to Resize and Reposition 'ImageView' by Screen Scale remain its ratio

文章底部有MainActivity.java和activity_main.xml的文本代码

我打算将具有相同比例(16:9)的图像设置为ImageView, 但是我遇到了一个小问题。

我要设置我的ImageView保持16:9的比例,其中的图片被交(1920*1080)

if I use wrap_content, it remains ratio of the given picture, But I can't resize the size of ImageView.

.

if I use fixed numbers that remain 16:9 ratio like 400dp * 225dp
I can resize the size I want, and modify the position by Screen scale by using app:layout_constraintHorizontal_bias= and app:layout_constraintVertical_bias=

But in other Device, the Size of ImageView is too small or too big for its Screen Space. because the value is fixed

所以,我想通过设备的屏幕比例设置 ImageView 大小和位置,同时保持其比例(16:9) 我该怎么做?

我看了很多问题,但我找到了 "to match the imageView ratio to the picture" 而不是 "to set the size of ImageView relative to the size of the screen" 的方法,因为我的搜索能力很差。

有没有什么方法可以表达如下? 这不是真正的代码,它只是伪代码,可以轻松解释我真正想做的事情。

android:layout_width="wrapcontent * 0.8"
android:layout_height="wrapcontent * 0.8"  //<!-- same as "layout_width * 9 / 16 " because ratio of give picture will be always 16 : 9 -->

app:layout_constraintHorizontal_bias=" ((Screen_width - ImageView_width) / 2) / 100 " //<!-- pseudo percentage that to stay in Screen's center -->
app:layout_constraintVertical_bias= "0.3" //<!-- pseudo percentage that to stay in Screen's custom upper part-->

对于您解释中的混乱,我深表歉意,希望您今天过得愉快。 MainActivity.The java 和 activity_main.xml 的代码如下。

MainActivity.java

package com.example.catexample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // I Want to Resize and Reposition 'ImageView' by Screen Scale remain its ratio( 16 : 9)
        ImageView imageView = findViewById(R.id.kitti_DD);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/kitti_DD"
        android:layout_width="400dp"
        android:layout_height="225dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"

        app:layout_constraintHorizontal_bias="0.1"
        app:layout_constraintVertical_bias="0.3"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        app:srcCompat="@drawable/kittisaurus_dd" />

</androidx.constraintlayout.widget.ConstraintLayout>

kittisaurus_dd.jpg

ps.The举个例子,可爱的猫的名字是DD,它来自크집사的视频,而不是크림히어로즈。

衷心感谢您阅读长篇文章!

将宽度和高度设置为 0dp 并添加 app:layout_constraintDimensionRatio 属性并将其值设置为您想要的宽高比,例如:4:3、16:9 等