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"/>
·@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>
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 inres/drawable/
·An animated vector drawable with the<animated-vector>element inres/drawable/
·One or more object animators with the<objectAnimator>element inres/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>
<!-- 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>
<!-- 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>
view2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Drawable dr= ((ImageView) v).getDrawable();
if(dr instanceof Animatable) ((Animatable) dr).start();
}
});
以上是Material风格的动画,学习完毕。
更多相关文章
- PopupWindow软键盘弹出遮挡输入框
- android是如何加载资源图片的
- Android(安卓)Studio如何更改JDK和SDK的路径?
- 【Android】Service中判断当前是否全屏(状态栏是否隐藏)
- android中checkbox的padding引发的问题
- Android(安卓)studio 百度地图开发(2)地图定位
- android AndroidManifest.xml文件解析
- Android(安卓)Dialog Activity
- Android权限总结