1. 程式人生 > >進度條與圖片框

進度條與圖片框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height
="match_parent" tools:context=".MainActivity">
<FrameLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/tv_main_tv1" android:layout_width="60dp" android:layout_height="wrap_content"
/>
<ProgressBar android:id="@+id/pb_main_pb1" android:layout_width="match_parent" android:max="100" style="?android:attr/progressBarStyleHorizontal" android:layout_height="40dp" /> </FrameLayout> <!-- <ImageView android:layout_width="100dp" android:background="@color/red" android:src="@drawable/small_image" android:scaleType="matrix" android:layout_height="100dp" />-->
<!-- <Button android:layout_width="match_parent" android:onClick="studyToast" android:layout_height="wrap_content" />--> <Button android:id="@+id/btn_main_btn1" android:layout_width="match_parent" android:onClick="setProgressBar" android:layout_height="wrap_content" /> </LinearLayout> ## 邏輯介面 ##

package com.example.android_05;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private ProgressBar pb1;
private int progress;
private TextView tv1;
private myHandle myHandle=new myHandle();
private int code=1;
private class myHandle extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (code==msg.what){
progress++;
pb1.setProgress(progress);
tv1.setText(progress+”%”);
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pb1=this.findViewById(R.id.pb_main_pb1);
    tv1=this.findViewById(R.id.tv_main_tv1);
}

//訊息彈框
public void studyToast(View view) {
Toast.makeText(this,”彈框”,Toast.LENGTH_LONG).show();
}

public void setProgressBar(View view) {
    if (progress==0){
        new MyThread().start();
    }
}

private class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
       while (true){
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           if (progress == 100){
               progress = 0;
               break;
           }
           Message msg=new Message();
           msg.what=1;
           myHandle.sendMessage(msg);

       }
    }
}

}

 進度條 ProgressBar
  1 常用屬性
      style="?android:attr/progressBarStyleHorizontal" 預設為圓形 
      android:progress="33"
      android:max="100" 


      執行緒休眠
      Thread.sleep(100);//拋異常
      SystemClock.sleep(100);//不會拋異常