• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在xml写动画的

武飞扬头像
PHP中文网
帮助24

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。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfhcfj
系列文章
更多 icon
同类精品
更多 icon
继续加载