如何在对视图执行缩放后获取以像素为单位的实际高度和宽度
How to get the Actual height and width in pixels after perform scaling on a View
我有一个 seekbar
和一个 ImageView
。在移动搜索栏时,我会相应地缩放 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"/>
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) {
}
});
}
}
如您所见,我只是缩放 Imageview 并打印其高度。
问题
这里的高度总是我得到一个常数值96。是否有可能在缩放后得到以像素为单位的实际高度?我需要的是相对于图像视图父级的实际占用高度。
在我的示例中,它是一个 300dp 高度的 FrameLayout。其中,缩放后我的 ImageView 占用了多少?
问题是,当您使用 scaleX
和 scaleY
为 View
设置动画时,您并没有更改层次结构中的 View
s 维度,而只是屏幕上渲染的尺寸。 View.getWidth()
和 View.getHeight()
return View
在父 View
中的布局大小,而 scaleX
和 scaleY
只影响渲染。
为了得到"actual"的渲染尺寸,需要将View的宽高乘以进度
float renderedWidth = view.getWidth() * progress;
float renderedHeight = view.getHeight() * progress;
此外,用 0
持续时间调用 animate()
有什么意义?可以直接使用View.setScaleX(float)
和View.setScaleY(float)
。
如果您想更好地理解布局和呈现的布局之间的区别 size/coordinates,我建议您 运行 这个布局:
<?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>
如您所见,黑色方块位于红色方块的正下方。另一方面,蓝色方块与红色方块重叠,即使它具有相同的属性 android:layout_below="@+id/redSquare"
。发生这种情况是因为 scaleX
和 scaleY
没有改变蓝色方块的位置,所以蓝色方块在布局中位于红色方块下方,但是当重新渲染时,它的大小按 2
因此与红色方块重叠
我有一个 seekbar
和一个 ImageView
。在移动搜索栏时,我会相应地缩放 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"/>
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) {
}
});
}
}
如您所见,我只是缩放 Imageview 并打印其高度。
问题
这里的高度总是我得到一个常数值96。是否有可能在缩放后得到以像素为单位的实际高度?我需要的是相对于图像视图父级的实际占用高度。 在我的示例中,它是一个 300dp 高度的 FrameLayout。其中,缩放后我的 ImageView 占用了多少?
问题是,当您使用 scaleX
和 scaleY
为 View
设置动画时,您并没有更改层次结构中的 View
s 维度,而只是屏幕上渲染的尺寸。 View.getWidth()
和 View.getHeight()
return View
在父 View
中的布局大小,而 scaleX
和 scaleY
只影响渲染。
为了得到"actual"的渲染尺寸,需要将View的宽高乘以进度
float renderedWidth = view.getWidth() * progress;
float renderedHeight = view.getHeight() * progress;
此外,用 0
持续时间调用 animate()
有什么意义?可以直接使用View.setScaleX(float)
和View.setScaleY(float)
。
如果您想更好地理解布局和呈现的布局之间的区别 size/coordinates,我建议您 运行 这个布局:
<?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>
如您所见,黑色方块位于红色方块的正下方。另一方面,蓝色方块与红色方块重叠,即使它具有相同的属性 android:layout_below="@+id/redSquare"
。发生这种情况是因为 scaleX
和 scaleY
没有改变蓝色方块的位置,所以蓝色方块在布局中位于红色方块下方,但是当重新渲染时,它的大小按 2
因此与红色方块重叠