一直觉得android的progress不太好看,想改造一下,参考了github上https://github.com/feiyangxiaomi/ProgressWheel/
第一个图是改造前的android自带的progress,后面的三个是改造后的progress,显然改造后的progress更加的灵活,同时也展现的比较美观。具体实现方法非常的简单,这里从界面到程序的介绍流程。
在我们的界面中我们使用ProgressWheel实现后面三个效果(只写一个):
<com.todddavies.components.progressbar.ProgressWheel
android:id="@+id/progressBarThree"
android:layout_width="150dp"
android:layout_height="150dp"
ProgressWheel:text=""
ProgressWheel:textColor="#222222"
ProgressWheel:textSize="14sp"
ProgressWheel:rimColor="#44000000"
ProgressWheel:circleColor="#2E9121"
ProgressWheel:barLength="20dp"
ProgressWheel:barColor="#8000"
ProgressWheel:barWidth="25dp"
ProgressWheel:rimWidth="25dp"
ProgressWheel:spinSpeed="-1dp" />
其中的属性意思,我下面介绍一下,在value/attars.xml 中有定义:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressWheel">
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="barColor" format="color" />
<attr name="rimColor" format="color" />
<attr name="rimWidth" format="dimension" />
<attr name="spinSpeed" format="dimension" />
<attr name="delayMillis" format="integer" />
<attr name="circleColor" format="color" />
<attr name="radius" format="dimension" />
<attr name="barWidth" format="dimension" />
<attr name="barLength" format="dimension" />
<attr name="contourColor" format="color"/>
<attr name="contourSize" format="dimension"/>
</declare-styleable>
</resources>
1.text、textColor、textSize为文字样式设置,位置见上图中的loading所示。
2.barColor、barWidth、barLength为外层的圈,也称bar,颜色、宽度、总共一圈分为多少等份,位置间上图中loading外的那一层。
3.rimColor、rimWidth是在bar上滑块滑动的颜色、宽度,这里没有长度,位置见上图中所示。
4.spinSpeed滑块滑动速度,delayMillis每次滑完一圈的延时,circleColor为progress中心颜色,radius为中心半径。
5.contourColor和ContourSize为bar的轮廓颜色、大小。
Java程序:
main.java程序为:
package com.todddavies.components.progressbar;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* A sample activity showing some of the functions of the progress bar
*/
public class main extends Activity {
boolean running;
ProgressWheel pw_two;
ProgressWheel pw_three;
ProgressWheel pw_four;
//ProgressWheel pw_five;
int progress = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_wheel_activity);
pw_two = (ProgressWheel) findViewById(R.id.progressBarTwo);
pw_three = (ProgressWheel) findViewById(R.id.progressBarThree);
pw_four = (ProgressWheel) findViewById(R.id.progressBarFour);
//pw_five = (ProgressWheel) findViewById(R.id.progressBarFive);
int[] pixels = new int[] { 0xFF2E9121, 0xFF2E9121, 0xFF2E9121,
0xFF2E9121, 0xFF2E9121, 0xFF2E9121, 0xFFFFFFFF, 0xFFFFFFFF};
Bitmap bm = Bitmap.createBitmap(pixels, 8, 1, Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(bm,Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
pw_three.setRimShader(shader);
pw_three.spin();
pw_four.spin();
pw_two.setText("Loading...");
pw_two.spin();
final Runnable r = new Runnable() {
public void run() {
running = true;
while(progress<361) {
pw_two.incrementProgress();
progress++;
try {
Thread.sleep(15);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
running = false;
}
};
Button spin = (Button) findViewById(R.id.btn_spin);
spin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!running) {
if(pw_two.isSpinning) {
pw_two.stopSpinning();
}
pw_two.resetCount();
pw_two.setText("Loading...");
pw_two.spin();
}
}
});
Button increment = (Button) findViewById(R.id.btn_increment);
increment.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!running) {
progress = 0;
pw_two.resetCount();
Thread s = new Thread(r);
s.start();
}
}
});
}
@Override
public void onPause() {
super.onPause();
progress = 361;
pw_two.stopSpinning();
pw_two.resetCount();
pw_two.setText("Click\none of the\nbuttons");
}
}
看着很多,其实实际我们用的时候就用pw_two.spin,其他的是改变背景,如图所示的第三个图形,也还有就是显示加载百分比的效果,这里我没有贴图。
主要使用了ProgressWheel.java
源码地址:https://github.com/feiyangxiaomi/ProgressWheel/