首頁  >  文章  >  後端開發  >  在xml中寫動畫的實例詳解

在xml中寫動畫的實例詳解

Y2J
Y2J原創
2017-04-25 10:06:332693瀏覽

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檔案後,在沒有新增任何屬性時其內部程式碼結構為:

<?xml version="1.0" encoding="utf-8" ?>       
<selector xmlns:android="http://schemas.android.com/apk/res/android">        
</selector>

下面就可以根據專案需求,在其內部定義為自己想要的樣式了,主要屬性如下:

<?xml version="1.0" encoding="utf-8" ?>       
<selector xmlns:android="http://schemas.android.com/apk/res/android">     
<!-- 默认时的背景图片-->      
  <item android:drawable="@drawable/pic1" />        
<!-- 没有焦点时的背景图片 -->      
  <item android:state_window_focused="false"       
        android:drawable="@drawable/pic1" />       
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->      
  <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" />     
<!-- 触摸模式下单击时的背景图片-->      
<item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />      
<!--选中时的图片背景-->      
  <item android:state_selected="true"   android:drawable="@drawable/pic4" />       
<!--获得焦点时的图片背景-->      
  <item android:state_focused="true"   android:drawable="@drawable/pic5" />       
</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標籤,改標籤如下

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator">   
</set>

3.在該標籤當中加入rotate,alpha,scale或translate標籤
4.在程式碼當中使用AnimationUtils載入xml文件,並產生Animation物件

Alpha動畫

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <alpha    
        android:fromAlpha="1.0"    
        android:toAlpha="0.0"    
        android:startOffset="500"    
        android:duration="2000"    
            />      
</set>  
Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);  
iv.startAnimation(a);

Scale動畫

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <scale    
        android:fromXScale="1.0"    
        android:toXScale="0.0"    
        android:fromYScale="1.0"    
        android:toYScale="0.0"    
        android:pivotX="50%"    
        android:pivotY="50%"    
        android:duration="2000"    
    />      
</set>

Rotate動畫

#
<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <rotate    
        android:fromDegrees="0"    
        android:toDegrees="400"    
        android:pivotX="50%"    
        android:pivotY="50%"    
        android:duration="3000"    
    />      
</set>

Translate動畫

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android"    
    android:interpolator="@android:anim/accelerate_interpolator">    
    <translate    
        android:fromXDelta="50%"    
        android:toXDelta="100%"    
        android:fromYDelta="50%"    
        android:toYDelta="100%"    
        android:duration="3000"    
    />      
</set>

#這裡重點提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta
android :pivotX="50"使用絕對座標
android:pivotX="50%"相對自己
android:pivotX="50%p"相對父控制項


這些動畫怎麼調用的呢?
在styles.xml中呼叫:

#
<?xml version="1.0" encoding="utf-8"?>  
<resources>      
    <style mce_bogus="1" name="ThemeActivity">  
        <item name="android:windowAnimationStyle">@style/AnimationActivity</item>  
         <item name="android:windowNoTitle">true</item>  
    </style>      
    <style name="AnimationActivity" parent="@android:style/Animation.Activity" mce_bogus="1">  
        <item name="android:activityOpenEnterAnimation">@anim/translate</item>  
         <item name="android:activityOpenExitAnimation">@anim/rotate</item>  
          <item name="android:activityCloseEnterAnimation">@anim/close_enter</item>  
           <item name="android:activityCloseExitAnimation">@anim/close_exit</item>  
    </style>      
</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。
 

以上是在xml中寫動畫的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn