Android: Start the circular progress bar from top (270°)

后端 未结 6 453
北海茫月
北海茫月 2021-01-30 21:40

I have defined a circular progress bar using the following drawable \"ciruclar_progress_bar.xml\"


<         


        
6条回答
  •  被撕碎了的回忆
    2021-01-30 21:48

    Here's how I made circular progressbar with percentage inside circle in pure code without any library.

    Reference is taken from here : circular progress bar android

    first create a drawable file called circular.xml

    
    
    
        
            
    
    
                
            
        
    
        
            
    
                
    
    
                    
    
                    
    
                
            
        
    
    

    Now in your activity_main.xml add following:

      
    
    
        
    
        
    
        
    
    
    

    In activity_main.xml I have used one circular image with white background to show white background around percentage. Here is the image:

    You can change color of this image to set custom color around percentage text.

    Now finally add following code to MainActivity.java :

    import android.content.res.Resources;
    import android.graphics.drawable.Drawable;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.animation.DecelerateInterpolator;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        int pStatus = 0;
        private Handler handler = new Handler();
        TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.circular);
            final ProgressBar mProgress = (ProgressBar) findViewById(R.id.circularProgressbar);
            mProgress.setProgress(0);   // Main Progress
            mProgress.setSecondaryProgress(100); // Secondary Progress
            mProgress.setMax(100); // Maximum Progress
            mProgress.setProgressDrawable(drawable);
    
          /*  ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
            animation.setDuration(50000);
            animation.setInterpolator(new DecelerateInterpolator());
            animation.start();*/
    
            tv = (TextView) findViewById(R.id.tv);
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (pStatus < 100) {
                        pStatus += 1;
    
                        handler.post(new Runnable() {
    
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                mProgress.setProgress(pStatus);
                                tv.setText(pStatus + "%");
    
                            }
                        });
                        try {
                            // Sleep for 200 milliseconds.
                            // Just to display the progress slowly
                            Thread.sleep(8); //thread will take approx 1.5 seconds to finish
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
    

    If you want to make horizontal progressbar, follow this link, it has many valuable examples with source code:
    http://www.skholingua.com/android-basic/user-interface/form-widgets/progressbar

提交回复
热议问题