Android-6 steps to teach you how to customize View

Android-6 steps to teach you how to customize View

If you plan to completely customize a View, you need to implement the View class (all Android Views are implemented in this class) and implement the onMeasure(...) method to determine the size and the onDraw(...) method to confirm the drawing.

Customizing View is divided into 6 steps

***step

  1. public class SmileyView extends View {
  2. private Paint mCirclePaint;
  3. private Paint mEyeAndMouthPaint;
  4.  
  5. private float mCenterX;
  6. private float mCenterY;
  7. private float mRadius;
  8. private RectF mArcBounds = new RectF();
  9.  
  10. public SmileyView(Context context) {
  11. this(context, null );
  12. }
  13.  
  14. public SmileyView(Context context, AttributeSet attrs) {
  15. this(context, attrs, 0);
  16. }
  17.  
  18. public SmileyView(Context context, AttributeSet attrs, int defStyleAttr) {
  19. super(context, attrs, defStyleAttr);
  20. initPaints();
  21. }
  22.  
  23. private void initPaints() {/* ... */}
  24.  
  25. @Override
  26. protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {/* ... */}
  27.  
  28. @Override
  29. protected void onDraw(Canvas canvas) {/* ... */}
  30. }

2. Implement the paint brush class

There are two brushes in this article

  1. private void initPaints() {
  2. mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  3. mCirclePaint.setStyle(Paint.Style.FILL);
  4. mCirclePaint.setColor(Color.YELLOW);
  5. mEyeAndMouthPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  6. mEyeAndMouthPaint.setStyle(Paint.Style.STROKE);
  7. mEyeAndMouthPaint.setStrokeWidth(16 * getResources().getDisplayMetrics().density);
  8. mEyeAndMouthPaint.setStrokeCap(Paint.Cap.ROUND);
  9. mEyeAndMouthPaint.setColor(Color.BLACK);
  10. }

3. Override the onMeasure(…) method

Implementing this method tells the parent container how to abandon the custom View. You can determine the height and width of your View by providing measureSpecs. The following is a square. Confirm that its width and height are the same.

  1. @Override
  2. protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
  3. int w = MeasureSpec.getSize(widthMeasureSpec);
  4. int h = MeasureSpec.getSize(heightMeasureSpec);
  5.  
  6. int   size = Math.min (w, h) ;
  7. setMeasuredDimension( size , size );
  8. }

Notice:

This method requires at least one setMeasuredDimension(..) call, otherwise an IllegalStateException error will be reported.

4. Implement the onSizeChanged(…) method

This method is how you get the current width and height of the View. Here we calculate the center and radius.

  1. @Override
  2. protected void onSizeChanged( int w, int h, int oldw, int oldh) {
  3. mCenterX = w / 2f;
  4. mCenterY = h / 2f;
  5. mRadius = Math. min (w, h) / 2f;
  6. }

5. Implement the onDraw(…) method

This method provides how to draw the view, and the Canvas class it provides can be used for drawing.

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. // draw face
  4. canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);
  5. // draw eyes
  6. float eyeRadius = mRadius / 5f;
  7. float eyeOffsetX = mRadius / 3f;
  8. float eyeOffsetY = mRadius / 3f;
  9. canvas.drawCircle(mCenterX - eyeOffsetX, mCenterY - eyeOffsetY, eyeRadius, mEyeAndMouthPaint);
  10. canvas.drawCircle(mCenterX + eyeOffsetX, mCenterY - eyeOffsetY, eyeRadius, mEyeAndMouthPaint);
  11. // draw mouth
  12. float mouthInset = mRadius /3f;
  13. mArcBounds.set (mouthInset, mouthInset, mRadius * 2 - mouthInset, mRadius * 2 - mouthInset) ;
  14. canvas.drawArc(mArcBounds, 45f, 90f, false , mEyeAndMouthPaint);
  15. }

6. Add your View

  1. <FrameLayout
  2. xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. android:layout_width= "match_parent"  
  4. android:layout_height= "match_parent" >
  5.  
  6. <com.example.app.SmileyView
  7. android:layout_width= "match_parent"  
  8. android:layout_height= "match_parent" />
  9. </FrameLayout>

That’s it. Customizing View is not as difficult as you think.

<<:  10 Tips for Solving Problems in Android Development

>>:  Android Studio debugging tips you don't know

Recommend

Entering the "flower umbrella world" in the ocean - jellyfish

In the mysterious depths of the ocean, there live...

Why do allergies always occur in spring?

Spring is supposed to be a beautiful season full ...

Why does the watermelon in the refrigerator smell like leeks?

The refrigerator is a weird place. A half-eaten w...

How to plan an excellent Christmas recruitment event?

As December is coming to an end, Christmas is aro...

Ten secrets that new programmers should know

You are a freshman who steps into the workplace w...

Pinduoduo’s suicidal PR and H&H’s “brand failure theory”

On the first working day of 2021, Pinduoduo contr...

Spam SMS filtering app based on iOS 11 machine learning: Pandas eat SMS

As a tech geek living in the mobile Internet era,...

A comprehensive review of the top ten most classic interactive marketing cases!

Today, I will take you to review the top ten inte...

Startup App, 4 Cold Start Methods from 0 to 1

When cold-starting , Internet products can be rou...

How to Evolve Neural Networks with Automatic Machine Learning

For most people working in machine learning, desi...

What is the use of the “blue” in Isatis root?

As a common household medicine for clearing away ...