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中写动画的实例详解的详细内容。