Selector、动画之xml

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。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值