# 常用控件

## CircularBar - 圆形进度条

CircularBar是一个特殊动画的圆形进度条, 常用于提示用户等待网络/io等结果. 效果如下:

![CircularBar](/files/-LsLm8FuOm5hcpuWA539)

CircularBar的用法很简单, 有3个可配置选项:

```
<declare-styleable name="CircularBar">
    <attr name="circularColor" format="color" /> // 圆弧颜色
    <attr name="circularBackgroundColor" format="color" /> // 圆弧背景色
    <attr name="circularWidth" format="dimension" /> // 圆弧宽度
</declare-styleable>
```

```
<com.corelibs.views.CircularBar
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:circularWidth="5dp"
    app:circularBackgroundColor="#ccc"
    app:circularColor="#FFFF0000"/>
```

也可通过代码设置:

```
bar.setFrontColor(0xFFFF0000)
bar.setStrokeWidth(DisplayUtil.dip2px(context, 5));
bar.setBackgroundColor(0xFFCCCCCC)
```

需要注意的是, 默认情况下, 无需对CircularBar做任何处理, 甚至无需在java里将其声明出来. 但是如果一旦要反复的控制CircularBar的显示隐藏, 就需要调用CircularBar的`startAnimation()`和`stopAnimation()`来控制动画的启动与停止. 这是为什么呢? 由于CircularBar内部使用的是ObjectAnimator, 并且是反转的无限动画. 无限动画如果不在适当的时候停止动画, 很容易造成context的内存泄漏. CircularBar会在`onAttachedToWindow()`中启动动画, `onDetachedFromWindow()`中停止动画, 因此大部分情况下无需处理动画的停止.

## LoadingDialog - BaseActivity中默认的加载框

LoadingDialog是BaseActivity中默认的加载框, 长得跟下面一样:

![LoadingDialog](/files/-LsLm8FwUkzACiu-TOmd)![LoadingDialog1](/files/-LsLm8FyKKMMFqhTlEsL)![LoadingDialog2](/files/-LsLm8G-zWs7l6BAMTnm)

LoadingDialog默认是没有文字的, 可以通过`loadingDialog.setMessage()`来设置String或者resource的文字消息. 可以看到LoadingDialog中使用了CircularBar, LoadingDialog对CircularBar的动画做了以处理避免内存泄漏的发生. 使用方法:

```
LoadingDialog loadingDialog = new LoadingDialog(this);
// LoadingDialog loadingDialog = new LoadingDialog(this, R.string.hint);

loadingDialog.setMessage(R.string.hint);

loadingDialog.show();
loadingDialog.dismiss();
```

## MaterialDialog - Material风格的对话框

项目中推荐默认使用MaterialDialog, 而无需跟着UI设计自己编写一个Dialog, 重复工作太多. MaterialDialog的用法比较简单, 支持的布局也很多, 具体可以参考 [GitHub](https://github.com/afollestad/material-dialogs) .


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryan-8.gitbook.io/android-architecture-journey/widgets/common_widget.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
