我尝试创建一个圆角的方线进度条来绘制图像周围的进度。
到目前为止,我有以下XML来定义我的圆角方块:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="6dp"
android:color="@android:color/holo_green_light" />
<padding
android:left="5dp"
android:right="5dp"
android:bottom="5dp"
android:top="5dp" />
<corners android:radius="50dp" />
</shape>
我知道这个解决方案:https://github.com/mrwonderman/android-square-progressbar但我并不感兴趣,因为效果不是我想要的效果。
我试图在圆角方形线上创建一个普通圆圈,并尝试将两者合并到PorterDuff,但到目前为止我还无法创建进度条效果。绘制一个圆形的馅饼来绘制进度。
我还尝试创建圆角正方形,以防XML膨胀被视为普通图像,并且在PorterDuff合并期间考虑了所有像素。但结果相同。
答案 0 :(得分:3)
尝试这个简单的自定义类:
class V extends View {
Path path = new Path();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float length;
float[] intervals = {0, 0};
public V(Context context) {
super(context);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
path.reset();
RectF rect = new RectF(0, 0, w, h);
float inset = paint.getStrokeWidth();
rect.inset(inset, inset);
path.addRoundRect(rect, 100, 100, Path.Direction.CW);
length = new PathMeasure(path, false).getLength();
intervals[0] = intervals[1] = length;
PathEffect effect = new DashPathEffect(intervals, length);
paint.setPathEffect(effect);
}
public void setProgress(int progress) {
PathEffect effect = new DashPathEffect(intervals, length - length * progress / 100);
paint.setPathEffect(effect);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
}
测试代码(放在Activity#onCreate
内):
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
SeekBar sb = new SeekBar(this);
ll.addView(sb);
final V v = new V(this);
ll.addView(v);
setContentView(ll);
SeekBar.OnSeekBarChangeListener sbcl = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
v.setProgress(progress);
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override public void onStopTrackingTouch(SeekBar seekBar) {}
};
sb.setOnSeekBarChangeListener(sbcl);