zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - Animation: 视图动画(View Animation)自定义 Interpolator

    示例如下:

    /animation/AnimationDemo3.java

    /**
     * 视图动画(View Animation)自定义 Interpolator
     * 视图动画只是改变了 View 的视觉效果,而并没有改变 View 的属性(比如 left, top, right, bottom 之类的都是不变的)
     * View Animation(视图动画)即 Tween Animation(补间动画)
     * 视图动画支持插值器(用于计算不同时间点的动画结果的比例值),相当于 easing 动画,默认值是 LinearInterpolator 匀速变化
     *
     *
     * 继承 BaseInterpolator 实现一个自定义的 Interpolator 请参见 animation/AnimationDemo3CustomInterpolator.java
     */
    
    package com.webabcd.androiddemo.animation;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class AnimationDemo3 extends AppCompatActivity {
    
        private TextView _textView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_animation_animationdemo3);
    
            _textView1 = findViewById(R.id.textView1);
    
            sample();
        }
    
        private void sample() {
            // 定义一个动画
            TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f);
            translateAnimation.setDuration(2000);
            translateAnimation.setRepeatCount(-1);
            translateAnimation.setRepeatMode(Animation.REVERSE);
    
            // 实例化自定义的 Interpolator 用于实现一个先慢后快的效果
            AnimationDemo3CustomInterpolator customInterpolator = new AnimationDemo3CustomInterpolator(6f);
            translateAnimation.setInterpolator(customInterpolator);
    
            // 启动指定的动画
            _textView1.startAnimation(translateAnimation);
        }
    }
    

    animationAnimationDemo3CustomInterpolator.java

    /**
     * 自定义 Interpolator 实现一个先慢后快的效果
     *
     * Interpolator 用于计算不同时间点的动画结果的比例值
     */
    
    package com.webabcd.androiddemo.animation;
    
    import android.view.animation.BaseInterpolator;
    
    // 继承 BaseInterpolator 实现一个自定义的 Interpolator
    public class AnimationDemo3CustomInterpolator extends BaseInterpolator {
    
        private float mPow;
    
        /**
         * @param pow 值越大,先慢后快的效果越明显
         */
        public AnimationDemo3CustomInterpolator(float pow) {
            mPow = pow;
        }
    
        // 传入一个动画的时间点值(0 - 1 之间,0 对应动画开始的时间点,1 对应动画结束的时间点),返回插值结果(允许不在 0 - 1 之间)
        // 这个插值结果就是指定时间点的动画结果的比例值,这个比例值乘以(目标位置 - 初始位置)就是动画当前时间点的相对于初始位置的位置
        @Override
        public float getInterpolation(float input) {
            return (float) Math.pow(input, mPow);
        }
    }
    
    

    /layout/activity_animation_animationdemo3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFF0000" />
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    Java初学者的学习路线推荐
    C#基础(七)虚函数
    C#基础(六)--枚举的一些常用操作
    C#基础(五)--枚举的含义及其应用
    C#基础(四)--值类型和引用类型,栈和堆的含义
    C#基础(三)--运算符及条件控制语句
    Zipkin+Sleuth调用链监控集成和使用
    jenkins集成cucumber-resport报告
    微信小程序自动化,记录趟过的坑!
    解压版mysql+免破解版Navicat,好用!
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_animation_AnimationDemo3.html
Copyright © 2011-2022 走看看