Dynamically add background, half rounded corner, and dynamic background rounded corner to the component

Source: Internet
Author: User

Dynamically add background, half rounded corner, and dynamic background rounded corner to the component

Respect labor results, reprinted please indicate the source: http://www.cnblogs.com/tangZH/p/8305063.html

 

When customizing a view, we sometimes need to give the component a background, which may be irregular, such as a red background, but there are rounded corners on the left and no corners on the right, so what should we do? Here I use textView as an example.

Xml is easy to implement, but it is implemented dynamically in the code.

 

Put it first:

 

The idea at the beginning is to draw the text directly on the TextView, but this will happen, that is, after the painting, the font will not be visible, so we have to start with the background, use the setBackgroundDrawable () method to give it a background, which is drawn by ourselves. The Drawable class can achieve this effect:

1. Paint Brush and view

/*** Paint background Paint brush */private Paint mPaintBg; mPaintBg = new Paint ();/*** textView */private TextView TV;

 

2. initialize the paint brush:

// Set the paint brush color mPaintBg. setColor (mContext. getResources (). getColor (R. color. theme); // The type is filled with mPaintBg. setStyle (Paint. style. FILL); // anti-sawtooth mPaintBg. setAntiAlias (true); mPaintBg. setStrokeWidth (mContext. getResources (). getDimension (R. dimen. size ));

 

2. Set Drawable

Drawable drawable = new Drawable () {@ Override public void draw (@ NonNull Canvas ){
// Draw the background on the canvas (there are two parameters, Path, Paint, and mPaintBg, which are the Paint brushes on the canvas,
// As for path, I will talk about canvas below. drawPath (path, mPaintBg) ;}@ Override public void setAlpha (int I) {}@ Override public void setColorFilter (@ Nullable ColorFilter colorFilter) {}@ Override public int getOpacity () {return PixelFormat. TRANSLUCENT ;}};

 

4. I noticed at home that the above CodeCanvas. drawPath (path, mPaintBg)The parameter has a path, which is the path we have drawn. How to set this path? The Code is as follows:

RectF rectfBg;Path path = new Path();rectfBg = new RectF(0, 0, leftTv.getWidth(), leftTv.getHeight());path.addRoundRect(rectfBg, new float[]{10, 10, 0, 0, 0, 0, 10, 10}, Path.Direction.CW);

 

Here we use a Path method:

Public void addRoundRect (RectF rect, float [] radii, Path. Direction dir)

Add a closed rounded rectangle to the path.

 

Radii:Indicates the radius points x and y of each corner, which are the upper left corner, the upper right corner, the lower right corner, and the lower left corner,

Rect:Rectangle position

Dir:The direction of drawing. There are two options:

Path. Direction. CW: clockwise

Path. Direction. CCW: counterclockwise

Finally, set the Drawable to textView.
tv.setBackgroundDrawable(drawable)

This is done.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.