Android-圆角正方形线进度栏
作者:互联网
我正在尝试创建一个圆角的正方形线进度条,以围绕图像绘制进度.
到目前为止,我有以下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合并过程中考虑了所有像素.但结果相同.
解决方法:
试试这个简单的自定义类:
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);
标签:line,android-progressbar,rounded-corners,android 来源: https://codeday.me/bug/20191118/2029378.html