ProgressBar 等比例

ProgressBar 等比例

 

优点:任务完成 其也正好到达终点

 

 

缺点:需要知道以下变量取值:

 

1. ProgressBar.max()

 

2. 任务所需总长度

 

3. 更新时 任务的完成值

 

 

 

整个代码 或是 思路 都比较简单 直接上代码 有问题或其他 请跟帖

 

 

1. main.xml 2个ProgressBar

 

<?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"
    >
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/progress1"  
	android:max="100"
	style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="100dip"
    />
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/progress2"  
	android:max="30"
	style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:paddingTop="100dip"
    />
</LinearLayout>

 

 

 

 

2. ProgressUsage.java

 

 

public class ProgressUsage extends Activity {
	ProgressBar pBar1,pBar2;
	Handler handler = new Handler();
	
	MediaPlayer mp;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        pBar1 = (ProgressBar)findViewById(R.id.progress1);
        pBar2 = (ProgressBar)findViewById(R.id.progress2);
        
        mp = new MediaPlayer();
        AssetFileDescriptor  fd = getResources().openRawResourceFd(R.raw.audio);   
                   
        try {
			mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
			
			fd.close(); 
			
			mp.prepare();   
	        mp.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
                       
		startProgressUpdate();              
    }
    
    public void startProgressUpdate(){
    	Runnable update = new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				int position = mp.getCurrentPosition();
				int duration = mp.getDuration();
				
				int max1 = pBar1.getMax();
				int max2 = pBar2.getMax();
				
				//等比例取值
				int i1 = max1 * position/duration;
				int i2 = max2 * position/duration;
				
				pBar1.setProgress(i1);
				pBar2.setProgress(i2);
				
				startProgressUpdate();
			}
    		
    	};
    	
    	handler.postDelayed(update, 10);
    }
}

 

 

 

3. emulator 运行截图: 注意2ProgressBar 虽然刷新频率一样 但是刷新长度不等

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值