Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the control demo of Android following finger movement

Shulou Source: shulou.com Published: 2022-06-02 05:30:07 09月27日 Update

This article mainly explains "how to realize the control demo of Android following finger movement". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize the control demo of Android following finger movement".

Use custom View components to implement demo.

The first step

First create an Android project, and we use Android studio to automatically create the activity.

Step 2:

Prepare two pictures, one is the background picture of the prairie, the other is the running horse. Put both pictures under the src/main/res/mipmap-xhdpi file.

Step 3:

Set the layout under the activity_main file. The general custom components are placed in the frame layout manager, using the prairie image as the background for the layout, and giving the layout an id, because it will be used in the later logic code. The detailed code is as follows:

The fourth step

Create a java file called HorseDemo.java, which mainly sets the basic properties of the horse and instantiates the drawing and bitmap methods. The detailed code is as follows:

Package com.xiaozeng.demo;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.view.View;public class HorseDemo extends View {/ / set two variables public float horseX,horseY;// creation constructor public HorseDemo (Context context) {super (context); horseX = 200; horseY = 300 Step 2: rewrite the onDraw method Ctrl+o @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); / / instantiate the drawing object Paint paint = new Paint (); / / Bitmap object Bitmap bitmap = BitmapFactory.decodeResource (this.getResources (), R.mipmap.pic2); / / Recycle canvas.drawBitmap (bitmap,horseX,horseY,paint) If (bitmap.isRecycled ()) {bitmap.recycle ();} step 5

You need to get the layout manager in the MainActivity file and add a touch event listener. The complete code is as follows:

Package com.xiaozeng.demo;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.FrameLayout;import android.widget.ViewAnimator;public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / get layout manager FrameLayout frameLayout = findViewById (R.id.horse) / / instantiate HorseDemo horseDemo = new HorseDemo (this); / / Touch event listener horseDemo.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {/ / coordinates horseDemo.horseX = event.getX (); horseDemo.horseY = event.getY () / / redraw horseDemo.invalidate (); return true;}}); frameLayout.addView (horseDemo);}} at this point, I believe you have a deeper understanding of "how to implement the control demo of Android following finger movement". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Layout file code picture method finger control movement instance management event bitmap content object listener component background prairie horse learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi MySQL Xiaomi Shulou Tech Info