开源项目ProgressWheel为我们提供了多样化的圆形滚动条,本篇带领读者利用它来实现自定义的滚动条。在官方demo中,一屏显示了好几种风格,而本篇只介绍一种风格,就是居中显示,透明布局。 而且使用static进行了封装,启动和关闭都只需要一行代码即可完成。
- 开源项目ProgressWheel地址:
- 开源项目ProgressWheel效果图:
- 自定义滚动条(透明效果)的实现:
1). xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_progress_wheel"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
>
<com.todddavies.components.progressbar.ProgressWheel
android:id="@+id/pw_spinner"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
ProgressWheel:barColor="#0097D6"
ProgressWheel:barLength="100dp"
ProgressWheel:barWidth="5dp"
ProgressWheel:rimColor="#330097D6"
ProgressWheel:rimWidth="10dp"
ProgressWheel:text="Loading..."
ProgressWheel:textColor="#222"
ProgressWheel:contourColor="#330097D6"
ProgressWheel:textSize="14sp" />
</LinearLayout>
2). style:
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
3). java代码:
public class LoadingCommentDialog {
private static Dialog mDialog = null;
private static ProgressWheel mProgressWheel = null;
public static void showCommentDialog(Context context, String text) {
closeCommentDialog(context);
WindowManager m = ((Activity) context).getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
mDialog = new Dialog(context, R.style.Dialog);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View layout = inflater.inflate(R.layout.layout_progress_wheel, null);
mDialog.setContentView(layout);
mProgressWheel = (ProgressWheel) layout.findViewById(R.id.pw_spinner);
mProgressWheel.setText(text);
Window dialogWindow = mDialog.getWindow();//
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.gravity = Gravity.CENTER;
// lp.x = 20; // 新位置X坐标
// lp.y = 60; // 新位置Y坐标
lp.width = d.getWidth() - 20; // 宽度
// lp.height = (int) (d.getHeight()*0.4); // 高度
dialogWindow.setAttributes(lp);
mProgressWheel.spin();
mDialog.show();
}
public static void closeCommentDialog(Context context) {
if (mProgressWheel!=null) {
mProgressWheel.stopSpinning();
mProgressWheel = null;
}
if (mDialog!=null) {
mDialog.dismiss();
mDialog = null;
}
}
}
4). 调用方法:
打开:
LoadingCommentDialog.showCommentDialog(mContext,mContext.getString(R.string.loading)); //这里可以自定义圆形中的文字。
//YourFouction() //这里,是开始你的线程,或者异步请求,或者耗时的操作...
关闭:
LoadingCommentDialog.closeCommentDialog(mContext);
最后,附上自定义滚动条(透明效果)的效果图:
开源的力量是无穷的!
时间: 2024-10-14 07:06:18