Android中創(chuàng)建一個透明的進度對話框?qū)嵗?/h1>
更新時間:2014年05月06日 09:15:50 作者:
這篇文章主要介紹了Android中創(chuàng)建一個透明的進度對話框?qū)嵗?需要的朋友可以參考下
首先我們看一下什么叫做透明的進度對話框:

接下來我們講一下如何創(chuàng)建:
1、使用Eclipse創(chuàng)建一個新的Android 項目,使用Android 2.2或以上。
2、在/res/layout文件夾,創(chuàng)建線性布局activity_main.xml文件,主要是為了添加一個文本標簽和一個按鈕
復(fù)制代碼 代碼如下:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="20sp"
android:text="Transparent Progress Indicator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check it out!"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:id="@+id/the_button" />
</LinearLayout>
3、在/res/values中打開styles.xml,在這里將添加透明對話框的樣式。請務(wù)必指定父屬性,否則你在運行時會出現(xiàn)問題
復(fù)制代碼 代碼如下:
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- Transparent dialog -->
<style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
4、 在/res中間添加一個動態(tài)旋轉(zhuǎn)的動畫圖片:

5、現(xiàn)在可以實現(xiàn)您的MainActivity.java文件了
復(fù)制代碼 代碼如下:
MainActivity.java
package com.authorwjf.transparentprogressdialog;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private TransparentProgressDialog pd;
private Handler h;
private Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h = new Handler();
pd = new TransparentProgressDialog(this, R.drawable.spinner);
r =new Runnable() {
@Override
public void run() {
if (pd.isShowing()) {
pd.dismiss();
}
}
};
findViewById(R.id.the_button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
pd.show();
h.postDelayed(r,5000);
}
@Override
protected void onDestroy() {
h.removeCallbacks(r);
if (pd.isShowing() ) {
pd.dismiss();
}
super.onDestroy();
}
}
6、以下是實現(xiàn)透明動畫的代碼
復(fù)制代碼 代碼如下:
private class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context, int resourceIdOfImage) {
super(context, R.style.TransparentProgressDialog);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
iv = new ImageView(context);
iv.setImageResource(resourceIdOfImage);
layout.addView(iv, params);
addContentView(layout, params);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);
}
}
最后的結(jié)果是

您可能感興趣的文章:- Android單選按鈕對話框用法實例分析
- Android中創(chuàng)建對話框(確定取消對話框、單選對話框、多選對話框)實例代碼
- Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
- Android實現(xiàn)單選與多選對話框的代碼
- Android中自定義對話框(Dialog)的實例代碼
- 實例詳解Android自定義ProgressDialog進度條對話框的實現(xiàn)
- Android中AlertDialog各種對話框的用法實例詳解
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- Android加載對話框同時異步執(zhí)行實現(xiàn)方法
- 8種android 對話框(Dialog)使用方法詳解
- Android編程雙重單選對話框布局實現(xiàn)與事件監(jiān)聽方法示例
相關(guān)文章
-
Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器
這篇文章主要為大家詳細介紹了Android實現(xiàn)一個簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下 2020-04-04
-
Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果
這篇文章主要為大家詳細介紹了Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下 2017-12-12
-
Android編程實現(xiàn)切換imageView的方法分析
這篇文章主要介紹了Android編程實現(xiàn)切換imageView的方法,結(jié)合具體實例形式分析了切換imageView的相關(guān)設(shè)置技巧與注意事項,需要的朋友可以參考下 2017-09-09
-
Android 高德地圖之poi搜索功能的實現(xiàn)代碼
這篇文章主要介紹了android 高德地圖之poi搜索功能的實現(xiàn)代碼,在實現(xiàn)此功能時遇到很多問題,在文章都給大家提到,需要的朋友可以參考下 2017-08-08
-
Android基礎(chǔ)控件RadioGroup使用方法詳解
這篇文章主要為大家詳細介紹了Android基礎(chǔ)控件RadioGroup的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下 2019-11-11
最新評論
首先我們看一下什么叫做透明的進度對話框:

接下來我們講一下如何創(chuàng)建:
1、使用Eclipse創(chuàng)建一個新的Android 項目,使用Android 2.2或以上。
2、在/res/layout文件夾,創(chuàng)建線性布局activity_main.xml文件,主要是為了添加一個文本標簽和一個按鈕
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="20sp"
android:text="Transparent Progress Indicator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check it out!"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:id="@+id/the_button" />
</LinearLayout>
3、在/res/values中打開styles.xml,在這里將添加透明對話框的樣式。請務(wù)必指定父屬性,否則你在運行時會出現(xiàn)問題
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- Transparent dialog -->
<style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
4、 在/res中間添加一個動態(tài)旋轉(zhuǎn)的動畫圖片:

5、現(xiàn)在可以實現(xiàn)您的MainActivity.java文件了
MainActivity.java
package com.authorwjf.transparentprogressdialog;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private TransparentProgressDialog pd;
private Handler h;
private Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h = new Handler();
pd = new TransparentProgressDialog(this, R.drawable.spinner);
r =new Runnable() {
@Override
public void run() {
if (pd.isShowing()) {
pd.dismiss();
}
}
};
findViewById(R.id.the_button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
pd.show();
h.postDelayed(r,5000);
}
@Override
protected void onDestroy() {
h.removeCallbacks(r);
if (pd.isShowing() ) {
pd.dismiss();
}
super.onDestroy();
}
}
6、以下是實現(xiàn)透明動畫的代碼
private class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context, int resourceIdOfImage) {
super(context, R.style.TransparentProgressDialog);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
iv = new ImageView(context);
iv.setImageResource(resourceIdOfImage);
layout.addView(iv, params);
addContentView(layout, params);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);
}
}
最后的結(jié)果是

- Android單選按鈕對話框用法實例分析
- Android中創(chuàng)建對話框(確定取消對話框、單選對話框、多選對話框)實例代碼
- Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
- Android實現(xiàn)單選與多選對話框的代碼
- Android中自定義對話框(Dialog)的實例代碼
- 實例詳解Android自定義ProgressDialog進度條對話框的實現(xiàn)
- Android中AlertDialog各種對話框的用法實例詳解
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- Android加載對話框同時異步執(zhí)行實現(xiàn)方法
- 8種android 對話框(Dialog)使用方法詳解
- Android編程雙重單選對話框布局實現(xiàn)與事件監(jiān)聽方法示例
相關(guān)文章
Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器
這篇文章主要為大家詳細介紹了Android實現(xiàn)一個簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果
這篇文章主要為大家詳細介紹了Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android編程實現(xiàn)切換imageView的方法分析
這篇文章主要介紹了Android編程實現(xiàn)切換imageView的方法,結(jié)合具體實例形式分析了切換imageView的相關(guān)設(shè)置技巧與注意事項,需要的朋友可以參考下2017-09-09
Android 高德地圖之poi搜索功能的實現(xiàn)代碼
這篇文章主要介紹了android 高德地圖之poi搜索功能的實現(xiàn)代碼,在實現(xiàn)此功能時遇到很多問題,在文章都給大家提到,需要的朋友可以參考下2017-08-08
Android基礎(chǔ)控件RadioGroup使用方法詳解
這篇文章主要為大家詳細介紹了Android基礎(chǔ)控件RadioGroup的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

