[Android] ProgressBar Android 2011. 12. 21. 15:27

프로그레스는 진행 상태를 나타 내 준다.

안드로이드에서는 우리가 알고 있는 막대 형태의 프로그레스와 원형의 프로그레스 두가지가 있다.

막대 형태의 프로그레스를 쓰고 싶으면 style="?android:attr/ProgressBarStyleHorizontal" 처리를 하면 나타나고

원형을 하고 싶다면 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">
    <ProgressBar       
        android:id="@+id/prg1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="10"
        android:secondaryProgress="50"
    />
    <ProgressBar       
        android:id="@+id/prg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
    />
    <Button
        android:id="@+id/defirst"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="defirst"
    />
    <Button
        android:id="@+id/infirst"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="infirst"
    />
    <Button
        android:id="@+id/desecond"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="desecond"
    />
    <Button
        android:id="@+id/insecond"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="insecond"
    />
   
    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="start"
    />
    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="stop"
    />
</LinearLayout>

다음은 프로그레스바와 관련된 메서드에 대한 설명이다.

 메서드  설명 
 setProgress(int value)  프로그레스 값 설정
 setSecondaryProgress(int value)   두번째 프로그레스 값 설정
 incrementProgressBy(int value)  프로그레스 값 증가
 incrementSecondaryProgressBy(int value)   두번째 프로그레스 값 증가

여기서 첫번째 두번째 프로그레스라고 명시한 것은 동영상 버퍼링 중 재생 할 떄 뒷 색깔과 앞 색깔이 다른 것을 본 적이 있을 것이다. 그것을 말하는 것이다.

public class ImageViewActivity extends Activity{
 ProgressBar _prg1;
 ProgressBar _prg2;
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        _prg1 = (ProgressBar)findViewById(R.id.prg1);
        _prg2 = (ProgressBar)findViewById(R.id.prg2);
       
        findViewById(R.id.defirst).setOnClickListener(listener);
        findViewById(R.id.infirst).setOnClickListener(listener);
        findViewById(R.id.desecond).setOnClickListener(listener);
        findViewById(R.id.insecond).setOnClickListener(listener);
        findViewById(R.id.start).setOnClickListener(listener);
        findViewById(R.id.stop).setOnClickListener(listener);
    }
 
 Button.OnClickListener listener = new View.OnClickListener() {
  
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   switch(v.getId()){
   case R.id.defirst :
    _prg1.incrementProgressBy(-2);
    break;
   case R.id.infirst :
    _prg1.incrementProgressBy(2);
    break;
   case R.id.desecond:
    _prg1.incrementSecondaryProgressBy(-2);
    break;
   case R.id.insecond:
    _prg1.incrementSecondaryProgressBy(2);
    break;
   case R.id.start:
    _prg2.setVisibility(View.VISIBLE);
    break;
   case R.id.stop:
    _prg2.setVisibility(View.INVISIBLE);
    break;
   }
  }
 };
}
이 자바 코드를 넣고 실행을 해 보면 각기 메서드들에 대해서 확인이 가능 할 것이다.

단 원형 프로그레스는 값을 변경 하는 것이 아니라 대기 상황에 따라 보여주거나 사라지게 해야한다.

이러한 것은 쓰레드에서 이용하면 적합하다고 사료된다.