Simple application of view in Android---small ball that moves with your finger

Source: Internet
Author: User

public class Drawview extends view{public float currentx=40;    public float currenty=50;    Public Drawview (Context context) {super (context);        }//rewrite OnDraw, protected void OnDraw (canvas canvas) {Paint paint=new paint () through canvas painting;        Paint.setcolor (color.red);    Canvas.drawcircle (Currentx,currenty,25,paint); }
<?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:o rientation= "vertical"     android:background= "#99FFCC"     android:id= "@ +id/root "></LINEARLAYOUT> 
public class mainactivity extends activity{     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (r.layout.activity_main);         linearlayout root= (linearlayout)  findviewbyid (r.id.root);        final  Drawview drawview=new drawview (this);         windowmanager  wm = this.getwindowmanager ();         int width  = wm.getdefaultdisplay (). getwidth ();         int height  = wm.getdefaultdisplay (). GetHeight ();         toast.maketext (This, "Width is" +width+ "Height "+height,toast.length_long). Show ();         Drawview.setminimumheight (+);         drawview.setminimumheight (500);         drawview.setontouchlistener (New View.OnTouchListener () {              @Override              public boolean ontouch (View v, MotionEvent  event)  {                 drawview.currentx=event.getx ();                 drawview.currenty=event.gety ();                 drawview.invalidate ();                 return  true;            }         });             root.addview (Drawview);     }}


This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1726924

Simple application of view in Android---small ball that moves with your finger

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.