SoFunction
Updated on 2025-03-01

Android development method to implement rounded rectangles based on Drawable

This article describes the Android development method of implementing rounded rectangles based on Drawable. Share it for your reference, as follows:

Step 1: Write a class to inherit the drawable, rewrite the methods inside, and the core code of the implementation is in the draw

Key Technologies: BitmapShader

public BitmapShader(Bitmap bitmap, tileX, tileY)

Call this method to generate a renderer (Shader) with a bitmap.

bitmapBitmap used in the renderer
tileX The tiling mode for x to draw the bitmap in.
tileY The tiling mode for y to draw the bitmap in.

TileMode
CLAMP: If the renderer exceeds the original boundary range, edge staining within range will be copied.
REPEAT: Repeat renderer pictures in landscape and portrait, tile.
MIRROR: Repeat renderer images in landscape and portrait. This is different from REPEAT repetition method. It is tiled in mirror mode.

/**
 * Four screen size categories: small, normal, large, and xlarge
 * Four density categories: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
 * DPI of four screens: ldpi is 120dpi, mdpi is 160dpi, hdpi is 240dpi, xhdpi is 320dpi
 * The corresponding density of the four screens: 0.75, 1, 1.5, 2
 * Four types of image resource folders: drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi
 */
/**
  * Round corners
  *
  * @Project App_View
  * @Package
  * @author chenlin
  * @version 1.0
  * @Note TODO
  */
public class RoundImageDrawable extends Drawable {
 private Paint mPaint;
 private Bitmap mBitmap;
 private RectF mRectF;
 private int mRound;
 public RoundImageDrawable(Bitmap bitmap) {
   = bitmap;
  mPaint = new Paint();
  (true);
  BitmapShader shader = new BitmapShader(mBitmap, , );
  (shader);
 }
 /**
   * Initialize the area
   */
 @Override
 public void setBounds(int left, int top, int right, int bottom) {
  mRectF = new RectF(left, top, right, bottom);
  (left, top, right, bottom);
 }
 /**
   * Core code: Draw rounded corners
   */
 @Override
 public void draw(Canvas canvas) {
  (mRectF, mRound, mRound, mPaint);
 }
 /**
   * Exposed to the outside to set the size of the rounded corners
   *
   * @param round
   */
 public void setRound(int round) {
   = round;
 }
 /**
   * getIntrinsicWidth and getIntrinsicHeight are mainly used when view uses wrap_content.
   * Provide the size, the default is -1, which is not what we hope
   */
 @Override
 public int getIntrinsicHeight() {
  return ();
 }
 @Override
 public int getIntrinsicWidth() {
  return ();
 }
 /**
   * Set the transparency of drawable according to the brush
   */
 @Override
 public void setAlpha(int alpha) {
  (alpha);
 }
 /**
   * Set the drawable color filter according to the brush
   */
 @Override
 public void setColorFilter(ColorFilter cf) {
  (cf);
 }
 @Override
 public int getOpacity() {
  return ;
 }
}

Step 2: Implement the class

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

activity_round_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <ImageView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scaleType="fitXY" />
</LinearLayout>

Draw a circle picture

Core code:

mWidth = ((), ());
(mWidth / 2, mWidth / 2, mRadius, mPaint);

/**
  * Circular
  * @Project App_View
  * @Package
  * @author chenlin
  * @version 1.0
  * @Note TODO
  */
public class CircleImageDrawable extends Drawable {
  private Bitmap mBitmap;
  private Paint mPaint;
  private int mWidth;
  private int mRadius;
  public CircleImageDrawable(Bitmap bitmap){
     = bitmap;
    (true);
    BitmapShader shader = new BitmapShader(mBitmap, , );
    (shader);
    mWidth = ((), ());
    mRadius = mWidth / 2;
  }
  /**
    * Core code
    */
  @Override
  public void draw(Canvas canvas) {
    (mWidth / 2, mWidth / 2, mRadius, mPaint);
  }
  /**
    * getIntrinsicWidth and getIntrinsicHeight are mainly used when view uses wrap_content.
    * Provide the size, the default is -1, which is not what we hope
    */
  @Override
  public int getIntrinsicHeight() {
    return mWidth;
  }
  @Override
  public int getIntrinsicWidth() {
    return mWidth;
  }
  @Override
  public void setAlpha(int alpha) {
    (alpha);
  }
  @Override
  public void setColorFilter(ColorFilter cf) {
    (cf);
  }
  @Override
  public int getOpacity() {
    return ;
  }
}

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.