Android custom View1 and android custom view1

Source: Internet
Author: User

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.

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.