demo下载地址:
demo地址:https://github.com/pengcaihua123456/shennandadao
功能分析:
包含:移动,缩放,旋转!!!
组合自定义View
动画直接写在控件里面进行控制。
差值器的使用
动画监听的使用
总结:该动画可用组合控件 + AnimatorSet 实现
动画不生效的原因:
1.属性值写错了,就会有问题translationY
分析:
下落的时候,阴影变小。
上抛的时候阴影变大(监听动画执行完毕)
下落的时候速度越来越快(差值器)
上抛的时候速度比较慢(差值器)
上抛的时候进行旋转
动画监听:有几种方式
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
startUpAnimaor();//可以把这3个动画写在一起
//进行旋转
startRoration();
}
});
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/shapeLoadingView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher"
android:layout_marginTop="4dp" />
<View
android:id="@+id/indication"
android:layout_width="23dp"
android:layout_height="3dp"
android:layout_centerHorizontal="true"
android:background="@color/black"
android:layout_marginTop="82dp"
/>
<TextView
android:id="@+id/promptTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/indication"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="玩命加载中..."
android:textColor="#757575"
android:textSize="14sp" />
</LinearLayout>
/**
* Description: 类作用描述
*
* @Author:
* Date: 2021/5/7 18:02
**/
public class LoadingView extends LinearLayout {
public LoadingView(Context context) {
this(context, null);
}
public LoadingView(Context context, @Nullable AttributeSet attrs) {
this(context, null, 0);
initLayout();
}
public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
Context mContext;
ImageView imageView;
View view_shadow;
private void initLayout() {
View view = View.inflate(mContext, R.layout.item, null);
imageView = view.findViewById(R.id.shapeLoadingView);
view_shadow = view.findViewById(R.id.indication);
addView(view);
startFallAnimator();
}
private void startFallAnimator() {
Log.d("LoadingView", "startFallAnimator");
ObjectAnimator fallobjectAnimator = ObjectAnimator.ofFloat(imageView, "translationY", 0, 200f);
fallobjectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
ObjectAnimator scallObjectAnimator = ObjectAnimator.ofFloat(view_shadow, "scaleX", 1f, 0.2f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(fallobjectAnimator, scallObjectAnimator);
animatorSet.setDuration(3000);
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
startUpAnimaor();//可以把这3个动画写在一起
//进行旋转
startRoration();
}
});
}
private void startUpAnimaor() {
Log.d("LoadingView", "startUpAnimaor");
ObjectAnimator fallobjectAnimator = ObjectAnimator.ofFloat(imageView, "translationY", 200, 0f);
fallobjectAnimator.setInterpolator(new DecelerateInterpolator());
ObjectAnimator scallObjectAnimator = ObjectAnimator.ofFloat(view_shadow, "scaleX", 0.2f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(fallobjectAnimator, scallObjectAnimator);
animatorSet.setDuration(2000);
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
startFallAnimator();
}
});
}
private void startRoration() {
ObjectAnimator rorateObjectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 180f);
rorateObjectAnimator.setDuration(3000);
rorateObjectAnimator.start();
}
}