效果图:
1.添加依赖:
implementation 'com.android.support:support-dynamic-animation:27.1.1'1
2.主函数代码:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.yechaoa.materialdesign.R;
public class Animation2Activity extends AppCompatActivity {
private GamePauseMenu gamePauseMenu;
private Button button;
int can = 0;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation2);
gamePauseMenu = findViewById(R.id.gamePauseMenu);
button = findViewById(R.id.button);
gamePauseMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gamePauseMenu.hideAnimal();
can = 0;
}
});
gamePauseMenu.setMenuListener(new GamePauseMenu.IGamePauseMenuListener() {
@Override
public void onExit() {
gamePauseMenu.hideAnimal();
can = 0;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(can == 0){
gamePauseMenu.showAnimal();
can = 1;
}else {
}
}
});
}
}
3.自定义封装动画方法类:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.animation.DynamicAnimation;
import android.support.animation.SpringAnimation;
import android.support.animation.SpringForce;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.yechaoa.materialdesign.R;
/**
* 自定义封装动画方法类
*/
public class GamePauseMenu extends RelativeLayout {
private IGamePauseMenuListener menuListener;
private ConstraintLayout constraintLayout;
private Button closeBtn;
private float height;
/**
* 定义监听接口
*/
public interface IGamePauseMenuListener {
void onExit();
}
public GamePauseMenu(Context context) {
super(context);
init(context);
}
public GamePauseMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GamePauseMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.view_pause_layout, this);
constraintLayout = child.findViewById(R.id.content);
closeBtn = child.findViewById(R.id.closeBtn);
closeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (menuListener != null) {
menuListener.onExit();
}
}
});
}
public void setMenuListener(IGamePauseMenuListener menuListener) {
this.menuListener = menuListener;
}
/**
* 开始动画
*/
public void showAnimal() {
setVisibility(View.VISIBLE);
SpringAnimation signUpBtnAnimY = new SpringAnimation(constraintLayout, DynamicAnimation.TRANSLATION_Y, 0);
signUpBtnAnimY.getSpring().setStiffness(SpringForce.STIFFNESS_LOW);
signUpBtnAnimY.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY);
signUpBtnAnimY.setStartVelocity(5000);
signUpBtnAnimY.start();
}
/**
* 结束动画
*/
public void hideAnimal() {
height = (ScreenTools.getScreenHeight(getContext()) - constraintLayout.getHeight()) / 2 + constraintLayout.getHeight() + ScreenTools.dp2px(getContext(),50);
ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 0f, -100f, height);
animator.setDuration(600);
animator.setInterpolator(new DecelerateInterpolator());
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
setVisibility(GONE);
reLayout();
}
});
animator.start();
}
private void reLayout(){
constraintLayout.setTranslationY(-height + 100);
}
}
4.获取分辨率工具类:
package com.yechaoa.materialdesign.activity.anims;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Display;
import java.lang.reflect.Method;
/**
* 获取分辨率工具类
*/
public class ScreenTools {
/**
* 获取屏幕宽度
* @param context
* @return
*/
public static int getScreenWith(Context context){
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.widthPixels;
}
/**
* 获取屏幕宽度
* @param context
* @return
*/
public static int getScreenHeight(Context context){
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.heightPixels;
}
/**
* 通过反射,获取包含虚拟键的整体屏幕高度
*
* @return
*/
public static int getHasVirtualKey(Activity activity) {
int dpi = 0;
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
dpi = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
/**
* 用于计算地图页,每个点的坐标,规定地图标准宽度为1000
* @param width 原地图尺寸
* @param realWidth 当前要显示的view的父view 的宽度
* @param changePoint 要计算的点
* @return
*/
public static double getScaleWidth(double width,double realWidth,double changePoint){
return realWidth / width * changePoint;
}
/**
* 用于计算地图页,每个点的坐标
* @param height 原地图尺寸
* @param realHeight 当前要显示的view的父view 的高度
* @param changePoint 要计算的点
* @return
*/
public static double getScaleHeight(double height,double realHeight, double changePoint){
return realHeight / height * changePoint;
}
/**
* dp转换成px
*/
public static int dp2px(Context context,float dpValue){
float scale=context.getResources().getDisplayMetrics().density;
return (int)(dpValue*scale+0.5f);
}
/**
* px转换成dp
*/
public static int px2dp(Context context,float pxValue){
float scale=context.getResources().getDisplayMetrics().density;
return (int)(pxValue/scale+0.5f);
}
/**
* sp转换成px
*/
public static int sp2px(Context context,float spValue){
float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue*fontScale+0.5f);
}
/**
* px转换成sp
*/
public static int px2sp(Context context,float pxValue){
float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue/fontScale+0.5f);
}
}
5.布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.anims.Animation2Activity">
<com.yechaoa.materialdesign.activity.anims.GamePauseMenu
android:id="@+id/gamePauseMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示菜单"
tools:layout_editor_absoluteX="120dp"
tools:layout_editor_absoluteY="114dp"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>