In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.