Custom view implementations follow the finger of the ball

Source: Internet
Author: User

 Packagecom.pingyijinren.test;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*** Created by Administrator on 2016/5/21 0021.*/ Public classDrawviewextendsView {Private floatx=100; Private floaty=100;  PublicDrawview (Context context) {Super(context); }   //when customizing a view, you must implement the view's three constructors     PublicDrawview (Context context, AttributeSet attrs) {Super(Context,attrs); }     PublicDrawview (context context, AttributeSet attrs,intDefstyle) {        Super(Context,attrs,defstyle); } @Override Public voidOnDraw (canvas canvas) {//OnDraw is not required and can be rewritten on demand        Super. OnDraw (canvas); Paint Paint=NewPaint ();        Paint.setcolor (color.red); Canvas.drawcircle (x, Y,100, paint); }     Public voidSetX (floatx) {         This. x=x; }     Public voidSety (floaty) {         This. y=y; }}
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:layout_width=" Match_ Parent "    android:layout_height=" Match_parent "    tools:context=" Com.pingyijinren.test.MainActivity ">    <com.pingyijinren.test.DrawView        android:layout_width = "Wrap_content"        android:layout_height= "Wrap_content"        android:id= "@+id/ Drawview "/></relativelayout>
 Packagecom.pingyijinren.test;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {PrivateDrawview Drawview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Try{Setcontentview (r.layout.activity_main); }        Catch(Exception e) {e.printstacktrace (); LOG.D ("Mainactivity", E.getmessage ()); } Drawview=(Drawview) Findviewbyid (R.id.drawview); Drawview.setontouchlistener (NewView.ontouchlistener () {//Drawview Responding to screen touch events@Override Public BooleanOnTouch (View V, motionevent event) {Drawview.setx (Event.getx ());                Drawview.sety (Event.gety ());                Drawview.invalidate (); return true;    }        }); }}

Custom View implements a small ball that follows 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.