Android 登录3D翻转动画效果
Posted 玖流之辈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 登录3D翻转动画效果相关的知识,希望对你有一定的参考价值。
android 登录3D翻转动画效果
描述:这是一个 登录3D翻转效果的Demo。
项目代码在最后面!!!!跳转到最后
控件效果如下:
实现功能:
- 使页面进行3D翻转(3D翻转效果)
- 可通过回调监听两个页面的显隐
设计核心:
主要的设计核心是依赖于Camera来进行视觉上深度的放大和缩小,而旋转则是依靠动画进行实现。
核心代码:
Rotate3dAnimation.java 一个3D的旋转动画效果,也是旋转动画的核心
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3dAnimation extends Animation
private float mCenterX,mCenterY;
private Camera mCamera;
private RotateListener rotateListener;
private boolean isBack=false;
private boolean ishalf=false;
private float mDegree=180;
public Rotate3dAnimation()
public void setRotateListener(RotateListener rotateListener)
this.rotateListener=rotateListener;
//翻转监听
public void setBack(boolean back)
isBack = back;
//初始化位置
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
super.initialize(width, height, parentWidth, parentHeight);
mCamera=new Camera();
mCenterX=(float) width/2;
mCenterY=(float)height/2;
//动画改变
//interpolatedTime 0~1
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
if (interpolatedTime>0.5&&interpolatedTime<=1)
if (!ishalf)
rotateListener.transhalf(isBack);
ishalf=true;
else if (interpolatedTime<0.5)
if (ishalf)
ishalf=false;
//是否已过一半
float degrees = mDegree * interpolatedTime;
if (isBack)
degrees =180- degrees;
mCamera.save();;
float z;
//Z轴高度500 默认-576
if (interpolatedTime<0.5)
z= 500*interpolatedTime*2;
else
z= 1000 *(1-interpolatedTime);
mCamera.translate(0,0,z);
final Matrix matrix=t.getMatrix();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
matrix.preTranslate(-mCenterX,-mCenterY);
matrix.postTranslate(mCenterX,mCenterY);
super.applyTransformation(interpolatedTime, t);
public interface RotateListener
void transhalf(boolean isback);//移动到一半 适合显隐界面
使用示例:
如效果图里的登录样例:
LoginActivity3d.java
import androidx.cardview.widget.CardView;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.ui.design.R;
import com.ui.design.main.base.BaseActivity;
import com.ui.design.main.constants.Constants;
import com.ui.design.view.rotateLogin3d.view.Rotate3dAnimation;
@Route(path = Constants.LoginActivity3d)
public class LoginActivity3d extends BaseActivity
private Rotate3dAnimation rotate3dAnimation;
@Override
protected int initLayout()
return R.layout.activity_login_activity3d;
@Override
protected void initView()
Button register_bt=findViewById(R.id.register_bt);
Button login=findViewById(R.id.login_bt);
Button registerfinish_bt=findViewById(R.id.registerfinish_bt);
Button backlogin_bt=findViewById(R.id.backlogin_bt);
LinearLayout linearLayout1=findViewById(R.id.linear1);
LinearLayout linearLayout2=findViewById(R.id.linear2);
CardView cardView=findViewById(R.id.cardView);
initAnimation();
rotate3dAnimation.setRotateListener((isback) ->
Log.e("TEST","roate");
if (isback)
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
else
linearLayout1.setVisibility(View.GONE);
linearLayout2.setVisibility(View.VISIBLE);
);
register_bt.setOnClickListener(v ->
rotate3dAnimation.setBack(false);
cardView.startAnimation(rotate3dAnimation);
);
login.setOnClickListener(v -> Toast.makeText(LoginActivity3d.this,"完成登录操作",Toast.LENGTH_SHORT).show());
registerfinish_bt.setOnClickListener(v -> Toast.makeText(LoginActivity3d.this,"完成注册操作",Toast.LENGTH_SHORT).show());
backlogin_bt.setOnClickListener(v ->
rotate3dAnimation.setBack(true);
cardView.startAnimation(rotate3dAnimation);
);
private void initAnimation()
rotate3dAnimation=new Rotate3dAnimation();
rotate3dAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
rotate3dAnimation.setDuration(2000);
rotate3dAnimation.setFillAfter(true);
@Override
protected void initData()
layout:activity_login_activity3d.xml
android:rotationY=“180” //非常关键 不然你的图是反的
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".view.rotateLogin3d.LoginActivity3d"
android:background="@drawable/jrtt_channel_bg_channel">
<include layout="@layout/title_include" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="400dp"
android:background="@color/white"
app:cardCornerRadius="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="登陆页"
android:textColor="@color/black"
android:textSize="15sp" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="250dp"
android:layout_height="40dp"
android:inputType="text"
android:layout_marginTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:background="@drawable/ic_radius_15dp_black_stroke"
android:textColor="@color/black"
android:hint="用户名"
android:textSize="12sp" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="250dp"
android:layout_height="40dp"
android:inputType="text"
android:layout_marginTop="10dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:background="@drawable/ic_radius_15dp_black_stroke"
android:textColor="@color/black"
android:hint="密码"
android:textSize="12sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_confim"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:hint="密码确认"
app:counterEnabled="true"
app:counterTextColor="@color/brown"
app:hintTextColor="@color/brown">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPassword"
android:textColorHint="@color/brown"
android:textSize="12sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/login_bt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="25dp"
android:backgroundTint="@color/brown"
android:text="登录"
android:textColor="@color/white"
app:cornerRadius="10dp"
app:rippleColor="@color/bisque" />
<com.google.android.material.button.MaterialButton
android:id="@+id/register_bt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="@color/lightgrey"
android:text="去注册"
android:textColor="@color/black"
app:cornerRadius="10dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:visibility="gone"
android:rotationY="180"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="注册页"
android:textColor="@color/black"
android:textSize="15sp" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="250dp"
android:layout_height="40dp"
android:inputType="text"
android:layout_marginTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
使用 electron 实现类似新版 QQ 的登录界面效果(阴影背景动画窗体3D翻转)