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

What did the Internet giants do on April Fools' Day?

Editor’s Note: Although this year's April Foo...

Tencent advertising strategy for the wedding photography industry

1. The market size of the wedding photography ind...

Why do you try so hard to keep up with the latest trends?

Chasing hot topics is something that operators of...

How much does it cost to create Sogou Encyclopedia? Are there any discounts?

Everyone knows that Sogou Encyclopedia is a platf...

User cognitive bias: Your brain is playing tricks on you…

People's understanding and judgment of the wo...

How to achieve "real sales" KOL marketing in 2 steps?

Unilever CMO Keith Weed announced at the recent C...