Small Android app instance-ball with fingers

Source: Internet
Author: User

 

Implementation:

 

Implementation Method:

The inherited View Class re-draws a circle by itself, adds the custom inherited View class to the current activity, and adds an ontouchlitener event to the custom view control.

 

Custom components inherit from the View class:

Package COM. jiahui. draw; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. view; public class drawview extends view {public float currentx = 40; public float currenty = 50; // You must override a constructor public drawview (context) {super (context ); // todo auto-generated constructor stub} // rewrite the ondraw method by painting with canvas @ overrideprotected void ondraw (canvas) {super. ondraw (canvas); // create paint = new paint (); paint. setcolor (color. red); // draw a small circle canvas. drawcircle (currentx, currenty, 25, paint );}}

 

Add custom components to the current activity

Package COM. jiahui. draw; import android. app. activity; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. widget. linearlayout; public class customeviewactivity extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); linearlayout root = (linearlayout) findviewbyid (R. id. root); // create an object that inherits from the view, final drawview = new drawview (this); // set the maximum width and height of the custom component drawview. setminimumwidth (300); drawview. setminimumheight (500); drawview. setontouchlistener (new view. ontouchlistener () {public Boolean ontouch (view V, motionevent event) {drawview. currentx = event. getx (); drawview. currenty = event. gety (); // redraw drawview. invalidate (); // return true indicates that the method has handled the event return true;}); root. addview (drawview );}}

Main. xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="#99FFCC"    android:id="@+id/root"></LinearLayout>

If reprint please declare the Article Source: http://blog.csdn.net/jiahui524

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

 

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.