Android Realizes Floating Pictures

  • 2021-12-05 07:31:41
  • OfStack

In this paper, we share the specific code of Android to realize floating pictures for your reference. The specific contents are as follows


@SuppressLint("AppCompatCustomView")
public class MoveImageView extends ImageView {

 // Press that 1 Carved coordinates and   Control up, down, left and right distance 
 private float lastX;
 private float lastY;
 private int left;
 private int top;
 private int right;
 private int bottom;


 // If it is a drag event, there is no need to respond to a click event 
 boolean isMove = false;
 boolean isAnimatoring = false;

 // Screen width and height 
 private int screenWidthPx;
 private int screenHeightPx;


 public MoveImageView(Context context) {
  this(context, null);
 }

 public MoveImageView(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public MoveImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  screenWidthPx = getScreenWidthPx(getContext());
  screenHeightPx = getScreenHeightPx(getContext());
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    // During animation execution, do not respond 1 Cutting operation, 
    //  This is equivalent to not letting it go behind MotionEvent.ACTION_MOVE: And MotionEvent.ACTION_UP:
    if (isAnimatoring) {
     return false;
    }
    lastX = event.getRawX();
    lastY = event.getRawY();
    left = getLeft();
    top = getTop();
    right = getRight();
    bottom = getBottom();
    break;
   case MotionEvent.ACTION_MOVE:
    isMove = true;
    float x = event.getRawX();
    float y = event.getRawY();


    int l = (int) (left + (x - lastX));
    int t = (int) (top + (y - lastY));
    int r = (int) (right + (x - lastX));
    int b = (int) (bottom + (y - lastY));
    layout(l, t, r, b);
    break;
   case MotionEvent.ACTION_UP:
    if (isMove) {
     // If the top is dragged out of the screen, return to the front 
     if (getTop() < 0) {
      layout(getLeft(), 0, getRight(), getHeight());
     }

     //getBottom()  What you get is   The distance from the bottom of the control to the top of the parent container, so you need to subtract the height of the status bar 
     int bottomHeight = screenHeightPx - getStatusBarHeight(getContext());
     // If the bottom is dragged out of the screen, return to the front 
     if (getBottom() > bottomHeight) {
      layout(getLeft(), bottomHeight-getHeight(), getRight(), bottomHeight);
     }
     isMove = false;
     startAnimation();
     return true;
    }
    return super.onTouchEvent(event);
  }
  return super.onTouchEvent(event);
 }

 private void startAnimation() {

  isAnimatoring = true;

  // Right margin 
  int marinRight = DisplayUtils.dpToPx(20);
  int endValue = screenWidthPx - marinRight;
  ValueAnimator animator = ValueAnimator.ofInt(getRight(), endValue);

  animator.setDuration(Math.abs(endValue - getRight()) > 1000 ? 1000 : Math.abs(endValue - getRight()));

  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator animation) {
    int curValue = (int) animation.getAnimatedValue();
    layout(curValue - getWidth(), getTop(), curValue, getHeight() + getTop());
   }
  });
  animator.addListener(new AnimatorListenerAdapter() {
   @Override
   public void onAnimationStart(Animator animation) {

   }

   @Override
   public void onAnimationEnd(Animator animation) {
    isAnimatoring = false;
    animator.removeAllUpdateListeners();
    animator.removeAllListeners();
   }
  });
  animator.start();
 }

 /**
  *  Get the height of the status bar 
  */
 public static int getStatusBarHeight(Context context) {
  int result = 24;
  int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resId > 0) {
   result = context.getResources().getDimensionPixelSize(resId);
  } else {
   result = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
     result, Resources.getSystem().getDisplayMetrics());
  }
  return result;
 }

 public static int getScreenWidthPx(Context context) {
  WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics dm = new DisplayMetrics();
  if (windowManager != null) {
//   windowManager.getDefaultDisplay().getMetrics(dm);
   windowManager.getDefaultDisplay().getRealMetrics(dm);
   return dm.widthPixels;
  }
  return 0;

 }

 public static int getScreenHeightPx(Context context) {
  WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics dm = new DisplayMetrics();
  if (windowManager != null) {
//   windowManager.getDefaultDisplay().getMetrics(dm);
   windowManager.getDefaultDisplay().getRealMetrics(dm);
   return dm.heightPixels;
  }
  return 0;

 }

}

Application:

Layout file:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".TestActivity">


 <com.lingtao.ltvideo.widgets.MoveImageView
  android:layout_width="50dp"
  android:id="@+id/MoveImageView"
  android:layout_height="50dp"
  android:layout_alignParentRight="true"
  android:layout_alignParentBottom="true"
  android:layout_marginBottom="100dp"
  android:layout_marginRight="20dp"
  android:src="#ff0000" />


</RelativeLayout>

Activity:


public class TestActivity extends AppCompatActivity {

 private MoveImageView moveImageView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_test2);
  moveImageView = ((MoveImageView) findViewById(R.id.MoveImageView));
  moveImageView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Toast.makeText(TestActivity.this, " Click event ", Toast.LENGTH_SHORT).show();
   }
  });
 }
}

Related articles: