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

时间:2022-01-15 14:41:33

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

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

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

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