2012年3月14日星期三

實現透明進度對話框(Transparent Progress Dialog)

通過自定義對話框, 修改佈局參數的 alpha 屬性, 可以實現透明的進度對話框(Transparent Progress Dialog).

透明進度對話框(Transparent Progress Dialog)

創建 /res/layout/transparentprogressdialog.xml 定義包含進度條(ProgressBar)的對話框.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>


修改主活動, 實現自定義對話框.
package com.AndroidProgressDialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class AndroidProgressDialog extends Activity {

static final int CUSTOM_PROGRESSDIALOG_ID = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button)findViewById(R.id.start);

buttonStart.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new asyncTaskUpdateProgress().execute();
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_PROGRESSDIALOG_ID:
dialog = new Dialog(AndroidProgressDialog.this);
dialog.setContentView(R.layout.transparentprogressdialog);
dialog.setTitle("ProgressDialog");

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.5f;
dialog.getWindow().setAttributes(params);

break;
}

return dialog;
}

public class asyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {

int progress;
//ProgressDialog progressDialog;

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dismissDialog(CUSTOM_PROGRESSDIALOG_ID);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress = 0;
showDialog(CUSTOM_PROGRESSDIALOG_ID);
}

@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
while(progress<100){
progress++;
SystemClock.sleep(20);
}

return null;

}
}
}


主佈局 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>

</LinearLayout>


沒有留言:

發佈留言