Android中创建一个透明的进度对话框实例_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);
 }
}

最后的结果是

时间: 2024-08-08 01:53:29

Android中创建一个透明的进度对话框实例_Android的相关文章

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"

在photoshop中创建一个透明玻璃按钮

在这一篇教程中,我将教给你怎样在photoshop中创建一个透明玻璃效果的干净的网页用按钮. 这就是我们最终的结果.好了,让我们开始吧.在photoshop中新建文档,尺寸为430x430,白色背景. STEP-01 新建图层,命名为 "box".用圆角矩形工具 (U)创建一个圆角矩形.点击ctrl+回车创建选区,填充为白色. STEP-02 给这个形状创建图层属性-渐变叠加,内发光和描边. STEP-03 给这个形状创建渐变叠加.渐变颜色为-左边(#B1DB08) 中间(#66AB0

Android中自定义一个View的方法详解_Android

本文实例讲述了Android中自定义一个View的方法.分享给大家供大家参考,具体如下: Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到一些坑,我把自己遇到的一些问题和解决方法总结一下,希望对广大码友们有所帮助. 注意点① 用xml定义Layout时,Root element 最好使用merge 当我们需要继承一个布局比较复杂的ViewGroup(比较多的是LinearLayout.RelativeLayout)时,通常会用xml来

Android中使用Service实现后台发送邮件功能实例_Android

本文实例讲述了Android中使用Service实现后台发送邮件功能.分享给大家供大家参考,具体如下: 程序如下: import android.app.Activity; import android.content.Intent; import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.widget.TextView; public class A05Ac

Android中创建快捷方式及删除快捷方式实现方法_Android

/** * * 创建快捷方式 * @param map 快捷方式图标 * @param appName 快捷方式标题 * @param appUrl 快捷方式打开的地址 * @param iconUrl 快捷方式图标地址 * * */ public static void createShortcut(Context activity ,Bitmap map ,String appName ,String appUrl ,String iconUrl){ Intent shortcut = ne

Android中编写属性动画PropertyAnimation的进阶实例_Android

0.基础回顾PropertyAnimation,属性动画,顾名思义就是利用对象的属性变化形成动画的效果.属性动画的类可以用Animator这个抽象类来表示,通常使用它的子类:AnimatorSet和ValueAnimator,同时ValueAnimator有两个子类分别是ObjectAniamtor和TimeAnimator. 定义属性动画的XML资源的时候通常可以是如下三个元素之一作为根元素: <set>元素:该资源元素代表的是AniamtorSet类,这个类可以包含<set>,

android中webview控件和javascript交互实例_Android

当我们要实现丰富的图文混排效果的时候,我们一般会使用webview,这是一个功能十分强大的的控件,来看看官方的解释: 复制代码 代码如下: A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit

android中WebView和javascript实现数据交互实例_Android

在看懂这篇文章之前首先要有javascript基础. (1) js调用android的方法: 复制代码 代码如下: WebView wView: wView. addJavascriptInterface (Object obj, String interfaceName); 是实例化一个对象,在html的js中调用,第二个参数是实例化对象的别名,如果要使用这个obj,则在js中使用的名字 就是interfaceName. 复制代码 代码如下: public class jsWebDemo ex

Android中ViewPager的PagerTabStrip与PagerTitleStrip用法实例_Android

零.概览1.ViewPager说白了就是个控件,在使用时包名要带全是Android.support.v4.view.ViewPager.由于我的ADT-Bundle版本比较高,这个包默认自带了,且默认是随apk打包导出的.如下图: 如果在Android Private Libraries里没这个包,则要自己在属性的Libraries里自己添加.添加后记得在上图所示的Order and Export里将其打勾. 2.理论上说要实现滑屏只要一个ViewPager就可以了,不需要再在里面嵌套如下: <