作者:5欣欣向荣_135 | 来源:互联网 | 2023-10-13 10:01
首先我们看一下什么叫做透明的进度对话框:
上述就是android开发分享Android中创建一个透明的进度对话框实例的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—编程笔记
接下来我们讲一下如何创建:
1、使用eclipse创建一个新的android 项目,使用android 2.2或以上。
2、在/res/layout文件夹,创建线性布局activity_main.xml文件,主要是为了添加一个文本标签和一个按钮
代码如下:
activity_main.xml
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>
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” />
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” />
3、在/res/values中打开styles.xml,在这里将添加透明对话框的样式。请务必指定父属性,否则你在运行时会出现问题
代码如下:
styles.xml
base application theme, dependent on api level. this theme is replaced
by appbasetheme from res/values-vxx/styles.xml on newer devices.
–>
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);
}
}
最后的结果是