动画---Animation
1、属性动画
文件位置:
res/animator/filename.xml
资源数据类型:
ValueAnimator
,ObjectAnimator
,orAnimatorSet
.
资源引用:
In Java: R.animator.filename
In XML: @[package:]animator/filename
语法:
<set android:ordering=["together" | "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </set> </set>
2、视图动画
1、Tween animation
-
文件位置:
-
res/anim/filename.xml
资源数据类型:
-
Animation
.
资源引用:
-
In Java:
R.anim.filename
In XML:@[package:]anim/filename
语法:
-
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <alpha android:fromAlpha="float" android:toAlpha="float" /> <scale android:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="float" android:toYDelta="float" /> <rotate android:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> <set> ... </set> </set>
2、Frame animation
res/drawable/filename.xml
AnimationDrawable
R.drawable.filename
In XML:
@[package:]drawable.filename
-
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" /> </animation-list>
状态颜色列表---Color List
文件位置;
res/color/filename.xml
资源数据类型:
ColorStateList
资源引用:
In Java: R.color.filename
In XML: @[package:]color/filename
语法:
-
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
图形处理资源
图片文件(Bitmap File)
res/drawable/filename.png
(.png
, .jpg
, or.gif
)
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/myimage" />
Resources res =getResources()
; Drawable drawable = res.getDrawable
(R.drawable.myimage);
Bitmpa XML
同上。
-
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:antialias=["true" | "false"] android:dither=["true" | "false"] android:filter=["true" | "false"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:mipMap=["true" | "false"] android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
Nine-Patch File
-
file location:
-
res/drawable/filename.9.png
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
NinePatchDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
example:
-
With an image saved at
res/drawable/myninepatch.9.png
, this layout XMLapplies the Nine-Patch to a View:<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch" />
XML Nine-Patch
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
NinePatchDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:dither=["true" | "false"] />
Layer List
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
LayerDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@[package:]drawable/drawable_resource" android:id="@[+][package:]id/resource_name" android:top="dimension" android:right="dimension" android:bottom="dimension" android:left="dimension" /> </layer-list>
State List
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
StateListDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
Level List设置Level替换图片
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
LevelListDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/drawable_resource" android:maxLevel="integer" android:minLevel="integer" /> </level-list>
eg:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/ff"
android:maxLevel="0"/>
<item
android:drawable="@drawable/hh"
android:maxLevel="11"/>
</level-list>
public void onClick(View v) {
pic.setImageLevel(11);
}
Transition Drawable渐变
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
TransitionDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@[package:]drawable/drawable_resource" android:id="@[+][package:]id/resource_name" android:top="dimension" android:right="dimension" android:bottom="dimension" android:left="dimension" /> </transition>
XML file saved at res/drawable/transition.xml
:
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/on" /> <item android:drawable="@drawable/off" /> </transition>
This layout XML applies the drawable to a View:
<ImageButton android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" />
And the following code performs a 500ms transition from the first item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);
==================================================
//点击触发
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TransitionDrawable drawable = (TransitionDrawable) ib.getDrawable();
drawable.startTransition(5000);
}
});
==================================================
Inset Drawable
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
InsetDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:insetTop="dimension" android:insetRight="dimension" android:insetBottom="dimension" android:insetLeft="dimension" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/filename"...
Clip Drawable被剪掉
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
ClipDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:clipOrientation=["horizontal" | "vertical"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
XML file saved at res/drawable/clip.xml
:
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/android" android:clipOrientation="horizontal" android:gravity="left" />
The following layout XML applies the clip drawable to a View:
<ImageView android:id="@+id/image" android:background="@drawable/clip" android:layout_height="wrap_content" android:layout_width="wrap_content" />
The following code gets the drawable and increases the amount of clipping in order toprogressively reveal the image:
ImageView imageview = (ImageView) findViewById(R.id.image); ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); drawable.setLevel(drawable.getLevel() + 1000);
Scale Drawable缩放
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
ScaleDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:scaleHeight="percentage" android:scaleWidth="percentage" />
eg:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ff"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
<ImageView
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/filename" />
ScaleDrawable scaleDrawable = (ScaleDrawable) ib.getDrawable();
scaleDrawable.setLevel(1); // level 1的时候就是50%
Shape Drawable形状
-
file location:
-
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
-
Resource pointer to a
GradientDrawable
.
resource reference:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
-
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>
XML file saved at res/drawable/gradient_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
This layout XML applies the shape drawable to a View:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
This application code gets the shape drawable and applies it to a View:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
Layout Resource