How to Customize a Progress Bar In Android

前端 未结 9 2392
鱼传尺愫
鱼传尺愫 2020-11-22 16:07

I am working on an app in which I want to show a ProgressBar, but I want to replace the default Android ProgressBar.

So how can I customize

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 16:31

    In case of complex ProgressBar like this,

    enter image description here

    use ClipDrawable.

    NOTE : I've not used ProgressBar here in this example. I've achieved this using ClipDrawable by clipping image with Animation.

    A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars, by increasing the drawable's level with setLevel().

    NOTE : The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.

    I've used this two images to make this CustomProgressBar.

    scall.png

    scall.png

    ballon_progress.png

    ballon_progress.png

    MainActivity.java

    public class MainActivity extends ActionBarActivity {
    
    private EditText etPercent;
    private ClipDrawable mImageDrawable;
    
    // a field in your class
    private int mLevel = 0;
    private int fromLevel = 0;
    private int toLevel = 0;
    
    public static final int MAX_LEVEL = 10000;
    public static final int LEVEL_DIFF = 100;
    public static final int DELAY = 30;
    
    private Handler mUpHandler = new Handler();
    private Runnable animateUpImage = new Runnable() {
    
        @Override
        public void run() {
            doTheUpAnimation(fromLevel, toLevel);
        }
    };
    
    private Handler mDownHandler = new Handler();
    private Runnable animateDownImage = new Runnable() {
    
        @Override
        public void run() {
            doTheDownAnimation(fromLevel, toLevel);
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        etPercent = (EditText) findViewById(R.id.etPercent);
    
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        mImageDrawable = (ClipDrawable) img.getDrawable();
        mImageDrawable.setLevel(0);
    }
    
    private void doTheUpAnimation(int fromLevel, int toLevel) {
        mLevel += LEVEL_DIFF;
        mImageDrawable.setLevel(mLevel);
        if (mLevel <= toLevel) {
            mUpHandler.postDelayed(animateUpImage, DELAY);
        } else {
            mUpHandler.removeCallbacks(animateUpImage);
            MainActivity.this.fromLevel = toLevel;
        }
    }
    
    private void doTheDownAnimation(int fromLevel, int toLevel) {
        mLevel -= LEVEL_DIFF;
        mImageDrawable.setLevel(mLevel);
        if (mLevel >= toLevel) {
            mDownHandler.postDelayed(animateDownImage, DELAY);
        } else {
            mDownHandler.removeCallbacks(animateDownImage);
            MainActivity.this.fromLevel = toLevel;
        }
    }
    
    public void onClickOk(View v) {
        int temp_level = ((Integer.parseInt(etPercent.getText().toString())) * MAX_LEVEL) / 100;
    
        if (toLevel == temp_level || temp_level > MAX_LEVEL) {
            return;
        }
        toLevel = (temp_level <= MAX_LEVEL) ? temp_level : toLevel;
        if (toLevel > fromLevel) {
            // cancel previous process first
            mDownHandler.removeCallbacks(animateDownImage);
            MainActivity.this.fromLevel = toLevel;
    
            mUpHandler.post(animateUpImage);
        } else {
            // cancel previous process first
            mUpHandler.removeCallbacks(animateUpImage);
            MainActivity.this.fromLevel = toLevel;
    
            mDownHandler.post(animateDownImage);
        }
    }
    }
    

    activity_main.xml

    
    
    
    
        
    
        

    clip_source.xml

    
    
    

    In case of complex HorizontalProgressBar just change cliporientation in clip_source.xml like this,

    android:clipOrientation="horizontal"
    

    You can download complete demo from here.

提交回复
热议问题