SoFunction
Updated on 2025-04-07

Android implements the method of making pictures move arbitrarily on the screen (drag and drop function)

This article describes the method of Android to enable pictures to move arbitrarily on the screen. Share it for your reference, as follows:

public class DragExampleActivity extends Activity {
  Bitmap mBitmap;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    mBitmap = (getResources(), );
    setContentView(new DragView(this));
  }
  @Override
  protected void onDestroy()
  {
    ();
    if(null != mBitmap) {
      ();
      mBitmap = null;
    }
  }
  private class DragView extends View {
    private int mMotionX = 0;
    private int mMotionY = 0;
    private Paint paint;
    public DragView(Context context)
    {
      super(context);
      paint = new Paint();
    }
    @Override
    public void draw(Canvas canvas)
    {
      (canvas);
      (mBitmap, mMotionX, mMotionY, paint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
      if(() == MotionEvent.ACTION_DOWN)
      {
        mMotionX = (int) ();
        mMotionY = (int) ();
        invalidate();
        return true;
      }else {
        return (ev);
      }
    }
  }
}

For more information about Android related content, please check out the topic of this site:Summary of Android photography and picture processing skills》、《Android programming activity operation skills summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.