1 PackageUI;2 3 ImportAndroid.content.Context;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.Color;6 ImportAndroid.graphics.Paint;7 ImportAndroid.util.AttributeSet;8 Importandroid.view.MotionEvent;9 ImportAndroid.view.View;Ten One /** A * Created by Administrator on 2016/4/3. - */ - Public classMovecircleextendsview{ the Publicmovecircle (Context context) { - Super(context); - } - + Publicmovecircle (Context context, AttributeSet attrs) { - Super(context, attrs); + } A at PublicMovecircle (context context, AttributeSet attrs,intdefstyleattr) { - Super(context, attrs, defstyleattr); - } - - floatCurrentX = 50.0f; - floatCurrentY = 50.0f; in - @Override to protected voidOnDraw (canvas canvas) { + Super. OnDraw (canvas); -Paint paint =NewPaint (); the Paint.setcolor (color.red); *Canvas.drawcircle (currentx,currenty,10, paint); $ }Panax Notoginseng - @Override the Public Booleanontouchevent (Motionevent event) { + Super. Ontouchevent (event); ACurrentX =event.getx (); theCurrentY =event.gety (); + //Redraw - invalidate (); $ return true; $ - } -}Custom Spheres
1 < ui. Movecircle 2 android:id = "@+id/circle" android:layout_width = "Wrap_content" 4 Android:layout_height = "Wrap_content" 5 android:minheight = "100DP" 6 Android:minwidth Span style= "color: #0000ff;" >= "100DP" 7 />
XML
1 Follow the finger to move the ball