Android 动画学习 二

学习速度比较慢,坚持更新,积少成多。

首先介绍新发现的一个链接,介绍Material Design的动画的,还有效果展示:
http://www.mobile-open.com/2015/85188.html(Material Design动画用法大全)

一、触摸反馈Touch Feedback

      通过设置控件的xml文档的background属性为以下两个值来实现:

·        ?android:attr/selectableItemBackground 有编辑的涟漪效果.

·        ?android:attr/selectableItemBackgroundBorderless 超过控件边界的涟漪效果。边界受该控件的直接父类控制。可以设置mask drawable.在API21中引入。

1)xml实现方式:

   <Button android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch Feedback"
        android:stateListAnimator="@null"
        android:background="@drawable/rippledrawable1" 或android:background="?android:attr/selectableItemBackgroundBorderless"
        android:onClick="clicked"/>
其中rippledrawable1.xml类似如下格式:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/mask"
         android:drawable="@drawable/novel_book_bean_icon" />
</ripple>

2)代码通过RippleDrawable来实现。

Button button2 = (Button) findViewById(R.id.button2);
        Resources res = getResources();
        ColorStateList cl = res.getColorStateList(R.color.colorstatelist, getTheme());//设置不同状态的颜色列表
        RippleDrawable rd = null;
        if (cl != null) {
            Drawable mask = res.getDrawable(R.drawable.novel_book_bean_icon),getTheme());//设置mask drawable
            rd = new RippleDrawable(cl, null, mask);
        }
        if (rd != null) {
            button2.setBackground(rd);
        } 

3)自定义

   通过以下两种方法改变feedback的颜色:

    给RippleDrawable赋予一个颜色, 或使用

   在主题中使用android:colorControlHighlight 属性。


二,路径动画(CurvedMotion)

 按照官方文档创建了一个path interpolator(如下),可是却不找如何使用,网上也没有找到,有高人看到的话麻烦指点一下:

<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:controlX1="0.4"
    android:controlY1="0"
    android:controlX2="1"
    android:controlY2="1"/>

PathInterpolator是一个新添加的基于Bézier曲线或一个Path对象的interpolator. 
在material设计中,系统提供了三种基本的曲线:

·        @interpolator/fast_out_linear_in.xml

·        @interpolator/fast_out_slow_in.xml

·        @interpolator/linear_out_slow_in.xml

可以传递一个PathInterpolator对象给Animator.setInterpolator()方法。
ObjectAnimator类也提供了一个新的构造函数,可以同时为两个及以上的属性设置动画路径。如:

ObjectAnimator mAnimator;
mAnimator = ObjectAnimator.ofFloat(view,View.X,View.Y, path);
...
mAnimator.start();


三, View状态动画(Animate View State Changes)
1)StateListAnimator可以用来设置View状态改变时的动画。
使用步骤
第一步,使用xml来定义StateListAnimator:

<!-- animate the translationZ property of a view whenpressed -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <set>
      <objectAnimator android:propertyName="translationZ"
        android:duration="@android:integer/config_shortAnimTime"
        android:valueTo="2dp"
        android:valueType="floatType"/>
        <!-- you could have other objectAnimator elements
             here for "x" and"y", or other properties -->

    </set>
  </item>
  <item android:state_enabled="true"
    android:state_pressed="false"
    android:state_focused="true">
    <set>
      <objectAnimator android:propertyName="translationZ"
        android:duration="100"
        android:valueTo="0"
        android:valueType="floatType"/>
    </set>
  </item>
</selector>

第二步,
设置View的android:statelistAnimator属性即可。
或者使用代码:
        StateListAnimator animator = AnimatorInflater.loadStateListAnimator(this, R.drawable.statelistanimator1);
        Button button = (Button) findViewById(R.id.button1);
        button.setStateListAnimator(animator);
为控件设置状态改变动画。

2)AnimatedStateDrawable类可以在关联的view状态改变时,创建drawable来显示动画。一些Android 5.0系统控件已经默认使用这些动画。
定义方法如下:

<!-- res/drawable/myanimstatedrawable.xml -->
<animated-selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- providea different drawable for each state-->
    <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
        android:state_pressed="true"/>
    <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
        android:state_focused="true"/>
    <item android:id="@id/default"
        android:drawable="@drawable/drawableD"/>

    <!-- specifya transition -->
    <transition android:fromId="@+id/default" android:toId="@+id/pressed">
        <animation-list>
            <item android:duration="15" android:drawable="@drawable/dt1"/>
            <item android:duration="15" android:drawable="@drawable/dt2"/>
            ...
        </animation-list>
    </transition>
    ...
</animated-selector>


四,矢量动画(Vector Drawable)
使用AnimatedVectorDrawable类来实现矢量动画。
通过如下三个文件来定义Vector Drawable:

·        A vector drawable with the <vector> element in res/drawable/

·        An animated vector drawable with the <animated-vector> element in res/drawable/

·        One or more object animators with the <objectAnimator> element in res/anim/

具体文件示例如下:
1)Vector drawable

<!-- res/drawable/vectordrawable.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="64dp"
    android:width="64dp"
    android:viewportHeight="600"
    android:viewportWidth="600">
    <group
        android:name="rotationGroup"
        android:pivotX="300.0"
        android:pivotY="300.0"
        android:rotation="45.0" >
        <path
            android:name="v"
            android:fillColor="#000000"
            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
    </group>
</vector>

2)animatedvector drawable

<!-- res/drawable/animvectordrawable.xml -->
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  android:drawable="@drawable/vectordrawable" >
    <target
        android:name="rotationGroup"
        android:animation="@anim/rotation" />
    <target
        android:name="v"
        android:animation="@anim/path_morph" />
</animated-vector>

3)animation definitions

<!-- res/anim/rotation.xml -->
<objectAnimator
    android:duration="6000"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360" />

<!-- res/anim/path_morph.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="3000"
        android:propertyName="pathData"
        android:valueFrom="M300,70 l0,-70 70,70 0,0   -70,70z"
        android:valueTo="M300,70 l 0,-70 70,0  0,140-70,0 z"
        android:valueType="pathType" />
</set>

在代码中通过如下方式使用:
ImageView view2 = (ImageView) findViewById(R.id.imageview2);
        view2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Drawable dr= ((ImageView) v).getDrawable();
                if(dr instanceof Animatable) ((Animatable) dr).start();                
            }            
        });
以上是Material风格的动画,学习完毕。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值