带分割的Android环形
我需要创建带有间隙的圆形 我正在创建一个表示时钟的进度条。12个间隔,每个间隔分配到小时 这正是我想要实现的目标。(死星上的外环) 以下是我目前的代码: 在带分割的Android环形,android,android-progressbar,android-shape,Android,Android Progressbar,Android Shape,我需要创建带有间隙的圆形 我正在创建一个表示时钟的进度条。12个间隔,每个间隔分配到小时 这正是我想要实现的目标。(死星上的外环) 以下是我目前的代码: 在activity.xml中 <ProgressBar android:id="@+id/progressBar" android:layout_width="200dp" android:layout_height="200dp" android:indeterminate="false" an
activity.xml中
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminate="false"
android:progressDrawable="@drawable/circular_progress_bar"
android:background="@drawable/circle_shape"
style="?android:attr/progressBarStyleHorizontal"
android:max="500"
android:progress="65"
android:layout_marginBottom="165dp"
android:layout_above="@+id/button5"
android:layout_centerHorizontal="true" />
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="#CCC" />
<stroke
android:dashGap="10px"
android:dashWidth="10px"
android:width="1dp"
android:color="#ababb2" />
</shape>
在circle\u shape.xml中
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminate="false"
android:progressDrawable="@drawable/circular_progress_bar"
android:background="@drawable/circle_shape"
style="?android:attr/progressBarStyleHorizontal"
android:max="500"
android:progress="65"
android:layout_marginBottom="165dp"
android:layout_above="@+id/button5"
android:layout_centerHorizontal="true" />
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thickness="4dp"
android:useLevel="false">
<solid android:color="#CCC" />
<stroke
android:dashGap="10px"
android:dashWidth="10px"
android:width="1dp"
android:color="#ababb2" />
</shape>
我如何分开我的形状,使它看起来像这样?我的路径正确吗?过去我必须创建一个自定义进度条。
我不确定你的解决方案,所以我不会对此发表评论。
我就是这样处理这个问题的:
我创建了一个覆盖LinearLayout的自定义类(我需要向其中添加更多视图,但您可以覆盖任何其他视图)
然后我覆盖onDraw,在画布上画一个拱门:
ArchProgressBar.java
public class ArchProgressBar extends LinearLayout {
private static final float START_ANGLE = 130;
private static final float ARCH_LENGTH = 50;
public ArchProgressBar(Context context) {
super(context);
init(context);
}
public ArchProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.setWillNotDraw(false);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.arch_progress_bar, this, true);
this.postInvalidate();
}
@Override
public void onDraw(Canvas canvas) {
float middleWidth = canvas.getWidth() / 2;
float middleHeight = canvas.getHeight() / 2;
float left = middleWidth - 105 * getResources().getDisplayMetrics().density;
float top = middleHeight - 90 * getResources().getDisplayMetrics().density;
float right = middleWidth + 105 * getResources().getDisplayMetrics().density;
float bottom = middleHeight + 120 * getResources().getDisplayMetrics().density;
Paint mPaintBackground = new Paint();
mPaintBackground.setAntiAlias(true);
mPaintBackground.setStyle(Paint.Style.STROKE);
mPaintBackground.setStrokeWidth(35);
mPaintBackground.setColor(Color.BLACK);
RectF mRectF = new RectF(left, top, right, bottom);
// draw background line
canvas.drawArc(mRectF, START_ANGLE, ARCH_LENGTH, false, mPaintBackground);
canvas.drawArc(mRectF, START_ANGLE + ARCH_LENGTH + 10, ARCH_LENGTH, false, mPaintBackground);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = View.MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, Math.max(800, heightMeasureSpec));
}
}
arch_progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
然后,您可以将其添加到任何需要的xml中,如下所示:
<com.training.archprogress.ArchProgressBar
android:id="@+id/result_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
您可以只绘制12个不同的分段,使拱门略小于分段的大小。
然后,只需在进度更新时绘制所需的分段数
我在github中为您放置了一个包含此解决方案的项目:
你能详细说明一下吗。什么去哪里。我对安卓完全陌生。我刚决定通过一些小项目来学习它,这是我第一次重新创建zooper小部件。我添加了一个更详细的解释,包括完整的类和一个示例应用程序的github链接,但我不确定为什么有人否决了我。。。但也许有更好的解决方案来解决这个问题这个自定义视图不能正常工作。。。我把它完全按照你描述的那样贴在布局上,它没有画任何东西。我将尝试看看它有什么问题。现在我明白了,只有在宽度和高度都匹配的情况下,它才有效。。。