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中写动画的实例详解的细致内容,更多请关注ki4网别的相干文章!