Android中创建一个透明的进度对话框实例

 更新时间:2014年05月06日 09:15:50   作者:  
这篇文章主要介绍了Android中创建一个透明的进度对话框实例,需要的朋友可以参考下

首先我们看一下什么叫做透明的进度对话框:



接下来我们讲一下如何创建:
1、使用Eclipse创建一个新的Andr​​oid 项目,使用Android 2.2或以上。
2、在/res/layout文件夹,创建线性布局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,在这里将添加透明对话框的样式。请务必指定父属性,否则你在运行时会出现问题

复制代码 代码如下:

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中间添加一个动态旋转的动画图片:



5、现在可以实现您的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、以下是实现透明动画的代码

复制代码 代码如下:

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);
 }
}

最后的结果是

相关文章

  • Android编程实现将压缩数据库文件拷贝到安装目录的方法

    Android编程实现将压缩数据库文件拷贝到安装目录的方法

    这篇文章主要介绍了Android编程实现将压缩数据库文件拷贝到安装目录的方法,涉及Android处理压缩文件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Kotlin实现半圆形进度条的方法示例

    Kotlin实现半圆形进度条的方法示例

    这篇文章主要给大家介绍了关于Kotlin实现半圆形进度条的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
    2018-03-03
  • Android自定义View绘制贝塞尔曲线的方法

    Android自定义View绘制贝塞尔曲线的方法

    这篇文章主要为大家详细介绍了Android自定义View绘制贝塞尔曲线的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Android开发VR实战之播放360度全景视频

    Android开发VR实战之播放360度全景视频

    这篇文章主要为大家详细介绍了Android开发VR实战之播放360度全景视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android仿优酷圆形菜单学习笔记分享

    Android仿优酷圆形菜单学习笔记分享

    这篇文章主要为大家分享了Android仿优酷圆形菜单学习笔记,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • android通过google api获取天气信息示例

    android通过google api获取天气信息示例

    这篇文章主要介绍了android通过google api获取天气信息示例,需要的朋友可以参考下
    2014-04-04
  • android读取扫码模组数据的方法

    android读取扫码模组数据的方法

    这篇文章主要为大家详细介绍了android读取扫码模组数据的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android RelativeLayout相对布局属性简析

    Android RelativeLayout相对布局属性简析

    在Android应用开发过程中,为了界面的美观考虑,经常会使用到布局方面的属性,本文就以此问题深入解析,详解一下Android RelativeLayout相对布局属性在实际开发中的应用,需要的朋友可以参考下
    2012-11-11
  • Android自定义PasswordInputView密码输入

    Android自定义PasswordInputView密码输入

    这篇文章主要为大家详细介绍了Android自定义PasswordInputView密码输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android 无障碍全局悬浮窗实现示例

    Android 无障碍全局悬浮窗实现示例

    本文主要介绍了Android 无障碍全局悬浮窗实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论