가로 막대 프로그레스바

2018. 10. 15. 20:59Android

반응형

ㅇ res/drawable/progressbar.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="60dp"/>
<solid android:color="#000000"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="60dp"/>
<solid android:color="#ffffff"/>
</shape>
</clip>
</item>
</layer-list>

ㅇ res/layout/activity_main.xml

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<ProgressBar
android:id="@+id/progressBar"
android:layout_marginTop="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:progressDrawable="@drawable/progressbar"

style="?android:attr/progressBarStyleHorizontal"/>

<TextView
android:id="@+id/progress_msg"

android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_below="@id/progressBar"
android:gravity="center_horizontal"
android:text="다운로드"
android:textColor="#000000" />


</RelativeLayout>

ㅇ MainActivity.java

private ProgressBar mProgressBar;
private TextView mTextMsg;

private Thread mThread;

private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.i(TAG, "msg : "+msg.what);

mProgressBar.setProgress(msg.what);
mTextMsg.setText(msg.what+"%");
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mProgressBar = findViewById(R.id.progressBar);
mTextMsg =findViewById(R.id.progress_msg);

mThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}

Message message = mHandler.obtainMessage();
message.what = i;

mHandler.sendMessage(message);
}

}
});
mThread.start();


new MyAsyncTask().execute();

}

* Thread + Handler == AsyncTask()


private class MyAsyncTask extends AsyncTask<String,Integer,Boolean>{

@Override
protected Boolean doInBackground(String... strings) {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i); // => onProgressUpdate() call
}
return true;
}

@Override
protected void onProgressUpdate(Integer... values) {
int i = values[0];
mProgressBar.setProgress(i);
mTextMsg.setText(i+"%");
}

@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean!=null && aBoolean){
mProgressBar.setProgress(100);
mTextMsg.setText("100%");
}
}
}


반응형

'Android' 카테고리의 다른 글

NFC  (0) 2018.10.22
안드로이드 실행환경  (0) 2018.10.19
MD5, SHA1, SHA256  (0) 2018.10.03
BCrypt 암호화  (0) 2018.10.03
암호화(AES)  (0) 2018.10.02