SoFunction
Updated on 2025-03-10

Android development using Drawable to draw rounded corners and circular patterns

This article describes the Android development function of using Drawable to draw rounded corners and circular patterns. Share it for your reference, as follows:

1. Create the class RoundCircleDrawable inherits Drawable

/**
  * Rounded rectangle
  * @Project App_View
  * @Package
  * @author chenlin
  * @version 1.0
  * @Date April 21, 2016
  * @Note TODO
  */
public class RoundCircleDrawable extends Drawable{
  private Paint mPaint;//brush  private int mWidth;//The minimum value of image width and length  private int mRadius;//radius  private int mRound;//Round corner  private RectF mRectF;//rectangle  private Bitmap mBitmap;//picture  private Type mType = Type.TYPE_ROUND;//The default is rectangle  //Set type  enum Type{
    TYPE_ROUND, TYPE_CICLE;
  }
  public RoundCircleDrawable(Bitmap bitmap){
     = bitmap;
    //Initialize the brush    mPaint = new Paint();
    (true);
    BitmapShader shader = new BitmapShader(mBitmap, , );
    (shader);
    mWidth = ((), ());
    mRadius = mWidth / 2;
  }
  /**
    * Provide a method to set the image type outward
    * @param type
    */
  public void setType(Type type){
     = type;
  }
  /**
    * Exposed to the outside to set the size of the rounded corners
    *
    * @param round
    */
  public void setRound(int round) {
     = round;
  }
  @Override
  public void setBounds(int left, int top, int right, int bottom) {
    (left, top, right, bottom);
    mRectF = new RectF(left, top, right, bottom);
  }
  @Override
  public void draw(Canvas canvas) {
    if (mType == Type.TYPE_ROUND) {
      (mRectF, mRound, mRound, mPaint);
    }else {
      (mWidth / 2, mWidth / 2, mRadius, mPaint);
    }
  }
  @Override
  public int getIntrinsicWidth() {
    if (mType == Type.TYPE_CICLE) {
      return mWidth;
    }else {
      return ();
    }
  }
  @Override
  public int getIntrinsicHeight() {
    if (mType == Type.TYPE_CICLE) {
      return mWidth;
    }else {
      return ();
    }
  }
  @Override
  public void setAlpha(int alpha) {
    (alpha);
  }
  @Override
  public void setColorFilter(ColorFilter cf) {
    (cf);
  }
  @Override
  public int getOpacity() {
    return ;
  }
}

2. Implementation method

public class RoundActivity extends Activity {
  private ImageView mImageView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_round_drawable);
    mImageView = (ImageView) findViewById(.iv_round);
    Bitmap bitmap = (getResources(), );
    //RoundImageDrawable drawable = new RoundImageDrawable(bitmap);
    //(30);
    RoundCircleDrawable drawable = new RoundCircleDrawable(bitmap);
    (50);
    (drawable);
  }
}

For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.