SoFunction
Updated on 2025-04-09

View follows finger movement effect in Android

I recently did a project where I encountered such a requirement that the image moves to the place where the finger touches. The specific implementation code is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
private int speed = 10;
private MyPlane plane;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(.FLAG_FULLSCREEN,
.FLAG_FULLSCREEN);
plane = new MyPlane(this);
setContentView(plane);
// (.ic_launcher);
WindowManager manager = getWindowManager();
Display display = ();
DisplayMetrics metrics = new DisplayMetrics();
(metrics);
 =  / 2;
 =  / 2;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/*float x = 0;
float y = 0;
switch (()) {
case MotionEvent.ACTION_DOWN:
x = ();
y = ();
break;
case MotionEvent.ACTION_MOVE:
x = ();
y = ();
break;
case MotionEvent.ACTION_UP:
x = ();
y = ();
break;
}*/
=();
=();
();
return (event);
}
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MyPlane extends View {
Bitmap plane;
public float currentX;
public float currentY;
public MyPlane(Context context) {
super(context);
plane = ((),
.ic_launcher);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
(canvas);
Paint p = new Paint();
(plane, currentX, currentY, p);
}
}

The above is the effect of view following the finger movement in Android introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!