[Android] 안드로이드 스튜디오 핸들러 이용한 프로그레스바 구현

    두개의 xml과 두개의 java로 구성

    메인에선 프로그레스 바를 구성하고

    서브에선 이미지를 구성

     

    메인 xml

     

    <?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:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove The Bomb"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="match_parent"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="100"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="STOP" />
    
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="START" />
    
    </LinearLayout>

     

    메인자바

    package com.example.handlerprogresbar;
    
    import android.content.Intent;
    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.Button;
    import android.widget.ProgressBar;
    
    public class MainActivity extends AppCompatActivity {
    
    public static final long DELAY_MS = 100;                        //1초마다 감소
    public static final int WHAT_PROGRESS = 1;
    
    private ProgressBar progressBar;
    private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg){
    super.handleMessage(msg);
    
    if(progressBar.getProgress()==1){                                      //프로그레스바가 1이면 시작
    Intent intent = new Intent(MainActivity.this,submain.class);
    startActivity(intent);
    
    }
    
    if(progressBar.getProgress()>0){                                         //프로그레스바가 0보다크면점점 줄어듬     
    progressBar.setProgress(progressBar.getProgress()-1);                  
    }
    
    handler.sendEmptyMessageDelayed(WHAT_PROGRESS,DELAY_MS);
    }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    progressBar = (ProgressBar)findViewById(R.id.progressBar);
    
    handler.sendEmptyMessageDelayed(WHAT_PROGRESS,DELAY_MS);
    
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    
    handler.removeMessages(WHAT_PROGRESS);
    }
    });
    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    
    handler.sendEmptyMessageDelayed(WHAT_PROGRESS,DELAY_MS);
    }
    });
    }
    }

     

    서브 xml

     

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".submain">
    
    <ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src = "@drawable/bomb"
    />
    
    </android.support.constraint.ConstraintLayout>

     

    서브 자바

     

    package com.example.handlerprogresbar;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class submain extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_submain);
    }
    }

     

    이미지 화면

    빨간색  (프로그레스바)  가 점점 줄어듬  stop과 start로 조작 가능

     

    프로그레스 바가 0이 되면 이미지 출력

    댓글

    Designed by JB FACTORY