一个自动义的进度条
首先在”drawable“文件夹下添加两照用于进度条的图片,progress_bar_long.png,progress_bar.png
添加一个xml文件(progress_horizontal.xml)如下:
?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> ??? ??? <item android:id="@android:id/background"> ??????? <shape> ??????????? <corners android:radius="3dip" /> ??????????? <gradient ??????????????????? android:startColor="#88ffffff" ??????????????????? android:centerColor="#889d9e9d" ??????????????????? android:centerY="0.75" ??????????????????? android:endColor="#88747674" ??????????????????? android:angle="270" ??????????? /> ??????? </shape> ??? </item> ??? ??? <item android:id="@android:id/secondaryProgress"> ??????? <clip> ??????????? <shape> ??????????????? <corners android:radius="3dip" /> ??????????????? <gradient ??????????????????????? android:startColor="#80ffd300" ??????????????????????? android:centerColor="#80ffb600" ??????????????????????? android:centerY="0.75" ??????????????????????? android:endColor="#a0ffcb00" ??????????????????????? android:angle="270" ??????????????? /> ??????????? </shape> ??????? </clip> ??? </item> ??? ??? <item android:id="@android:id/progress"> ??????? <clip> ??????????? <shape> ??????????????? <corners android:radius="3dip" /> ??????????????? <gradient ??????????????? ??android:drawable="@drawable/progress_bar" ??????????????????????? android:startColor="#ffdeef73" ??????????????????????? android:centerColor="#ff7ba608" ??????????????????????? android:centerY="0.75" ??????????????????????? android:endColor="#ffe7f79c" ??????????????????????? android:angle="270" ??????????????? /> ??????????? </shape> ??????? </clip> ??? </item> ??? </layer-list>
2。在res/layout文件夹下添加main.xml如下
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ??? android:gravity="center_horizontal" ??? android:layout_width="fill_parent" ??? android:layout_height="fill_parent" ??? android:background="@drawable/music_detail_bg_picture">??? ??? <RelativeLayout android:id="@+id/main_progressBar" ??? ?android:layout_width="fill_parent" ??? ?android:layout_height="30px" ??? ?android:layout_marginLeft="10px" ??? ?android:layout_marginTop="200px"> ??? ?<ProgressBar android:id="@+id/progress_horizontal" ??????? android:layout_width="fill_parent" ??????? android:layout_height="wrap_content" ??????? android:indeterminateOnly="false" ??????? android:progressDrawable="@drawable/progress_horizontal" ??????? android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal" ??????? android:maxHeight="10dip" ??????? android:minHeight="10dip" ??????? android:max="100"/> ??? </RelativeLayout> </RelativeLayout>
3.代码
public class MainActivity extends Activity{ ?/** ? * 界面 ? */ ?private View mainView;? ?/** ? * 进度条的进度 ? */ ?private int progress; ?/** ? * 进度条视图 ? */ ?private ProgressBar progressBar; ?/** ? * 定时器 ? */ ?private Timer timer;
?/** ? * 刷新进度条的handler ? */ ?protected Handler processhandler = new Handler() ?{ ??@Override ??public void handleMessage(Message msg) ??{ ???switch (msg.what) ???{ ???case 2: ????timer.cancel(); //????Intent intent = new Intent( //??????MainActivity.this.getApplicationContext(), //??????MainSearch.class); //????startActivity(intent); //????MainActivity.this.finish(); ????break; ???default: ????break; ???} ???super.handleMessage(msg); ??} ?};
?public void onCreate(Bundle savedInstanceState) ?{ ??super.onCreate(savedInstanceState);
??// 初始化要用的视图 ??initViews(); ??// 启动定时任务 ??startLiveTimer(); ?}
?/** ? *? 初始化要用的视图?? ? */ ?private void initViews() ?{ ??// 实例化一个定时器 ??timer = new Timer(); ??// 获得显示广告界面的View ??mainView = View.inflate(this, R.layout.main, null); ??// 获得显示进度条的view ??progressBar = (ProgressBar) mainView ????.findViewById(R.id.progress_horizontal); ??setContentView(mainView); ?}
?/** ? * 启动发送live请求的定时器 ? */ ?public void startLiveTimer() ?{ ??// 构造一个定时器任务对象 ??TimerTask changeTask = new TimerTask() ??{ ???/** ??? * 定时器任务的执行体 ??? */ ???public void run() ???{ ????progress++;
????// 当加载进度小于100%的时候进行的操作 ????if (progress > 0 && progress < 100) ????{ ?????progressBar.setProgress(progress); ????} ????// 当加载完成时进行的操作 ????else if (progress >= 100) ????{ ?????progressBar.setProgress(progress); ?????processhandler.sendMessage(processhandler.obtainMessage( ???????2, null)); ????} ???} ??};
??// 启动定时器 ??timer.schedule(changeTask, 100, 100); ?} }