android studio imageview有时不停留在framelayout中

js4nwp54  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(348)

我有一个框架布局,大约是60%的屏幕大小。在framelayout中,我有一个imageview,它每秒随机改变位置。
除了这个问题外,一切都运行得很好:imageview有时会消失,因为它会将其位置更改为超过屏幕大小的某个位置。
有没有什么方法可以让imageview在更改坐标时保持在这个精确的framelayout中,这样它就不会出现在framelayout之外的某个地方?
职位变动代码:

  1. private void mueckePopUp() {
  2. final DisplayMetrics displaymetrics = new DisplayMetrics();
  3. getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
  4. final Timer timer = new Timer();
  5. timer.schedule(new TimerTask() {
  6. @Override
  7. public void run() {
  8. runOnUiThread(new Runnable() {
  9. @Override
  10. public void run() {
  11. Random R = new Random();
  12. final float dx = R.nextFloat() * displaymetrics.widthPixels;
  13. final float dy = R.nextFloat() * displaymetrics.heightPixels;
  14. iv_muecke.animate()
  15. .x(dx)
  16. .y(dy)
  17. .setDuration(0)
  18. .start();
  19. }
  20. });
  21. }
  22. }, 0, 1000);
  23. }

framelayout和imageview的xml代码:

  1. <FrameLayout
  2. android:layout_width="0dp"
  3. android:layout_height="430dp"
  4. tools:ignore="MissingConstraints"
  5. app:layout_constraintStart_toStartOf="parent"
  6. app:layout_constraintEnd_toEndOf="parent"
  7. app:layout_constraintTop_toBottomOf="@+id/tv_points"
  8. android:layout_marginTop="8dp"
  9. android:id="@+id/framelayout"
  10. app:layout_constraintHorizontal_bias="0.0">
  11. <ImageView
  12. android:id="@+id/iv_muecke"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:maxHeight="100dp"
  16. android:minHeight="100dp"
  17. android:maxWidth="125dp"
  18. android:minWidth="125dp"
  19. android:adjustViewBounds="true"
  20. app:srcCompat="@drawable/muecke"
  21. android:contentDescription="@string/todo"/>
  22. </FrameLayout>
8nuwlpux

8nuwlpux1#

你必须得到框架布局的高度和宽度,而不是屏幕

  1. //get image size
  2. ViewTreeObserver vti = iv_muecke.getViewTreeObserver();
  3. vti.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  4. @Override
  5. public void onGlobalLayout() {
  6. iv_muecke.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  7. imWidth = iv_muecke.getMeasuredWidth();
  8. imHeight = iv_muecke.getMeasuredHeight();
  9. }
  10. });
  11. //get FrameLayout size
  12. ViewTreeObserver vto = layout.getViewTreeObserver();
  13. vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  14. @Override
  15. public void onGlobalLayout() {
  16. layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  17. width = layout.getMeasuredWidth();
  18. height = layout.getMeasuredHeight();
  19. }
  20. });

然后您必须检查图像是否超出framelayout。设置在framelayout的边缘

  1. timer.schedule(new TimerTask() {
  2. @Override
  3. public void run() {
  4. runOnUiThread(new Runnable() {
  5. @Override
  6. public void run() {
  7. Random R = new Random();
  8. float dx = R.nextFloat() * width;
  9. float dy = R.nextFloat() * height;
  10. if (dx > (width - imWidth))
  11. dx = width - imWidth;
  12. if (dy > (height - imHeight))
  13. dy = height - imHeight;
  14. iv_muecke.animate()
  15. .x(dx)
  16. .y(dy)
  17. .setDuration(0)
  18. .start();
  19. }
  20. });
  21. }
  22. }, 0, 1000);
展开查看全部

相关问题