xml动画详解

来源:互联网 发布:最后一战源码 编辑:程序博客网 时间:2024/04/30 00:36

1. Selector

Android中的Selector主要是用来改变ListView和Button控件的默认背景。

1.创建mylist_view.xml文件
首先在res目录下新建drawable文件夹,再在新建的drawable文件夹中新建mylist_view.xml,其目录结构为:res/drawable/mylist_view.xml。
2.根据具体需求编辑mylist_view.xml文件
新建mylist_view.xml文件后,在没有添加任何属性时其内部代码结构为:

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>       
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">        
  3. </selector>  
下面就可以根据项目需求,在其内部定义为自己想要的样式了,主要属性如下:
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>       
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">     
  3. <!-- 默认时的背景图片-->      
  4.   <item android:drawable="@drawable/pic1" />        
  5. <!-- 没有焦点时的背景图片 -->      
  6.   <item android:state_window_focused="false"       
  7.         android:drawable="@drawable/pic1" />       
  8. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->      
  9.   <item android:state_focused="true" android:state_pressed="true"   android:drawable"@drawable/pic2" />     
  10. <!-- 触摸模式下单击时的背景图片-->      
  11. <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />      
  12. <!--选中时的图片背景-->      
  13.   <item android:state_selected="true"   android:drawable="@drawable/pic4" />       
  14. <!--获得焦点时的图片背景-->      
  15.   <item android:state_focused="true"   android:drawable="@drawable/pic5" />       
  16. </selector>    
3.引用mylist_view.xml文件
三种方法可以来引用刚才创建的文件:
(1)在ListView中添加如下属性代码
android:listSelector="@drawable/mylist_view" 
(2)在ListView的item界面中添加如下属性代码
android:background="@drawable/mylist_view"  
(3)利用JAVA代码直接编写
Drawable drawable = getResources().getDrawable(R.drawable.mylist_view);   
listView.setSelector(drawable);  

为了防止列表拉黑的情况发生,需要在ListView中添加以下的属性代码
android:cacheColorHint="@android:color/transparent"  
属性介绍:
android:state_selected选中
android:state_focused获得焦点
android:state_pressed点击
android:state_enabled设置是否响应事件,指所有事件


2. 在XML中写动画
Animation也可以放在XML文件中,这样程序的可维护性提高了。在XML中写动画的步骤如下
1.在res文件夹下面新建一个名为anim的文件夹
2.创建xml文件,并首先加入set标签,改标签如下

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_interpolator">   
  3. </set>  
3.在该标签当中加入rotate,alpha,scale或者translate标签
4.在代码当中使用AnimationUtils加载xml文件,并生成Animation对象

Alpha动画
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:interpolator="@android:anim/accelerate_interpolator">    
  4.     <alpha    
  5.         android:fromAlpha="1.0"    
  6.         android:toAlpha="0.0"    
  7.         android:startOffset="500"    
  8.         android:duration="2000"    
  9.             />      
  10. </set>  
  11. Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);  
  12. iv.startAnimation(a);  
Scale动画
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:interpolator="@android:anim/accelerate_interpolator">    
  4.     <scale    
  5.         android:fromXScale="1.0"    
  6.         android:toXScale="0.0"    
  7.         android:fromYScale="1.0"    
  8.         android:toYScale="0.0"    
  9.         android:pivotX="50%"    
  10.         android:pivotY="50%"    
  11.         android:duration="2000"    
  12.     />      
  13. </set>  
Rotate动画
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:interpolator="@android:anim/accelerate_interpolator">    
  4.     <rotate    
  5.         android:fromDegrees="0"    
  6.         android:toDegrees="400"    
  7.         android:pivotX="50%"    
  8.         android:pivotY="50%"    
  9.         android:duration="3000"    
  10.     />      
  11. </set>  
Translate动画
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:interpolator="@android:anim/accelerate_interpolator">    
  4.     <translate    
  5.         android:fromXDelta="50%"    
  6.         android:toXDelta="100%"    
  7.         android:fromYDelta="50%"    
  8.         android:toYDelta="100%"    
  9.         android:duration="3000"    
  10.     />      
  11. </set>  
这里重点提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta
android:pivotX="50"使用绝对坐标
android:pivotX="50%"相对自己
android:pivotX="50%p"相对父控件


这些动画怎么调用的呢?
在styles.xml中调用:
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>      
  3.     <style mce_bogus="1" name="ThemeActivity">  
  4.         <item name="android:windowAnimationStyle">@style/AnimationActivity</item>  
  5.          <item name="android:windowNoTitle">true</item>  
  6.     </style>      
  7.     <style name="AnimationActivity" parent="@android:style/Animation.Activity" mce_bogus="1">  
  8.         <item name="android:activityOpenEnterAnimation">@anim/translate</item>  
  9.          <item name="android:activityOpenExitAnimation">@anim/rotate</item>  
  10.           <item name="android:activityCloseEnterAnimation">@anim/close_enter</item>  
  11.            <item name="android:activityCloseExitAnimation">@anim/close_exit</item>  
  12.     </style>      
  13. </resources>  
注:在/res 目录下新建 anim 目录,上面的Translate.xml,Scale.xml都是在这个文件夹下新建的。

3> Interpolator -- 定义动画变化的速率
① AccelerateDecelerateInterpolator:
   在动画开始和结束的地方速率改变比较慢,在中间的时候加速;
② AccelaerateInterPolotor:
    在动画开始的地方速率改变比较慢,然后开始加速;
③ CycleInterpolator:
动画循环播放特定的次数,速率沿着正弦曲线
④ DecelerateInterpolator:
在动画结束的地方速率比较慢
⑤ LinearInterpolator:
动画以匀速运动


在xml文件中定义Interpolator
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
这样所有的Animation共用一个Interpolator。
在代码中用代码设置如下
anim.setInterpolator(new AccelerateInterpolator());
在new一个AnimationSet中传入true则所有的Animation共用Interpolator。
0 0
原创粉丝点击