Android 安卓圆形确定进度条
我想创建一个圆形的确定进度条,它在条的中心显示进度。有没有默认的方法来创建这个,或者我必须创建自己的自定义方法 首先将progress\u circle.xml添加到您的res/drawable目录,如下所示:Android 安卓圆形确定进度条,android,material-design,android-progressbar,material-components-android,progress-indicator,Android,Material Design,Android Progressbar,Material Components Android,Progress Indicator,我想创建一个圆形的确定进度条,它在条的中心显示进度。有没有默认的方法来创建这个,或者我必须创建自己的自定义方法 首先将progress\u circle.xml添加到您的res/drawable目录,如下所示: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:dr
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/progress_circular_background"/>
<item>
<shape
android:innerRadiusRatio="3.4"
android:shape="ring"
android:thicknessRatio="6.0" >
<gradient
android:endColor="#ffffffff"
android:startColor="#ffffff"
android:type="sweep"
android:useLevel="true" />
</shape>
</item>
<item>
<rotate
android:drawable="@drawable/progress_particle"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</item>
</layer-list>
我在谷歌上搜索了“progress\u particle.png”和“progress\u circular\u background.png”(带引号),因为android默认的绘图工具不见了。您可能想定制这些,但它们可以帮助您开始
然后在xml布局中:
<ProgressBar
android:id="@+id/timer_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminate="false"
android:max="60"
android:progressDrawable="@drawable/progress_circle" />
我的最大值是60,因为我用它来计时,但你可能有不同的东西
诀窍是你需要使用style=“?android:attr/progressBarStyleHorizontal”,即使这是一个循环过程。我在我的博客上写了详细的例子,你也可以在这里找到完整的源代码和解释 下面是我如何在没有任何库的纯代码中制作圆形progressbar,其中包含圆圈内的百分比 首先创建一个名为
circular.xml的可绘制文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/secondaryProgress">
<shape
android:innerRadiusRatio="6"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true">
<gradient
android:centerColor="#999999"
android:endColor="#999999"
android:startColor="#999999"
android:type="sweep" />
</shape>
</item>
<item android:id="@android:id/progress">
<rotate
android:fromDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270">
<shape
android:innerRadiusRatio="6"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true">
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<gradient
android:centerColor="#00FF00"
android:endColor="#00FF00"
android:startColor="#00FF00"
android:type="sweep" />
</shape>
</rotate>
</item>
</layer-list>
在activity_main.xml
中,我使用了一个白色背景的圆形图像来显示百分比周围的白色背景。图为:
您可以更改此图像的颜色,以围绕百分比文本设置自定义颜色
现在,最后将以下代码添加到MainActivity.java
:
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.DecelerateInterpolator;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int pStatus = 0;
private Handler handler = new Handler();
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.circular);
final ProgressBar mProgress = (ProgressBar) findViewById(R.id.circularProgressbar);
mProgress.setProgress(0); // Main Progress
mProgress.setSecondaryProgress(100); // Secondary Progress
mProgress.setMax(100); // Maximum Progress
mProgress.setProgressDrawable(drawable);
/* ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
animation.setDuration(50000);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();*/
tv = (TextView) findViewById(R.id.tv);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (pStatus < 100) {
pStatus += 1;
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mProgress.setProgress(pStatus);
tv.setText(pStatus + "%");
}
});
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(8); //thread will take approx 1.5 seconds to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
导入android.content.res.Resources;
导入android.graphics.drawable.drawable;
导入android.os.Handler;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.animation.decreater插值器;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类MainActivity扩展了AppCompatActivity{
int pStatus=0;
私有处理程序=新处理程序();
文本视图电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res=getResources();
可拉伸可拉伸=可拉伸(R.可拉伸.圆形);
最终进度条mProgress=(进度条)findViewById(R.id.circularProgressbar);
mProgress.setProgress(0);//主进度
mProgress.setSecondaryProgress(100);//二次进度
mProgress.setMax(100);//最大进度
mpprogress.setProgressDrawable(可绘制);
/*ObjectAnimator animation=ObjectAnimator.ofInt(mProgress,“progress”,0100);
动画。设定持续时间(50000);
setInterpolator(新减速器Interpolator());
animation.start()*/
tv=(TextView)findviewbyd(R.id.tv);
新线程(newrunnable()){
@凌驾
公开募捐{
//TODO自动生成的方法存根
而(pStatus<100){
pStatus+=1;
handler.post(新的Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
mpprogress.setProgress(pStatus);
tv.setText(pStatus+“%”);
}
});
试一试{
//睡眠200毫秒。
//只是为了慢慢地显示进度
线程。睡眠(8);//线程大约需要1.5秒才能完成
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}).start();
}
}
如果您想制作水平progressbar,请遵循此链接,它有许多有价值的示例,其中包含源代码:
对于问题的确定部分,现在支持确定循环进度条:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
有关更多信息,请参阅
其他答案可能有助于在中心插入进度标签。确定循环进度指示器示例
参考资料
很可能是这个问题的重复:@AlexBottoni不,这是关于不确定进度条的问题。非常不同progress_circle.xml是哪种类型的可绘制文件?您提供的不是一个完整的文件,它需要一个上层对象作为钢环形状(不是进度粒子)的一部分,从-90度(12小时)开始填充?我有一个小问题。有可绘制的ldpi、可绘制的hdpi等。我应该将此xml使用哪个文件夹?@bagusflyer将其放入名为“可绘制”的文件夹中,xml文件是独立于dpi的资源。style=“?android:attr/progressBarStyleHorizontal”
不是必需的,android:indterminatenly=“false”
是。为什么progressbar外有这么多额外的间距?我该如何将android:innerRadiusRatio=“2.5”
从6
减少到2.5
将android:innerRadiusRatio=“6”更改为android:innerRadiusRatio=“2.35”
它非常完美,几乎没有额外的空间任何人知道如何实现它。。。文档没有帮助。只是无法让它工作…@whoadityanawandar我自己没有使用过这些工具,但是如果通过实现你的意思是让进度条在代码中确定,文档建议:int progress=getLoadingProgress()indicator.setProgressCompat(progress,true)
谢谢,尽管这看起来还不完整,我的意思是你如何设置它的计时器和所有。。。
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content" />