zoukankan      html  css  js  c++  java
  • 第七章——动画

    问题:

    一、Animation的种类

        ①、重要Animaion的使用  ②、如何在代码中使用

    二、帧动画的使用

        ①、使用   ②、容易造成的问题

    三、控制ListView的出场与退出的动画

    四、Activity的切换效果

         ①、常用切换效果的使用 

    五、属性动画

         ①、属性动画的三个类  ②、xml中配置属性动画  ③、代码中配置属性动画  ④、属性动画的监听器  ⑤、属性动画的原理   ⑥、通过属性动画修改Button的大小

    回答:

    一、Animation

    Animation的种类:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation

    ①、在layout中创建anim文件夹,然后创建Animation.xml文件

    Set:代表Animation的集合

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:shareInterpolator="true"
        android:fillAfter="true">
    <!--①、不设置pivotX和pivotY,则旋转点为左上角  ②、pivot可用百分比代替-->
        <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"/>
    
        <!--不设置fromYDelta则默认值为当前左上角的位置,toYDelta同-->
        <translate
            android:fromXDelta="0"
            android:toXDelta="300" />
    
        <!--scale的值是按照当前控件的倍数来算的-->
        <scale
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromXScale="1"
            android:fromYScale="1"
            android:toXScale="1.5"
            android:toYScale="1.5"/>
        <!--alpha的取值在0-1之间-->
        <alpha
            android:fromAlpha="0"
            android:toAlpha="1"/>
    </set>
    test_animation

    ②、怎么使用配置好的animation:

    public class MainActivity extends AppCompatActivity {
        private Button mButton;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mButton = (Button)findViewById(R.id.main_btn);
            //获取Animation
            Animation animation = AnimationUtils.loadAnimation(this,R.anim.test_animation);
            //将animation配置到mButton上,并启动
            mButton.setAnimation(animation);
            //设置Anmation监听器
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
        }
    }
    MainActivity

    关于属性的具体分析:http://blog.csdn.net/harvic880925/article/details/39996643

    得出的结论:①、translate的所有属性、Scale和Rotate的pivot属性都可用 

                      可以是数值、百分数、百分数p(父控件) 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起                   始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的                   50%做为起始点x轴坐标。

    补充:如何在代码中创建Animation:Animation有它相对应的类

            Animation animation1 = new TranslateAnimation(0,200,0,200);
            animation.setDuration(1000);
            mButton.setAnimation(animation);
    MainActivity

    二、使用帧动画(animation-list)

    ①、在drawable中创建xml

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@mipmap/ic_launcher" android:duration="100"/>
        <item android:drawable="@mipmap/ic_launcher" android:duration="100"/>
        <item android:drawable="@mipmap/ic_launcher" android:duration="100"/>
    </animation-list>
    test_list

    ②、缺点:因为引用过多的图片,容易造成oom

    三、控制ListView中的Item的出场(当然可用于其他的View)

    步骤1、创建layoutAnimation在anim文件夹中

    delay:表示下一个子View出现的延迟。如果不设置delay则所有的View都是一起出现的。

    <?xml version="1.0" encoding="utf-8"?>
    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animationOrder="normal"
    android:animation="@anim/test_in"
    android:delay="1">
    </layoutAnimation>
    test_layout

    步骤2、创建入场动画

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:fromXDelta="-100%"
            android:toXDelta="0"
            android:duration="1000"/>
    </set>
    test_into

    步骤3、配置在ListView上

    layout_anmation属性

        <ListView
            android:id="@+id/main_lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutAnimation="@anim/test_layout"></ListView>
    activity_main

    注意事项

    动画只是适用于初始化的过程,也就是刚开始填充Adaper的数据,之后再添加的数据是没有动画的。

    四、Activity的切换效果

    特效开源库:SwitchLayout

    https://github.com/jaychou2012/SwitchLayout

    使用:

    ①、在startActivity之后使用:且overridePendingTransition(int enter,int exit)

    enter指的是:下个Activity的入场东爱护

    exit指的是:当前Activity的退场动画

    ②、在finish()后使用:

    enter指的是:当前Activity的入场动画

    exit指的是:下一个Activity的退场动画

    五、属性动画的类

    ①、三个类

    ObjectAnimator:动画类的集合,坐了一些常用的封装

    ValueAnmator:是ObjectAnimator的超类,可扩展更多。

    AnimatorSet:是动画类的集合。

    ②、xml中配置属性动画

    首先在创建animator文件夹,创建test_animator.xml文件

    主要属性:

    propertyName:调用的方法

    valueType:输入参数的类型

    startOffset:开始的延迟时间

    其他的与Animation相同

    如何调用:

    Animator animatorSet = AnimatorInflater.loadAnimator(this,R.animator.test_object);
    animatorSet.start();
    代码调用

    ③、代码中使用属性动画

            //调用该控件的setWidth方法,并将Width设置为500
            Animator animator = ObjectAnimator.ofInt(mBtn,"width",500);
            animator.setDuration(1000);
            animator.start();
    属性动画

    ④、属性动画的监听器(改变Button的大小)

      final Button btn = (Button)findViewById(R.id.main_btn);
            ValueAnimator animator = ValueAnimator.ofInt(1,100);
            //每切换一帧就会调用该方法
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                private IntEvaluator intEvaluator = new IntEvaluator();
                private int start = 200;
                private int end = 300;
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    /*说明,调用了该句话
                    * ValueAnimator animator = ValueAnimator.ofInt(1,100);
                    * 监听默认采用 LinearInterpolator和intEvaluator
                    * */
                    int currentValue = (Integer) animation.getAnimatedValue();
                    Log.d("Activity",currentValue+"  ");
                    //获取当前进度的百分比
                    float fraction = animation.getAnimatedFraction();
                    ViewGroup.LayoutParams params = btn.getLayoutParams();
                    params.width = intEvaluator.evaluate(fraction,start,end);
                }
            });
    MainActivity

    属性动画的原理:

    ①、通过Interpolar插值器,用于判断当前时间和需要时间的百分比。控制动画的快慢。

    默认线性插值器:匀速。

    ②、得到百分比(fraction)之后,通过估值器(Evaluator)得到,当前输入数值的估值。

    ③、通过反射调用控件的方法。

    ⑤、修改Button的width的大小。

    原理:因为ObjectAnimation是不断通过反射调用相应方法从而改变控件的属性。所以我们只要制作出一个方法,该方法能够改变Button的width。

    一般的控件都有setWidth()方法,设置宽度。但是Button的setWidth方法,是用来设置其内部Text的宽度。所以直接调用自带的方法行不通。

    所以,我们先创建一个适配类,在该类中创建setWidth()方法,用来改变Button的width。

    但是同时我们也要创建getWidth(),因为如果对ObjectAnimator没有提供该控件的初始值(Button开始的值),则会调用getWidth()获取初始值。

    ①、创建适配类ViewTag

    class ViewTag {
            private View view;
            public ViewTag(View view) {
                this.view = view;
            }
    
            public void setWidth(int width){
                view.getLayoutParams().width = width;
                //记住改变之后需要刷新,才会显示出来
                view.requestLayout();
            }
    
            public int getWidht(){
                return view.getLayoutParams().width;
            }
        }
    ViewTag

    ②、调用

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button)findViewById(R.id.main_btn);
            ViewTag tag = new ViewTag(btn);
            ObjectAnimator.ofInt(tag,"width",200).setDuration(1000).start();
        }
    View Code



  • 相关阅读:
    有线电视网络(最小割)
    太空飞行计划问题(最小割,最大权闭合图,网络流24题)
    攻击装置(最小割,最大权独立集)
    王者之剑(最小割,最大独立集)
    善意的投票(最小割)
    有向图破坏(最小割,最小点权覆盖)
    线性代数(最小割,最大密度子图,TJOI2015)
    codewars--js--counting duplicates
    codewars--js--the highest and lowest number + JS 字符串和数组相关知识
    work
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5718152.html
Copyright © 2011-2022 走看看