我想创建一个Circluar Determinate ProgressBar,它显示了Bar中心的Progress.是否有任何默认方式来创建它,或者我将创建自己的自定义方法.
Par*_*dik 47
我在我的博客demonuts.com上写了关于Android循环进度条的详细示例.您还可以在那里喜欢完整的源代码和解释.
这是我在没有任何库的纯代码中使用圆圈内百分比制作圆形进度条的方法.
首先创建一个名为drawable的文件 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>
Run Code Online (Sandbox Code Playgroud)
现在在您activity_main.xml
添加以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dialog"
tools:context="com.example.parsaniahardik.progressanimation.MainActivity">
<ProgressBar
android:id="@+id/circularProgressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="250dp"
android:layout_height="250dp"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:layout_centerInParent="true"
android:progressDrawable="@drawable/circular"
android:secondaryProgress="100"
/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:background="@drawable/whitecircle"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/tv"
android:layout_width="250dp"
android:layout_height="250dp"
android:gravity="center"
android:text="25%"
android:layout_centerInParent="true"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想制作水平进度条,请点击此链接,它有很多有价值的例子,包括源代码:http:
//www.skholingua.com/android-basic/user-interface/form-widgets/progressbar
小智 38
首先将progress_circle.xml添加到res/drawable目录,如下所示:
<?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>
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索的图像搜索了"progress_particle.png"和"progress_circular_background.png"(带引号),因为那些缺少的android默认drawables.您可能希望自定义这些,但他们会帮您入门.
然后在你的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" />
Run Code Online (Sandbox Code Playgroud)
我的最大值是60,因为我使用的是秒计时器,但你可能会有不同的东西.
诀窍是你需要使用style ="?android:attr/progressBarStyleHorizontal",即使它是循环进度.
确定循环进度指示器的示例
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="75"
app:indicatorColor="#FF0000"
app:indicatorSize="100dp"
app:trackColor="#D3D3D3"
app:trackThickness="10dp" />
Run Code Online (Sandbox Code Playgroud)
参考
对于问题的确定部分,材料设计库现在支持确定的圆形进度条:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此处。
其他答案可能有助于如何在中心插入进度标签。
归档时间: |
|
查看次数: |
39371 次 |
最近记录: |