Android custom View1 and android custom view1
The goal is to create a calendar, but you should first look at it first.
To put it simply, the custom view is divided into seven parts, each of which is written with a word. Then, the custom view can slide with the finger and restore to the initial position after raising the finger, click view to determine which view the vertex is. (all views are calculated by coordinates)
The Code is also simple. Check it out.
Public class AAAView extends View {private Paint paint; private float moveX; // move the distance from private float downX; // private int r where the finger is pressed; // The radius of each word is private int d; // the diameter is private int location; // The word on which the finger is pressed. private Context context; public AAAView (Context context) {super (context ); init (context);} public AAAView (Context context, @ Nullable AttributeSet attrs) {super (context, attrs); init (context);} public AAAView (Context context, @ Nullable AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context);} private void init (Context context) {paint = new Paint (); paint. setColor (Color. BLACK); paint. setTextSize (25); this. context = context ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); r = getWidth ()/14; d = 2 * r; for (int I = 0; I <7; I ++) {canvas. drawText ("a", r + I * 2 * r + moveX, 30, paint); }}@ Override public boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: downX = event. getX (); location = (int) Math. ceil (downX/d); return true; case MotionEvent. ACTION_MOVE: moveX = (int) (event. getX ()-downX); invalidate (); return true; case MotionEvent. ACTION_UP: // after each gesture is raised, the initial state moveX = 0; invalidate (); if (event. getX () <location * d & event. getX ()> (location-1) * d) {Toast. makeText (context, "click the" + location + "character", Toast. LENGTH_LONG ). show ();} return true;} return super. onTouchEvent (event );}}
It is difficult to start with everything. It is estimated that the calendar series will take a long time to write, but it should be done slowly.