高仿凤凰新闻的加载效果
介绍:
模仿凤凰新闻的加载。两个圆球旋转缩放。使用Animator属性动画。
本例子主要来自:http://www.itlanbao.com/code/20151208/10000/100680.html,效果实现主要由new AnimatorSet()然后通过几个动画ObjectAnimator
代码实现:
public class Loading {
private Context context ;
private ImageView big;
private ImageView small;
private View view;
public Loading(Context context) {
this.context = context;
}
public View initConfig(){
DisplayMetrics dm = new DisplayMetrics();
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(dm);
view = View.inflate(context, R.layout.loadview, null);
big = (ImageView) view.findViewById(R.id.big);
small = (ImageView) view.findViewById(R.id.small);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.leftMargin = dm.widthPixels /2 - 25;
big.setLayoutParams(params);
LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_VERTICAL);
params2.leftMargin = dm.widthPixels /2 + 25;
small.setLayoutParams(params2);
startAnimation();
return view;
}
private void startAnimation(){
AnimatorSet set = new AnimatorSet();
ObjectAnimator animator = ObjectAnimator.ofFloat(big, "alpha", 0.2f, 1f , 0.2f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(small, "alpha", 1f, 0.2f, 1f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "rotation", 0, 359);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(big, "scaleX", 0.2f, 1.4f,0.2f);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(small, "scaleX", 1.4f, 0.2f,1.4f);
ObjectAnimator animator6 = ObjectAnimator.ofFloat(big, "scaleY", 0.2f, 1.4f,0.2f);
ObjectAnimator animator7 = ObjectAnimator.ofFloat(small, "scaleY", 1.4f, 0.2f,1.4f);
animator.setRepeatCount(Integer.MAX_VALUE);
animator2.setRepeatCount(Integer.MAX_VALUE);
animator3.setRepeatCount(Integer.MAX_VALUE);
animator4.setRepeatCount(Integer.MAX_VALUE);
animator5.setRepeatCount(Integer.MAX_VALUE);
animator6.setRepeatCount(Integer.MAX_VALUE);
animator7.setRepeatCount(Integer.MAX_VALUE);
animator3.setDuration(10000);
animator.setDuration(2000);
animator2.setDuration(2000);
animator4.setDuration(2000);
animator5.setDuration(2000);
animator6.setDuration(2000);
animator7.setDuration(2000);
set.setInterpolator(new LinearInterpolator());//不停顿
set.play(animator).with(animator2).with(animator3).with(animator4).with(animator5).with(animator6).with(animator7);
set.start();
}
}
//动画使用
Loading loading = new Loading(MainActivity.this);
setContentView(loading.initConfig());