Android custom View3 and android custom view3

Source: Internet
Author: User
Tags drawtext

Android custom View3 and android custom view3

First look

In the previous article, custom view2 was not displayed on the homepage because of the number of words. It seems that it can only be viewed in my essay list. This is the third article. In order to gather the number of words, let's look at the key points.

1canvas. How does the drawText parameter control the text position?

Canvas. drawText (text, x, y, paint );

X is the left margin of the text by default, and the horizontal line corresponding to y is the bottom of the text, rather than the center of the text. It is important here. Obviously, we need to set the coordinates of the center x, if you enter y directly, you will not get the desired effect.

On the x axis, how does one set text to the center of the x parameter? It mainly relies on the paint to set a parameter.

Paint. setTextAlign (Paint. Align. CENTER); // after this setting, x coordinate points are the horizontal CENTER of the text.

On the y axis, how can I set the text to the y parameter as the center point? How can I write the text to the height of 50 if the height of the control is 100, y: this parameter is about 50 + text size/4 + a little bit (you can add or not, the above effect is not added, and the text is a bit down)

2. Draw a circle

canvas.drawCircle(x, y, paind);

3. Let's take a look at the overall code and make the changes based on the previous article.

Public class AAAView extends View {private Paint paint; private Paint bluePaint; private Paint whitePaint; private Paint greenPaind; private Scroller scroller; private float moveX; // The moving distance from private float downX; // The position where the finger is pressed private int r; // The radius of each word is private int d; // the diameter is private int h; // The control height is private int textSize = 25; private int location; // The word on which the finger is pressed. private int checked = 3; // select and change the value to Blue private String [] week = new String [] {"1 ", "2", "3", "4", "5", "6", "day"}; 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 (); bluePaint = new Paint (); whitePaint = new Paint (); greenPaind = new Paint (); scroller = new Scroller (context); paint. setColor (Color. BLACK); paint. setAntiAlias (true); paint. setTextSize (textSize); paint. setTextAlign (Paint. align. CENTER); bluePaint. setColor (Color. BLUE); bluePaint. setAntiAlias (true); bluePaint. setTextSize (textSize); bluePaint. setTextAlign (Paint. align. CENTER); greenPaind. setColor (Color. GREEN); greenPaind. setAntiAlias (true); greenPaind. setTextSize (textSize); greenPaind. setTextAlign (Paint. align. CENTER); whitePaint. setColor (Color. WHITE); whitePaint. setAntiAlias (true); whitePaint. setTextSize (textSize); whitePaint. setTextAlign (Paint. align. CENTER); this. context = context ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); r = getWidth ()/14; d = 2 * r; h = getHeight (); canvas. drawLine (0, h/2, getWidth (), h/2, paint); canvas. drawCircle (r + checked * d + moveX, h/4*3, 25, greenPaind); for (int I = 0; I <7; I ++) {if (I = 5 | I = 6) {canvas. drawText (week [I], r + I * d, h/4 + textSize/2, bluePaint); canvas. drawText (I + "", r + I * d + moveX, h/4*3 + textSize/4, bluePaint);} else {canvas. drawText (week [I], r + I * d, h/4 + textSize/2, paint); canvas. drawText (I + "", r + I * d + moveX, h/4*3 + textSize/4, 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 (); checked = location-1; invalidate ();} return true;} return super. onTouchEvent (event );}}

One step closer to making a calendar, the next one should write a smooth slide, and now the last restoration is a direct restoration, too stiff, and then fill in the real date, then we can complete some logic.

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.