如何在视图上执行缩放后获取实际的高度和宽度(以像素为单位)

doe*_*doe 4 android android-view

我有一个seekbar和 一个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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.app.pdf.pdfmetadata.Main2Activity">

<FrameLayout
    android:background="#20000000"
    android:layout_width="match_parent"
    android:layout_height="300dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@android:drawable/ic_dialog_info"/> 
</FrameLayout>

<SeekBar
    android:id="@+id/seekbar"
    android:min="1"
    android:max="5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

Java代码

public class MainActivity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final View imageView = findViewById(R.id.imageView);
    SeekBar seekBar = findViewById(R.id.seekbar);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            imageView.animate().scaleX(progress).scaleY(progress).setDuration(0).start();
            int height = imageView.getHeight();
            Log.d(TAG, "onProgressChanged: "+height);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

}

如您所见,我只是缩放 Imageview 并打印其高度。

问题

这里的高度总是我得到一个常数值 96。缩放后是否有可能获得以像素为单位的实际高度?我需要的是相对于图像视图父级占用的实际高度。在我的示例中,它的 FrameLayout 高度为 300dp。其中,缩放后我的 ImageView 占用了多少,我怎样才能得到?

lel*_*man 5

问题是,当您使用 和 为 a 制作动画时ViewscaleXscaleY不会更改View层次结构中的 s 尺寸,而只会更改屏幕上渲染的尺寸。View.getWidth()View.getHeight()返回 的大小,因为它在父级View中布局,而仅影响渲染。ViewscaleXscaleY

为了获得“实际”渲染大小,您需要将视图的宽度和高度乘以进度

float renderedWidth = view.getWidth() * progress;
float renderedHeight = view.getHeight() * progress;
Run Code Online (Sandbox Code Playgroud)

animate()另外,调用持续时间有什么意义0?可以直接使用View.setScaleX(float)and 。View.setScaleY(float)

如果您想更好地了解布局和渲染的大小/坐标之间的区别,我建议您运行此布局:

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

    <View
        android:id="@+id/redSquare"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerHorizontal="true"
        android:background="#ffff0000" />


    <View
        android:id="@+id/blackSquare"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_below="@+id/redSquare"
        android:layout_centerHorizontal="true"
        android:background="#ff000000" />

    <View
        android:id="@+id/blueSquare"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_below="@+id/redSquare"
        android:layout_centerHorizontal="true"
        android:scaleX="2"
        android:scaleY="2"
        android:background="#550000ff" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,黑色方块位于红色方块的正下方。另一方面,蓝色方块与红色方块重叠,即使它具有相同的属性android:layout_below="@+id/redSquare"。发生这种情况是因为scaleXscaleY没有改变蓝色方块的位置,因此蓝色方块在布局中位于红色方块下方,但在重新渲染时,其大小按 的一个因子缩放2,因此与红色方块重叠