android 自定义加载动画效果,Android开发中的正在加载动画效果

有两种实现效果,一种是系统默认效果,一种是自定义效果;默认效果比较简单但效果也比较单调,自定义可以做出比较好看的效果来;

第一种:通过创建一个ProgressDialog对象并设置属性来显示

public class Util {

private static ProgressDialog processDia;

/**

* 显示加载中对话框

*

* @param context

*/

public static void showLoadingDialog(Context context,String message,boolean isCancelable) {

if (processDia == null) {

processDia= new ProgressDialog(context,R.style.dialog);

//点击提示框外面是否取消提示框

processDia.setCanceledOnTouchOutside(false);

//点击返回键是否取消提示框

processDia.setCancelable(isCancelable);

processDia.setIndeterminate(true);

processDia.setMessage(message);

processDia.show();

}

}

/**

* 关闭加载对话框

*/

public static void closeLoadingDialog() {

if (processDia != null) {

if (processDia.isShowing()) {

processDia.cancel();

}

processDia = null;

}

}

}

其中style.xml中需要设置dialog的背景、字体等属性

@null

true

true

true

@android:color/transparent

true

@null

25sp

第二种:首先要准备至少两张图片,图片在切换的过程可以形成动画效果即可;

定义动画切换特效,anim/loading.xml

自定义dialog页面progress_dialog.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="vertical" >

android:id="@+id/loadingIv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@anim/loading"/>

android:id="@+id/loadingTv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/loadingIv"

android:layout_centerHorizontal="true"

android:textSize="20sp"

android:text="正在加载中.." />

创建diglog类MyProgressDialog

public class MyProgressDialog extends ProgressDialog {

private AnimationDrawable mAnimation;

private ImageView mImageView;

private TextView mTextView;

private String loadingTip;

private int resid;

/**

*

* @param context 上下文对象

* @param content 显示文字提示信息内容

* @param id 动画id

*/

public MyProgressDialog(Context context, String content, int resid) {

super(context);

this.loadingTip = content;

this.resid = resid;

//点击提示框外面是否取消提示框

setCanceledOnTouchOutside(false);

//点击返回键是否取消提示框

setCancelable(false);

setIndeterminate(true);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.progress_dialog);

mTextView = (TextView) findViewById(R.id.loadingTv);

mImageView = (ImageView) findViewById(R.id.loadingIv);

mImageView.setBackgroundResource(resid);

// 通过ImageView对象拿到背景显示的AnimationDrawable

mAnimation = (AnimationDrawable) mImageView.getBackground();

mImageView.post(new Runnable() {

@Override

public void run() {

mAnimation.start();

}

});

mTextView.setText(loadingTip);

}

}

最后在activity中进行调用,这样就完成了一个自定义的dialog提示框

public class MainActivity extends Activity {

private MyProgressDialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.btn_start);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

showMyDialog(view);

}

});

}

/**

* 显示对话框

* @param v

*/

public void showMyDialog(View v){

dialog =new MyProgressDialog(this, "正在加载中",R.anim.loading);

dialog.show();

Handler handler =new Handler();

handler.postDelayed(new Runnable() {

@Override

public void run() {

dialog.dismiss();

}

}, 3000);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值