1、首先在布局文件里面写普通的progressbar
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="693px"
android:layout_height="10px"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progress_shape"/>
注意的style写成horizontal,才是横向的,indeterminate是否模糊进度,为false就有精确的进度了。
最重要的是progressDrawable需要自定义drawable,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置ProgressBar背景色-->
<item android:id="@android:id/background">
<shape>
<solid android:color="#f0f0f0" />
</shape>
</item>
<!--设置ProgressBar进度条颜色-->
<item android:id="@android:id/progress">
<scale
android:drawable="@drawable/progress_color"
android:scaleWidth="100%" />
</item>
</layer-list>
引用的进度条的progress_color
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="180"
android:endColor="#2F74FB"
android:startColor="#00DBFF" />
</shape>
activity里面就可以模拟进度了。
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int i = 0;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
i++;
if (i == 100) {
handler.removeCallbacks(this);
}
progressBar.setProgress(i);
handler.postDelayed(this, 100);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
progressBar = findViewById(R.id.progressBar);
handler.postDelayed(runnable, 100);
}
}
显示效果:录下来转个烂gif
1515047752927mzgggg.gif
网友评论