Android rounded rectangle progress bar and android rounded rectangle

Source: Internet
Author: User

Android rounded rectangle progress bar and android rounded rectangle

A requirement for a recent project is the rounded corner progress bar. As follows.


At that time, the project was very time-consuming. If I had no idea how to achieve the best, I simply cut the corner progress bar provided by the artist into four copies. To cut and draw a Canvas. This can also achieve the effect, but the medication is poor. Recently I searched the internet for a long time and found that the Paint brush has the occlusion layer function.Android. graphics. Paint. setXfermode (Xfermode xfermode). One of the parameters is

Mode. DST_OUTDisplays the basemap image that does not overlap with the upper layer. So there is an idea: first draw the progress bar of the rounded corner rectangle, then set the paint brush occlusion attribute, and then draw a sector to mask the rounded corner rectangle. In this way, the progress bar of the rounded rectangle can be implemented.

Import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graphics. rectF; import android. util. attributeSet; import android. view. surfaceHolder; import android. view. surfaceVie W; public class MySurfaceView extends SurfaceView implements SurfaceHolder. callback {SurfaceHolder mSurfaceHolder; boolean isRun;/** progress bar background **/Bitmap BMP;/** movement angle **/float m = 0; /** circle radius of the progress bar background **/float r;/** moving speed **/float speed; public MySurfaceView (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stubmSurfaceHolder = this. getHolder (); mSurfaceHolder. addCal Lback (this);/** load progress bar **/BMP des = BitmapFactory. decodeResource (getResources (), R. drawable. d1 ). copy (Bitmap. config. ARGB_8888, true);/** calculation radius **/r = (float) Math. sqrt (Math. pow (BMP des. getWidth ()/2, 2) + Math. pow (BMP des. getHeight ()/2, 2);/** calculate the moving speed based on 25 s **/speed = (float) (360/(25000/33 .0 ));} @ Overridepublic void surfaceChanged (SurfaceHolder arg0, int arg1, int arg2, int arg3) {// TODO Auto-generated Method stub} @ Overridepublic void surfaceCreated (SurfaceHolder arg0) {// TODO Auto-generated method stubisRun = true; new Thread () {public void run () {while (isRun) {long start = System. currentTimeMillis (); Canvas canvas = null; synchronized (mSurfaceHolder) {canvas = mSurfaceHolder. lockCanvas (); if (canvas! = Null) {onGameDraw (canvas); if (canvas! = Null & mSurfaceHolder! = Null) {mSurfaceHolder. unlockCanvasAndPost (canvas);} m + = speed; if (m> = 360) m = 0;} long end = System. currentTimeMillis ()-start; if (end <33) {try {Thread. sleep (end);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}}. start () ;}@ Overridepublic void surfaceDestroyed (SurfaceHolder arg0) {// TODO Auto-generated method stubisRun = false;} private void onGameDraw (Canvas canvas) {/* Canvas. drawColor (Color. BLACK); c = new Canvas (BMP des); Paint paint = new Paint (); paint. setAntiAlias (true); paint. setXfermode (new porterduduxfermode (Mode. DST_OUT); int SC = c. saveLayer (0, 0, BMP des. getWidth (), BMP. getHeight (), null, LAYER_FLAGS); c. drawArc (new RectF (-(r-BMP des. getWidth ()/2),-(r-BMP des. getHeight ()/2), r * 2, r * 2), 0, m, true, paint); c. restoreToCount (SC); canvas. drawBitmap (BMP des, 0, 0, null); */canvas. drawColor (Color. BLACK); Paint paint = new Paint (); paint. setFilterBitmap (false); // draw the second layer int SC = canvas. saveLayer (0, 0, 0 + BMP. getWidth (), 0 + BMP des. getHeight (), null, Canvas. MATRIX_SAVE_FLAG | Canvas. CLIP_SAVE_FLAG | Canvas. HAS_ALPHA_LAYER_SAVE_FLAG | Canvas. FULL_COLOR_LAYER_SAVE_FLAG | Canvas. CLIP_TO_LAYER_SAVE_FLAG); // draw the canvas of the progress bar. drawBitmap (BMP des, 0, 0, paint); // you can specify the occlusion attribute paint. setXfermode (new porterduduxfermode (Mode. DST_OUT); // draw a canvas that blocks the slice. drawArc (new RectF (-(r-BMP des. getWidth ()/2),-(r-BMP des. getHeight ()/2), r * 2, r * 2), 0, m, true, paint); // The second layer is fed back to the canvas. restoreToCount (SC );}}

Source code

Http://download.csdn.net/detail/li_xiao_kang/8750631 click open link

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.