ImageView based on Android to realize finger movement

  • 2020-12-19 21:12:52
  • OfStack

ImageView is used to display any image image, you can define the display size, display color and so on.

Here's what it looks like (text) :

When you first enter the program, tap your finger anywhere on the screen and the image will move accordingly.

Layout file


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0" >
<com.sgw.move.MoveImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" >
</com.sgw.move.MoveImageView>
</FrameLayout>

The implementation code


public class MoveImageView extends ImageView {
public MoveImageView(Context context) {
super(context);
}
public MoveImageView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public MoveImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setLocation(int x, int y) {
this.setFrame(x, y - this.getHeight(), x + this.getWidth(), y);
}
//  mobile 
public boolean autoMouse(MotionEvent event) {
boolean rb = false;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
this.setLocation((int) event.getX(), (int) event.getY());
rb = true;
break;
}
return rb;
}
}
public class TestImageViewMove extends Activity {
private MoveImageView moveImageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
moveImageView = (MoveImageView) this.findViewById(R.id.ImageView01);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
moveImageView.autoMouse(event);
return false;
}
}

The above content introduces the relevant knowledge of ImageView based on Android to realize finger movement. I hope this article will be helpful to you.


Related articles: