Android进度条怎么动态增长,android 自定义View实现进度条增长

现在的app下载的时候是下载的时候使用一个圆然后根据下载的进度进行显示:

自定义个atterx.xml文件:

然后进行layout.xml中调用:

xmlns:android_progress="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical">

android:id="@+id/start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="START" />

android:id="@+id/round_progress_bar"

android:layout_width="60dp"

android:layout_height="60dp"

android_progress:roundColor="@color/skyblue"

android_progress:roundWidth="10dip"

android_progress:roundProgressColor="@color/red" />

然后在自定义一个view的实现类:

public class RoundProgressBar extends View {

private int roundColor;

private float roundWidth;

private int roundProgressColour;

private Paint paint;

private int progress;

public RoundProgressBar(Context context) {

this(context, null);

}

public RoundProgressBar(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

paint = new Paint();

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, R.color.skyblue);

roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);

roundProgressColour = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, R.color.red);

typedArray.recycle();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int centre = getWidth() / 2;

int radius = (int) (centre - roundWidth / 2);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(roundWidth);

paint.setColor(roundColor);

canvas.drawCircle(centre, centre, radius, paint);

paint.setStrokeWidth(roundWidth);

paint.setColor(roundProgressColour);

RectF rectF = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);

paint.setStyle(Paint.Style.STROKE);

canvas.drawArc(rectF, 270, 360 * progress / 100, false, paint);

}

public synchronized int getProgress() {

return progress;

}

public synchronized void setProgress(int progress) {

if (progress < 0) {

throw new IllegalArgumentException("progress not less than 0");

}

if (progress > 100) {

progress = 100;

}

if (progress <= 100) {

this.progress = progress;

postInvalidate();//这句很重要,他表示了每一次进来后都需要进行重绘

}

}

}

在mainactivity中进行数据的调用:

public class MainActivity extends ActionBarActivity {

RoundProgressBar progressBar;

int progress = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.start);

progressBar = (RoundProgressBar) findViewById(R.id.round_progress_bar);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new Thread(new Runnable() {

@Override

public void run() {

while (progress <= 100) {

Log.e("test", progress + "");

progress += 3;

progressBar.setProgress(progress);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

});

}

http://download.csdn.net/detail/xushuangshuang1/8573645这是源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值