android 中带有背景的自定义进度条
Custom Progress bar in android with Background
如何制作这样的进度条
是复合视图(TextView + CustomProgressBar)
在这里我找到了我的答案
首先你需要添加 xml 名称为 progress_bar_background.xml
的可绘制资源
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="18dp" />
<stroke android:color="@color/transparent" android:width="6dp" />
</shape>
现在制作另一个 xml 名称为 curved_progress_bar.xml
的资源文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="18dp" />
<solid android:color="@color/transparent" />
<!-- <stroke android:color="@color/transparent" android:width="6dp" />-->
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="18dp" />
<solid android:color="#FFFFFF" />
</shape>
</clip>
</item>
</layer-list>
最后在 Splash activity xml 文件中粘贴此代码作为 进度条
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="22dp"
android:textStyle="bold"
android:fontFamily="@font/symbioarltmedium"
android:layout_height="wrap_content"
android:text="80%"/>
<ProgressBar
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView"
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="300dp"
android:layout_height="50dp"
android:indeterminate="false"
android:max="100"
android:padding="5dp"
android:progress="1"
android:background="@drawable/progress_bar_background"
android:progressDrawable="@drawable/curved_progress_bar"
/>
这里是 Java
private ProgressBar progressBar;
TextView textView;
private int progressStatus = 0;
private Handler handler = new Handler();
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"%");
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}).start();
并使用这个颜色
<color name="transparent">#27EEECEC</color>
如何制作这样的进度条
是复合视图(TextView + CustomProgressBar)
在这里我找到了我的答案
首先你需要添加 xml 名称为 progress_bar_background.xml
的可绘制资源 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="18dp" />
<stroke android:color="@color/transparent" android:width="6dp" />
</shape>
现在制作另一个 xml 名称为 curved_progress_bar.xml
的资源文件<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="18dp" />
<solid android:color="@color/transparent" />
<!-- <stroke android:color="@color/transparent" android:width="6dp" />-->
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="18dp" />
<solid android:color="#FFFFFF" />
</shape>
</clip>
</item>
</layer-list>
最后在 Splash activity xml 文件中粘贴此代码作为 进度条
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="22dp"
android:textStyle="bold"
android:fontFamily="@font/symbioarltmedium"
android:layout_height="wrap_content"
android:text="80%"/>
<ProgressBar
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView"
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="300dp"
android:layout_height="50dp"
android:indeterminate="false"
android:max="100"
android:padding="5dp"
android:progress="1"
android:background="@drawable/progress_bar_background"
android:progressDrawable="@drawable/curved_progress_bar"
/>
这里是 Java
private ProgressBar progressBar;
TextView textView;
private int progressStatus = 0;
private Handler handler = new Handler();
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"%");
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}).start();
并使用这个颜色
<color name="transparent">#27EEECEC</color>