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"
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" />
<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.button.MaterialButton
android:id="@+id/registerfinish_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" />
<com.google.android.material.button.MaterialButton
android:id="@+id/backlogin_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>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
專案代碼倉庫
如果直接復制可能會出現代碼缺陷,完整代碼請去倉庫下載
如果覺得還行,耽誤您幾秒鐘的時間去我的倉庫點點star,萬一以后用到了呢?
UIDesign 開源專案
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/387932.html
標籤:其他
