package com.demo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class ProgressBar extends View implements Runnable
{
private int offset;
private int count;
private int mWidth;
private Drawable mBar;
private int len;
public ProgressBar(Context context)
{
super(context);
initProgressBar(context);
}
public ProgressBar(Context context, AttributeSet attrs)
{
super(context, attrs);
initProgressBar(context);
}
private void initProgressBar(Context context)
{
mBar = context.getResources().getDrawable(R.drawable.progressbar);
mWidth = mBar.getIntrinsicWidth();
new Thread(this).start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(200, 30);
count = 2 * getMeasuredWidth() / mWidth;
offset = len = count * mWidth - getMeasuredWidth();
}
@Override
protected void onDraw(Canvas canvas)
{
for (int i = 0; i < count; i++)
{
mBar.setBounds(i * mWidth - offset, 0, (i + 1) * mWidth - offset,
30);
mBar.draw(canvas);
}
offset = offset > 0 ? offset - 3 : len;
}
@Override
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
postInvalidate();
}
}
}
自定义ProgressBar
最新推荐文章于 2024-05-07 18:00:19 发布