布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!--android:rotation="180" 旋转180度从右往左移动 progressDrawable自定义了颜色控制 --> <ProgressBar android:progressDrawable="@drawable/progressbar_a" android:rotation="180" style="@android:style/Widget.ProgressBar.Horizontal" android:id="@+id/id_progressbar01" android:layout_weight="1" android:layout_width="0dp" android:layout_height="15dp" /> <ProgressBar android:progressDrawable="@drawable/progressbar_a" style="@android:style/Widget.ProgressBar.Horizontal" android:id="@+id/id_progressbar02" android:layout_width="0px" android:layout_weight="1" android:layout_height="15dp" /> </LinearLayout> </RelativeLayout> drawable文件,这里把圆角,进度控制等全部去掉,只是简单的更改 颜色<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 背景 gradient是渐变,corners定义的是圆角 --> <item android:id="@android:id/background"> <clip> <shape> <solid android:color="#ffffff" /> </shape> </clip> </item> <!-- 第二条进度条颜色 --> <item android:id="@android:id/progress" > <clip> <shape> <solid android:color="#0568fc" /> </shape> </clip> </item> </layer-list>
关键代码public class MainActivity extends Activity implements Runnable{ //实现Runnable接口 //声明线程 private Thread th; 声明控件 ProgressBar progressBar1; ProgressBar progressBar2; //这里本来想动态改变颜色的。没成功 // Drawable a=getResources().getDrawable(R.drawable.progressbar_a); //Drawable b=getResources().getDrawable(R.drawable.progressbar_b); private boolean stateChage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar1 = (ProgressBar) findViewById(R.id.id_progressbar01); progressBar2 = (ProgressBar) findViewById(R.id.id_progressbar02); th=new Thread(this); //启动线程 th.start(); } public void run() { while (true){ //获取初始进度值,获取最大进度值,获取第二进度值 int current1=progressBar1.getProgress(); int current2=progressBar2.getProgress(); int currentMax1=progressBar1.getMax(); int currentMax2=progressBar1.getMax(); int secCurrent1=progressBar1.getSecondaryProgress(); int secCurrent2=progressBar1.getSecondaryProgress(); if (stateChage==false){ if (current1>=currentMax1&¤t2>=currentMax2){ stateChage=true; // b.setBounds(progressBar1.getProgressDrawable().getBounds()); // progressBar1.setProgressDrawable(b); //progressBar2.setProgressDrawable(b); }else { progressBar1.setProgress(current1+5); progressBar2.setProgress(current2+5); progressBar1.setSecondaryProgress(secCurrent1 +5); progressBar2.setSecondaryProgress(secCurrent2+5); } }else { if (current1<=0&¤t2<=0){ stateChage=false; }else { progressBar1.setProgress(current1-1); progressBar2.setProgress(current2-1); } } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }
ProgressBar,双向进度
最新推荐文章于 2023-12-17 01:50:58 发布