我有一个“seekbar”和一个“ImageView”。在移动seekbar时,我会相应地缩放Imageview。以下是我的全部代码:
XML
问题:
在这里,我始终获得一个恒定值96。是否有可能在缩放后获取实际的像素高度?我需要相对于ImageView的父级实际占用高度。在我的示例中,它是300dp高的FrameLayout。在缩放后,我的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。是否有可能在缩放后获取实际的像素高度?我需要相对于ImageView的父级实际占用高度。在我的示例中,它是300dp高的FrameLayout。在缩放后,我的ImageView占用了多少空间,我该如何获取?
imageView.setScaleX(progress);imageView.setScaleY(progress);
吗?但结果还是一样的。 - doe