Home > Backend Development > XML/RSS Tutorial > Detailed explanation of examples of writing animation in xml

Detailed explanation of examples of writing animation in xml

Y2J
Release: 2017-04-25 10:06:33
Original
2809 people have browsed it

1. Selector

The Selector in Android is mainly used to change the default background of the ListView and Button controls.

1. Create the mylist_view.xml file
First create a new drawable folder in the res directory, and then create a new mylist_view.xml in the newly created drawable folder. , its directory structure is: res/drawable/mylist_view.xml.
2. Edit the mylist_view.xml file according to specific needs
After creating a new mylist_view.xml file, its internal code structure is as follows when no attributes are added:

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

Now you can define the style you want internally according to the project requirements. The main attributes are as follows:

<?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>
Copy after login

3. Quote the mylist_view.xml file
There are three ways to reference the file you just created:
(1) Add the following attribute code to the ListView
android:listSelector="@drawable/mylist_view"
(2) In Add the following attribute code to the item interface of ListView
android:background="@drawable/mylist_view"
(3) Use JAVA code to write directly
Drawable drawable = getResources().getDrawable(R.drawable.mylist_view );
listView.setSelector(drawable);

In order to prevent the list from being blacklisted, you need to add the following attribute code to the ListView
android:cacheColorHint="@android:color/transparent "
Attribute introduction:
android:state_selected selected
android:state_focused gets focus
android:state_pressed click
android:state_enabled sets whether to respond to events, referring to all events


2. Write animation in XML
Animation can also be placed in XML files, which improves the maintainability of the program. The steps to write animation in XML are as follows
1. Create a new folder named anim under the res folder
2. Create an xml file, and first add the set tag, change the tag as follows

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

3. Add rotate, alpha, scale or translate tags to the tag
4. Use AnimationUtils to load the xml file in the code and generate the Animation object

Alpha animation

<?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);
Copy after login

Scale animation

<?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>
Copy after login

Rotate animation

<?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>
Copy after login

Translate animation

<?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>
Copy after login

Here we focus on android:pivotX and android:pivotY and android:fromXDelta,android:toXDelta
android :pivotX="50" uses absolute coordinates
android:pivotX="50%"relative to self
android:pivotX="50%p"relative to parent control


How are these animations called?
Call in 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>
Copy after login

注:在/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。
 

The above is the detailed content of Detailed explanation of examples of writing animation in xml. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template