1.定义图片
@drawable/progress_rect1
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<gradient
android:angle="90"
android:endColor="#FFFFFFFF"
android:startColor="#FFFFFFDE"
android:type="linear" />
<stroke
android:dashWidth="2px"
android:width="2px"
android:color="#FF616D73" />
<corners android:radius="12dp" />
</shape>
@drawable/progress_rect2
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FF62CCDE" />
<gradient
android:angle="90"
android:endColor="#FF98D7E8"
android:startColor="#FF65CADE"
android:type="linear" />
<stroke
android:dashWidth="2px"
android:width="2px"
android:color="#FF616D73" />
<corners android:radius="12dp" />
</shape>
2.xml布局文件引用
<com.huawei.roadmng.login.ProcessView
android:id="@+id/loadingProcess"
android:layout_width="500dip"
android:layout_height="32dip"
android:layout_marginTop="25dip"
android:background="@drawable/progress_rect1"
android:src="@drawable/progress_rect2" />
3.progressview处理
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
public class ProcessView extends ImageButton
{
/**
* 用于绘制进度条
*/
private static final int PERCENT = 100;
/**
* 进度条的进度百分比
*/
private int precent;
/**
* 进度条的宽度
*/
private int mWidth;
/**
* 进度条的高度
*/
private int mHeight;
/**
* 背景
*/
private Drawable mBackGround;
/**
* 前景
*/
private Drawable mImg;
/** <默认构造函数>
* @param context Context
*/
public ProcessView(Context context)
{
super(context);
}
/** <默认构造函数>
* @param context Context
* @param attrs AttributeSet
*/
public ProcessView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
/**
* {@inheritDoc}
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int height = View.MeasureSpec.getSize(heightMeasureSpec);
int width = View.MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, height);
}
/**
* {@inheritDoc}
*/
public void onDraw(Canvas canvas)
{
if (mBackGround == null)
{
mImg = this.getDrawable();
mBackGround = this.getBackground();
}
if (mWidth == 0 || mHeight == 0)
{
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}
mBackGround.draw(canvas);
canvas.clipRect(0, 0, mWidth * precent / PERCENT, mHeight);
mImg.draw(canvas);
// canvas.clipRect(0, 0, mWidth, mHeight);
}
/**
* {@inheritDoc}
*/
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
mImg = this.getDrawable();
mBackGround = this.getBackground();
}
/**
* 设置进度
* @param curPrecent int
* @see [类、类#方法、类#成员]
*/
public void setProgress(int curPrecent)
{
precent = curPrecent < 0 ? 0 : curPrecent > 100 ? 100 : curPrecent;
invalidate();
}
}