相当简单,直接贴
先看主界面布局activity_custom_progress_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <Button android:text="自定义颜色进度条" android:id="@+id/color" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="自左向右的进度条" android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
ProgressBarMain
package com.example.progressdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ProgressBarMain extends Activity implements OnClickListener { /** * 按钮点击进入自定义颜色的进度条页面 */ private Button color; /** * 按钮点击进入左方向进度条页面 */ private Button left; /** * 按钮点击进入右方向进度条页面 */ private Button right; /** * 按钮点击进入不定进度条页面 */ private Button indeter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_progress_main); initProperty(); } private void initProperty() { color = (Button) findViewById(R.id.color); left = (Button) findViewById(R.id.left); color.setOnClickListener(this); left.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.color: startActivity(new Intent(this, ProgressBar_Color.class)); break; case R.id.left: startActivity(new Intent(this, ProgressBar_Left.class)); break; default: break; } } }
接下来看ProgressBar_Color
package com.example.progressdemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ProgressBar; public class ProgressBar_Color extends Activity { private ProgressBar mColor = null; private int mCount = 0; private final int HANDLER_LEFT = 1; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == HANDLER_LEFT) { finish(); } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_progress_mypage_color); showIndeterDialog(); } private void showIndeterDialog() { mCount = 0; mColor = (ProgressBar) findViewById(R.id.progress_horizontal_color); mColor.setMax(100); mColor.setProgress(0); //)就是滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人“我正在工作”,但不能提示工作进度到哪个阶段 mColor.setIndeterminate(false); new Thread() { public void run() { try { while (mCount <= 100) { mColor.setProgress(mCount++); Thread.sleep(100); } if (mCount > 100) { mHandler.sendEmptyMessage(HANDLER_LEFT); } } catch (Exception ex) { } } }.start(); } }
看activity_custom_progress_mypage_color.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <ProgressBar android:id="@+id/progress_horizontal_color" style="@style/progressBarHorizontal_color" android:layout_width="200dip" android:layout_height="10dip" android:max="100" android:progress="41" /> </LinearLayout>
styles.xml
<style name="progressBarHorizontal_color" parent="android:Widget.ProgressBar.Horizontal"> <item name="android:indeterminateOnly">false</item> <item name="android:progressDrawable">@drawable/progress_color_horizontal</item> <item name="android:minHeight">5dip</item> <item name="android:maxHeight">5dip</item> </style>
接下来看drawable
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 最下边层 angle表示方向角度,0为从左向又,然后逆时针转,90时候表示从下往上 --> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:angle="90" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:startColor="#ff9d9e9d" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:angle="90" android:centerColor="#ffBBFFBB" android:centerY="0.75" android:endColor="#ff00DB00" android:startColor="#ff00DB00" /> </shape> </clip> </item> </layer-list>
----------------------------------------------第一个效果实现了,下面是同样效果的第二种方法----------------------------------------------------
<style name="progressBarHorizontal" parent="android:Widget.ProgressBar.Horizontal"> <item name="android:indeterminateOnly">false</item> <item name="android:progressDrawable">@drawable/progress_horizontal</item> <item name="android:minHeight">5dip</item> <item name="android:maxHeight">5dip</item> </style>
接下来看drawable
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background" android:drawable="@drawable/progressbar_back"> </item> <item android:id="@android:id/progress" android:drawable="@drawable/progressbar_indeter1"> </item> </layer-list>
时间: 2024-10-30 01:39:08