Small example of Android app-the ball that follows your finger

Source: Internet
Author: User

 

implementation:

Implementation method:

Inherit the view class and redraw a circle yourself, and then add this custom inherited view class to the current activity and adds a Ontouchlitener event for this custom view control

Custom components inherit from the view class:

[Java]  View Plain copy print?
  1. Package Com.jiahui.draw;
  2.   
  3. import android.content.Context;
  4. import Android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.view.View;
  8.   
  9. public class drawview extends View {
  10.   
  11. public float currentx=;
  12. public float currenty=;
  13.       
  14. //Must override a constructor method
  15. Public Drawview (context context) {
  16. Super(context);
  17. //TODO auto-generated constructor stub
  18.     }  
  19. //Rewrite the OnDraw method through canvas painting
  20. @Override
  21. protected void onDraw (canvas canvas) {
  22. Super. OnDraw (canvas);
  23. //Create brushes
  24. Paint paint=new paint ();
  25. Paint.setcolor (color.red);
  26. //Draw a small circle
  27. canvas.drawcircle (CurrentX, CurrentY, +, paint);
  28.   
  29.     }  
  30.       
  31. }  


Add a custom component to the current activity

[HTML]  View Plain copy print?
  1. Package Com.jiahui.draw;
  2.   
  3. import android.app.Activity;
  4. import Android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import Android.view.View;
  7. import android.widget.LinearLayout;
  8.   
  9. Public class Customeviewactivity extends Activity {
  10. Public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13.           
  14. linearlayout root= (linearlayout) Findviewbyid (r.id.root);
  15.           
  16. //Create an object that you inherit from the view
  17. final Drawview drawview=new Drawview (this);
  18. //Set the maximum width and height of the custom component
  19. Drawview.setminimumwidth (300);
  20. Drawview.setminimumheight (500);
  21.     
  22. Drawview.setontouchlistener (New View.ontouchlistener () {
  23. Public Boolean OnTouch (View V, motionevent event) {
  24. drawview.currentx=event. GetX ();
  25. drawview.currenty=event. GetY ();
  26. // Redraw
  27. drawview.invalidate ();
  28. //Returns True to indicate that the method has handled the event
  29. return true;
  30.             }  
  31.         });
  32. Root.addview (Drawview);
  33.     }  
  34. }  


Main.xml:

[HTML]  View Plain copy print?
  1. <? XML version="1.0" encoding="Utf-8" ?>
  2. < linearlayout xmlns:android="http://schemas.android.com /apk/res/android "
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="#99FFCC"
  7. android:id="@+id/root">
  8. </linearlayout >


If reproduced please declare the article source: http://blog.csdn.net/jiahui524

Source code Download: http://download.csdn.net/detail/jiahui524/3786788

Small example of Android app-the ball that follows your finger

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.