Android之欢迎页动画(视差、3D翻转、放大缩小)

本篇介绍app欢迎页的一些动画效果

(1)视差效果 是否看到每个view位置移动了、且移动速度不一致
这里写图片描述 这里写图片描述

(2)3D翻转
这里写图片描述

(2)放大缩小
这里写图片描述

一、 自定义动画

说到欢迎页一定会想到ViewPager,动画设置自然想到ViewPager.setPageTransformer(boolean, PageTransformer) ,所以我们先自定义一个PageTransformer的子类WelcomePagerTransformer 来实现我们想要的动画效果

public class WelcomePagerTransformer implements ViewPager.PageTransformer {
    @Override
    public void transformPage(View view, float position) {
        if (position < 1 && position > -1) {
            //拿到所有的View
            ViewGroup rlGroup = (ViewGroup) view.findViewById(R.id.rl_group);
            for (int i = 0; i < rlGroup.getChildCount(); i++) {
                View child = rlGroup.getChildAt(i);
                //设置视差效果,给当前的view来个加速度
                float factor = (float) Math.random() * 2;
                if (child.getTag() == null) {
                    child.setTag(factor);
                } else {
                    factor = (float) child.getTag();
                }
                child.setTranslationX(position * factor * child.getWidth());
            }
            //放大缩小
//            view.setScaleX(1-Math.abs(position));
//            view.setScaleY(1-Math.abs(position));
//            view.setScaleX(Math.max(0.9f,1-Math.abs(position)));
//            view.setScaleY(Math.max(0.9f,1-Math.abs(position)));
            //3D翻转
//            view.setPivotX(position < 0f ? view.getWidth() : 0f);
//            view.setRotationY(position * 90f);
            //3D内翻转
//            view.setPivotX(position < 0f ? view.getWidth() : 0f);
//            view.setPivotY(view.getHeight()*0.5f);
//            view.setRotationY(position * 60f);
            //偏移
//            view.setTranslationX(position < 0 ? 0f : -view.getWidth() * position);

        }

    }
}

二、 设置动画

在activity设置viewpage自定义的动画

 vpWelcome = (ViewPager)findViewById(R.id.vp_welcome);
 WelcomePagerTransformer welcomePagerTransformer = new WelcomePagerTransformer();
 vpWelcome.setPageTransformer(true,welcomePagerTransformer);

接下来就是设置viewpage的适配器FragmentPagerAdapter跟平常的欢迎页的操作是一样的,该怎么写就怎么写!

demo下载地址:https://github.com/972242736/WelcomeAnimation.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一直都很喜欢Instagram的快拍(Story)功能,也很喜欢他们的翻转效果,是一种简单的3D翻转效果。大致效果如下:instagramstory.gif貌似最近微博也出了一个差不多的Story的功能,用的翻转动画也是和Instagram一样。思路看到这样的效果,很容易想到用ViewPager的Transformer动画来实现。当然这种翻转效果网上也有相应的实现,就是以View的左边或右边为旋转轴进行空间上Y轴的旋转。于是很容易我们可以写出下面的代码public class StereoPagerTransformer implements ViewPager.PageTransformer {   private static final float MAX_ROTATE_Y = 90;   private final float pageWidth;   public StereoPagerTransformer(float pageWidth) {     this.pageWidth = pageWidth;   }   public void transformPage(View view, float position) {     view.setPivotY(view.getHeight() / 2);     if (position < -1) { // [-Infinity,-1)       // This page is way off-screen to the left.       view.setPivotX(0);       view.setRotationY(90);     } else if (position <= 0) { // [-1,0]       view.setPivotX(pageWidth);       view.setRotationY(position * MAX_ROTATE_Y);     } else if (position <= 1) { // (0,1]       view.setPivotX(0);       view.setRotationY(position * MAX_ROTATE_Y);     } else { // (1, Infinity]       // This page is way off-screen to the right.       view.setPivotX(0);       view.setRotationY(90);     }   } }然后运行代码看一下我们实现的效果:badtransformer.gif额,总觉哪里不对,嗯,就是动画的速度不对,上面的写法就是一个匀速进行的动画,效果非常不理想,这时我们就要重新写一个插值器(TimeInterpolator ),在尝试了系统自带的差值器后,发现效果仍然不是很理想。于是决定自己写一个插值器。关于插值器根据TimeInterpolator代码中的文档可以得知,插值器用于控制动画进行的速度,其输入值为0~1,输出值也为0~1。/**  * A time interpolator defines the rate of change of an animation. This allows animations  * to have non-linear motion, such as acceleration and deceleration.  */ public interface TimeInterpolator {     /**      * Maps a value representing the elapsed fraction of an animation to a value that represents      * the interpolated fraction. This interpolated value is then multiplied by the change in      * value of an animation to derive the animated value at the current elapsed animation time.      *      * @param input A value between 0 and 1.0 indicating our current point      *        in the animation where 0 represents the start and 1.0 represents      *        the end      * @return The interpolation value. This value can be more than 1.0 for      *         interpolators which overshoot their targets, or less than 0 for      *         interpolators that undershoot their targets.      */     float getInterpolation(float input); }经过简单的分析,这次我们的动画应该在动画前半段时进行缓慢一些(也就是输入值在0到某个值之间),在后半段时(也就是输入值在某个值到1之间)进行的快速一些。经过简单的调整,最终我写了如下的插值器private static final TimeInterpolator sInterpolator = new TimeInterpolator() {         @Override         public float getInterpolation(float input) {             if (input < 0.7) {                 return input * (float) pow(0.7, 3) * MAX_ROTATE_Y;             } else {                 return (float) pow(input, 4) * MAX_ROTATE_Y;             }         }     };再次运行代码,这次效果看上去好多了,哈哈,一个简单又好看的效果就完成了。goodtransformer.gif最后附上完整的代码:import android.animation.TimeInterpolator; import android.support.v4.view.ViewPager; import android.view.View; import static java.lang.Math.pow; /**  * @author wupanjie  */ public class StereoPagerTransformer implements ViewPager.PageTransformer {     private static final float MAX_ROTATE_Y = 90;     private static final TimeInterpolator sInterpolator = new TimeInterpolator() {         @Override         public float getInterpolation(float input) {             if (input < 0.7) {                 return input * (float) pow(0.7, 3) * MAX_ROTATE_Y;             } else {                 return (float) pow(input, 4) * MAX_ROTATE_Y;             }         }     };     private final float pageWidth;     public StereoPagerTransformer(float pageWidth) {         this.pageWidth = pageWidth;     }     public void transformPage(View view, float position) {         view.setPivotY(view.getHeight() / 2);         if (position < -1) { // [-Infinity,-1)             // This page is way off-screen to the left.             view.setPivotX(0);             view.setRotationY(90);         } else if (position <= 0) { // [-1,0]             view.setPivotX(pageWidth);             view.setRotationY(-sInterpolator.getInterpolation(-position));         } else if (position <= 1) { // (0,1]             view.setPivotX(0);             view.setRotationY(sInterpolator.getInterpolation(position));         } else { // (1, Infinity]             // This page is way off-screen to the right.             view.setPivotX(0);             view.setRotationY(90);         }     } }总结动画的灵魂在于它的插值器,插值器控制了动画进行的速度,这次我选择自己写了一个插值器作为练手,其实我这次写的插值器效果仍不是很平滑,动画的插值器也应该用贝塞尔曲线来制作,这样我们的动画就会进行的更平滑。具体大家可以参考自带的PathInterpolator,是在API 21以后引入的,当然它也有对应的兼容包。
以下是使用Android Studio创建欢迎面的步骤: 1. 在Android Studio中创建一个新项目。 2. 在项目中创建一个新的Activity,作为欢迎面。 3. 在欢迎面的布局文件中添加所需的UI元素,例如图片、文本等。 4. 在欢迎面的Java文件中添加所需的逻辑代码,例如倒计时等。 5. 在应用程序的主Activity中设置欢迎面为应用程序的启动面。 以下是一个简单的欢迎面的例子,其中包含一个倒计时按钮,点击该按钮将进入面1: 引用: Android Studio版本:Android Studio Arctic Fox | 2020.3.1 Patch 3 例程名称:WelcomePage。 ```xml <!-- activity_welcome.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/welcome_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcome_bg"> <ImageView android:id="@+id/welcome_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/welcome_logo" /> <Button android:id="@+id/countdown_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/welcome_logo" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="倒计时" android:textColor="@android:color/white" /> </RelativeLayout> ``` ```java // WelcomeActivity.java public class WelcomeActivity extends AppCompatActivity { private Button countdownButton; private CountDownTimer countDownTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); countdownButton = findViewById(R.id.countdown_button); countdownButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(WelcomeActivity.this, Page1Activity.class); startActivity(intent); finish(); } }); countDownTimer = new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { countdownButton.setText("倒计时 " + millisUntilFinished / 1000); } @Override public void onFinish() { Intent intent = new Intent(WelcomeActivity.this, Page1Activity.class); startActivity(intent); finish(); } }.start(); } @Override protected void onDestroy() { super.onDestroy(); countDownTimer.cancel(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值