自定义ProgressBar android

问题描述

如何在Android上执行进度条? 以及如何管理示例设置梯度呢?

enter image description here

解决方法

MainActivity.java

    public class MainActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rect"
android:gravity="center"
   >


    <com.example.HorizontalDottedProgress
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></com.example.HorizontalDottedProgress>


</LinearLayout>

Horizo​​ntalDottedProgress.java

    public class HorizontalDottedProgress extends View{
//actual dot radius
private int mDotRadius = 5;

//Bounced Dot Radius
private int mBounceDotRadius = 8;

//to get identified in which position dot has to bounce
private int  mDotPosition;

//specify how many dots you need in a progressbar
private int mDotAmount = 10;

public HorizontalDottedProgress(Context context) {
    super(context);
}

public HorizontalDottedProgress(Context context,AttributeSet attrs) {
    super(context,attrs);
}

public HorizontalDottedProgress(Context context,AttributeSet attrs,int defStyleAttr) {
    super(context,attrs,defStyleAttr);
}

//Method to draw your customized dot on the canvas
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();

    //set the color for the dot that you want to draw
    paint.setColor(Color.parseColor("#fd583f"));

    //function to create dot
    createDot(canvas,paint);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    //Animation called when attaching to the window,i.e to your screen
    startAnimation();
}

private void createDot(Canvas canvas,Paint paint) {

    //here i have setted progress bar with 10 dots,so repeat and wnen i = mDotPosition  then increase the radius of dot i.e mBounceDotRadius
        for(int i = 0; i < mDotAmount; i++ ){
            if(i == mDotPosition){
                canvas.drawCircle(10+(i*20),mBounceDotRadius,paint);
            }else {
                canvas.drawCircle(10+(i*20),mDotRadius,paint);
            }
        }


}

@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    int width;
    int height;

    //calculate the view width
    int calculatedWidth = (20*9);

    width = calculatedWidth;
    height = (mBounceDotRadius*2);



    //MUST CALL THIS
    setMeasuredDimension(width,height);
}

private void startAnimation() {
    BounceAnimation bounceAnimation = new BounceAnimation();
    bounceAnimation.setDuration(100);
    bounceAnimation.setRepeatCount(Animation.INFINITE);
    bounceAnimation.setInterpolator(new LinearInterpolator());
    bounceAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            mDotPosition++;
            //when mDotPosition == mDotAmount,then start again applying animation from 0th positon,i.e  mDotPosition = 0;
            if (mDotPosition == mDotAmount) {
                mDotPosition = 0;
            }
            Log.d("INFOMETHOD","----On Animation Repeat----");

        }
    });
    startAnimation(bounceAnimation);
}


private class BounceAnimation extends Animation {
    @Override
    protected void applyTransformation(float interpolatedTime,Transformation t) {
        super.applyTransformation(interpolatedTime,t);
        //call invalidate to redraw your view againg.
        invalidate();
    }
}
}

2 =使用其他库

'com.github.igortrncic.dotted-progress-bar:library:1.0.0' 

为inactiveDot和activeDot使用颜色或可绘制对象。

 <com.trncic.library.DottedProgressBar
  android:id="@+id/progress"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:padding="30dp"
  app:activeDot="@drawable/active_dot"
  app:dotSize="29dp"
  app:inactiveDot="@drawable/inactive_dot"
  app:jumpingSpeed="670"
  app:spacing="15dp" />

活动中:

DottedProgressBar progressBar = (DottedProgressBar) findViewById(R.id.progress);
 progressBar.startProgress();
 progressBar.stopProgress();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...