关于Progress Bar的使用价值

时间:2012-02-25 10:38:30

标签: android multithreading progress-bar

我正在开发Android游戏。我想用来重视进度条的进度来强制射箭。这是我的代码:

public class TheArrows extends Activity {
    ProgressBar myProgressBar;
    private int myProgress=0;
    private int t=0;

    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);
        app.quabongchuyendong.QuaBongView quaBongView=(app.quabongchuyendong.QuaBongView) findViewById(R.id.qua_bong);
        quaBongView.setOnTouchListener(new OnTouchListener() {
            @Override public boolean onTouch(View view, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){ t=1; }
                if(event.getAction()==MotionEvent.ACTION_MOVE){ t=1; }
                if(event.getAction()==MotionEvent.ACTION_UP){ t=0; }
                return false;
            }
        });
        new Thread(myThread).start();
    }

    private Runnable myThread = new Runnable(){
        @Override public void run() {
            // TODO Auto-generated method stub 
            while (myProgress<100){
                try{
                    myHandle.sendMessage(myHandle.obtainMessage());
                    Thread.sleep(100);
                } catch(Throwable t){ }
            }
        }

       Handler myHandle = new Handler(){
            @Override public void handleMessage(Message msg) {
                if(t==1) myProgress++;
                myProgressBar.setProgress(myProgress);
            } 
        };
    };

    public int progressValue(){
        return myProgress;
    }
}

虽然我myProgress++; myProgress progressValue()为什么{{1}}没有改变?请帮帮我。

2 个答案:

答案 0 :(得分:0)

new Thread(myThread).start(); 应该在返回声明之前保持联系;

答案 1 :(得分:0)

试试这个:

 @Override public boolean onTouch(View view, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){ t=1; }
                else if(event.getAction()==MotionEvent.ACTION_MOVE){ t=1; }
                else if(event.getAction()==MotionEvent.ACTION_UP){ t=0; }  
     new Thread(myThread).start(); 
                return false;
            }
        });

并删除处理程序处理程序中的if(t == 1)条件

   myHandle = new Handler(){
    @Override public void handleMessage(Message msg) {
        myProgress++;
        myProgressBar.setProgress(myProgress);
    } 
};

};