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

Master Xian's internal lesson on how to get rid of singleness

Introduction to the resources of Master Xian’s in...

The realm of "stealing skills": creating oneself from classics | Yihai Shizhen

How did the talented painter Lippi learn from oth...

The underlying logic of brand leveraging marketing!

Many brands are accustomed to using the marketing...

Attention! Don’t post these things to your friends circle

Chinese New Year Also in the circle of friends Th...

Tik Tok account homepage design guide

1. Why should we pay attention to the homepage de...

How hot is the sun? This probe has already touched it

During the Cold War between the United States and...

iPhone X operation becomes more complicated: Apple is helpless but determined

The arrival of iPhone X means that Apple has fina...