1.导入项目
build.gradle:
allprojects {
repositories {
jcenter()
maven { url "https://jcenter.bintray.com" }
}
}
dependencies {
// other repos ...
compile 'ch.halcyon:squareprogressbar:1.6.4'
}
2.使用范例
XML:
<ch.halcyon.squareprogressbar.SquareProgressBar
android:id="@+id/sprogressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="20dp" >
</ch.halcyon.squareprogressbar.SquareProgressBar>
代码中使用:
SquareProgressBar squareProgressBar = findViewById(R.id.sprogressbar);
squareProgressBar.setImage(R.drawable.example);
squareProgressBar.setProgress(50.0);
3.其他的基本方法
换颜色
如果您不喜欢square-progressbar的默认颜色,则可以通过不同的方式设置新的颜色。
- 一个Android全息颜色:
squareProgressBar.setHoloColor(color.holo_blue_bright);(凡色,以引用android.R.color) - 六角颜色:
squareProgressBar.setColor("#C9C9C9"); - RGB颜色:
squareProgressBar.setColorRGB(154, 11, 41);
宽度
如果要更改进度条的宽度,请使用以下方法:
squareProgressBar.setWidth(20);
不透明度
对于版本1.4.0,现在有两种不同的方式来处理不透明度的工作方式。
- 当进度变高时,图像会越来越多。
- 当进度变高时,图像会淡出。
设置此方法有两种方法:squareProgressBar.setOpacity(true, false);或squareProgressBar.setOpacity(true);
图像消失
如果您希望图像越来越淡,请使用 squareProgressBar.setOpacity(true, true);
显示进度(在图片中间显示当前进度 用数字显示)
将当前进度显示为图像中的文本有很多不同的可能性。基本设置需要两行代码:
squareProgressBar.showProgress(true);
squareProgressBar.setPercentStyle(new PercentStyle(Align.CENTER, 190, true));
PercentStyle
您可以在此对象上设置更多设置,然后在构造函数中显示。
替换'%'
您可以设置自己的替代文本,而不是默认的%。
squareProgressBar.showProgress(true);
PercentStyle percentStyle = new PercentStyle(Align.CENTER, 150, true);
percentStyle.setCustomText(".-");
squareProgressBar.setPercentStyle(percentStyle);
文字颜色
您也可以为进度文本设置不同的颜色:
squareProgressBar.showProgress(true);
PercentStyle percentStyle = new PercentStyle(Align.CENTER, 150, true);
percentStyle.setTextColor(Color.parseColor("#C9C9C9"));
squareProgressBar.setPercentStyle(percentStyle);
灰度
您也可以将图像的样式更改为灰度,只需调用以下方法:
squareProgressBar.setImageGrayscale(true);
最外层是否有轮廓
有一种方法可以显示一个漂亮的轮廓,使整个元素成为一个框架。只需使用以下方法:
squareProgressBar.drawOutline(true);
STARTLINE
也许你想显示起始线(元素开始绘制进度的地方):
squareProgressBar.drawStartline(true);
ScaleType
如果您使用大图片,可能会发生进度条不再适合图像。在这种情况下,尝试使用图像scaletype:
squareProgressBar.setImageScaleType(ScaleType.MATRIX);
进度条到达100%的时候清除它
当进度达到100%时,有可能让进度条(不是图像)消失。只需使用以下方法:
squareProgressBar.setClearOnHundred(true);
设置一个drawable
您也可以将drawable设置为图像:
squareProgressBar.setImageDrawable(drawable);
设置位图
您也可以将代码生成的位图设置为图像:
squareProgressBar.setImageBitmap(bmp);
中心线
有一种方法可以在路径的中心显示一条漂亮的线条,该线条必须围绕图像进行。只需使用以下方法:
squareProgressBar.drawCenterline(true);
不定
如果要显示进度条的不确定样式,请使用以下方法。当您不知道加载需要多长时间时,这非常有用。
squareProgressBar.setIndeterminate(true);
本文介绍了SquareProgressBar的导入与使用方法,包括XML布局和代码中设置,以及各种自定义属性如颜色、宽度、不透明度、进度显示等。还详细讲解了如何实现图像淡出、显示进度文本、设置灰度、轮廓、起始线、缩放类型、清除进度条、设置drawable和位图、中心线和不确定样式等功能。

564

被折叠的 条评论
为什么被折叠?



