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。

更多相关文章

  1. 在asp下通过xml打包网站文件的方法
  2. Android 淡入淡出动画xml配置代码展示
  3. dom4j解析xml文件代码示例
  4. 用Java解析XML文件的代码示例
  5. Android平移动画xml配置代码展示
  6. Android旋转动画xml配置的代码展示
  7. Android伸缩动画xml配置的代码展示
  8. src下xml等资源文件无法读取的问题在IDEA中如何解决
  9. 调用API生成RSS资源文件的实例详解

随机推荐

  1. Android screenOrientation 屏幕方向的设
  2. Android(安卓)4.0.3 联系人(通讯录)应用
  3. Android实用小技巧
  4. Android View MarqueeView 跑马灯效果
  5. 关于Android使用新浪API的一些说明
  6. Android高德地图自定义放大缩小控件
  7. 在android中使用OrmLite数据库框架
  8. Android map
  9. Mac 配置Android环境变量( 有图)
  10. Android 编辑框(EditText)属性