这算是第一篇正式的原创博文,好紧张。
下面是效果图:
本来想用自定义view做哪个完成动画,看了下贝赛尔曲线,但是没做出来,只能组装一下了。实现的原理就是遮罩动画,之前学了点flash,其中就有这个原理。简单讲,就是先把对号√用一层白色的布盖住,然后把上面的布一点一点往右拉动,下面的对号就显示出来了。原理很简单,性能可能没自定义的那么好,但容易实现。
下面是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/rect_10_radius"
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="150dp">
<FrameLayout
android:layout_marginTop="25dp"
android:layout_gravity="center_horizontal"
android:layout_width="60dp"
android:layout_height="60dp">
<!--timg是最后显示的对号图片-->
<ImageView
android:src="@drawable/timg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--遮盖住对号的一层布-->
<ImageView
android:id="@+id/iv_place"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--indeterminateDuration 这个属性控制progressbar 转圈的速率-->
<ProgressBar
android:layout_gravity="center"
android:id="@+id/pb"
android:indeterminateDuration="2500"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/tv_msg"
android:textSize="18sp"
android:textColor="#5a5a5a"
android:text="加载中..."
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
下面是代码:
public class ProgressDialog extends Dialog {
private ImageView ivPlace;
private TextView tvMsg;
private ProgressBar mPb;
public ProgressDialog(Context context) {
//可以换不同style改变进度圈的样式
super(context,android.R.style.Theme_Holo_Light_Dialog);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
init();
}
private void init() {
this.setContentView(R.layout.dialog_loading);
mPb = (ProgressBar) findViewById(R.id.pb);
tvMsg = (TextView) findViewById(R.id.tv_msg);
ivPlace = (ImageView) findViewById(R.id.iv_place);
}
/**
* 动画放完后,dialog会dismiss,后续操作可以实现OnCompleteListener接口,
* 传null 只是简单播放完成动画,然后让dialog消失
* @param completeListener
*/
public void complete(final OnCompleteListener completeListener){
//先让ProgressBar隐藏
mPb.setVisibility(View.GONE);
//执行动画,往右扯动布,让对号露出来
ObjectAnimator translationX = ObjectAnimator.ofFloat(ivPlace, "translationX", 0, ivPlace.getWidth());
translationX.setDuration(700);
translationX.setInterpolator(new AccelerateInterpolator());
translationX.start();
//改变提示信息内容和颜色
tvMsg.setTextColor(Color.parseColor("#4caf65"));
tvMsg.setText("完成");
//动画放完后再调用OnCompleteListener接口
translationX.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
dismiss();
if(completeListener!=null){
completeListener.onComplete();
}
}
});
}
/**
* 设置转圈时的提示信息
*/
public void setMsg(String msg){
tvMsg.setText(msg);
}
/**
* 一个接口,用来做完成动画放完后要做的事,比如finish();
*/
public interface OnCompleteListener{
void onComplete();
}
}
使用实例:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.show();
//TODO 一些耗时操作
// 调用以下一种实现你的需求
// 1.耗时操作执行完后,直接dismis
progressDialog.dismiss();
// 2.放完动画后直接dismiss
progressDialog.complete(null);
// 3.放完动画后dismiss,再做点事情
progressDialog.complete(new ProgressDialog.OnCompleteListener() {
@Override
public void onComplete() {
finish();
}
});