You are on page 1of 20

V THANH T

H CNG NGH, HQG H NI

Android
Animations
_____________
H NI, 05/ 2012

MC LC

Trang

Tng quan..................................................................................................................................................... 4
Phn 1. Frame-by-Frame Animation........................................................................................................... 4
1.1. V d v frame-by-frame animation ................................................................................................ 4
1.2. To Activity ....................................................................................................................................... 5
1.3. Thm Animation cho Activity .......................................................................................................... 5
Phn 2. Layout Animation ........................................................................................................................... 7
2.1. Cc kiu Tweening Animation c bn............................................................................................. 8
2.2. V d v layout animation ................................................................................................................ 8
2.3. To Activity v ListView .................................................................................................................. 9
2.4. To animation cho ListView .......................................................................................................... 10
Phn 3. View Animation ............................................................................................................................ 12
3.1. Tm hiu View Animation .............................................................................................................. 12
3.2. Thm Animation ............................................................................................................................. 14
3.3. S dng camera cm nhn c su .................................................................................. 16
3.4. Lp AnimationListener .................................................................................................................. 17
3.5. Mt s ch v ma trn bin i .................................................................................................. 18
Ti liu tham kho ..................................................................................................................................... 20

Tng quan
Animation (c th dch l hot nh) cho php mt i tng trn mn hnh c th thay i mu
sc, v tr, kch thc hay hng ca n theo thi gian. Animation trong Android rt thit thc,
vui nhn v n gin v vy chng c s dng rt thng xuyn.
Android 2.3 v cc phin bn trc h tr ba kiu animation: frame-by-frame animation,
layout animation, view animation (hai kiu sau thuc loi tweening animation).
Android 3.0 v cc bn sau tng cng kh nng Animation trong Android bng vic gii
thiu kh nng to hiu ng ng cho cc thuc tnh ca giao din ngi dng (UI).
Trong ti liu ny, chng ta s ln lt tm hiu v ba kiu animation frame-by-frame
animation, layout animation, view animation bng vic phn tch chng su hn thng qua
cc v d.

Phn 1. Frame-by-Frame Animation

Frame-by-frame animation l qu trnh n gin, hin th mt chui cc hnh nh lin tip


trong cc khong thi gian ngn to ra hiu ng cui cng l i tng di chuyn hoc thay
i. N cng ging nh hot ng ca cc my chiu phim vy.

1.1. V d v frame-by-frame animation


Hnh bn m t cc vng trn c cng kch c vi mt qu
bng c mu trn mi vng trn. Chng ta c th to ra
mt chui cc vng trn nh vy vi qu bng t ti cc
v tr khc nhau theo chu vi ca vng trn. Khi bn s
dng nhiu khung hnh nh trn, bn c th to ra hiu
ng ging nh qu bng di chuyn xung quanh vng trn
vy. Trong phn di y, chng ta s dng tm hnh nh
nh vy v chng c tn dng colored-ballN, c t
trong th mc /res/drawable.

1.2. To Activity
XML Layout cho v d
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textViewId1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debug Scratch Pad"
/>

<Button
android:id="@+id/startFAButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Activity load ImageView


public class FrameAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_animations_layout);
}
}

Bn c th gi activity ny qua mt intent nh on m di y,


Intent intent = new Intent(inActivity, FrameAnimationActivity.class);
inActivity.startActivity(intent);

1.3. Thm Animation cho Activity


Trong Android, vic thc hin frame-by-frame animation c thng qua mt class trong gi
ha c tn AnimationDrawable. Class Drawable cho php animation bng cch yu cu
container hoc view ca n gi mt class Runnable c bn v li Drawable bng cch s dng

mt tp cc tham s khc nhau. Ch rng, bn khng cn bit chi tit nhng th hin bn trong
nu s dng class AnimationDrawable.
s dng class AnimationDrawable, u tin chng ta to file XML nh ngha danh sch cc
frame t trong /res/drawable
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/colored_ball1" android:duration="50"
<item android:drawable="@drawable/colored_ball2" android:duration="50"
<item android:drawable="@drawable/colored_ball3" android:duration="50"
<item android:drawable="@drawable/colored_ball4" android:duration="50"
<item android:drawable="@drawable/colored_ball5" android:duration="50"
<item android:drawable="@drawable/colored_ball6" android:duration="50"
<item android:drawable="@drawable/colored_ball7" android:duration="50"
<item android:drawable="@drawable/colored_ball8" android:duration="50"
<item android:drawable="@drawable/colored_ball9" android:duration="50"
</animation-list>

/>
/>
/>
/>
/>
/>
/>
/>
/>

Cc tag trong animation-list v c bn s c chuyn thnh cc i tng AnimationDrawable


i din cho tp cc hnh nh. Sau , cn thit lp Drawable ny nh l background resource
cho ImageView,
view.setBackgroundResource(Resource.drawable.schemasframe_animation);

Sau khi thit lp, bn c th truy cp i tng AnimationDrawable nh sau


Object backgroundObject = view.getBackground();
AnimationDrawable ad = (AnimationDrawable)backgroundObject;

Khi c i tng AnimationDrawable, ta c th s dng hai phng thc start() v stop()


bt u hay kt thc animation. Hai phng thc quan trng khc l setOneShot() (chy
animation mt ln v sau dng li) v addFrame() (thm mt frame mi s dng mt i
tng Drawable v thit lp thi gian hin th ca n). t chng cnh nhau trong mt on m
hon chnh nh sau
public class FrameAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_animations_layout);
this.setupButton();
}
private void setupButton()
{
Button b = (Button)this.findViewById(R.id.startFAButtonId);
b.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v)
{
parentButtonClicked(v);
}
});
}

private void parentButtonClicked(View v)


{
animate();
}
private void animate()
{
ImageView imgView = (ImageView)findViewById(R.id.imageView);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
if (frameAnimation.isRunning())
{
frameAnimation.stop();
}
else
{
frameAnimation.stop();
frameAnimation.start();
}
}
}//eof-class

Ghi ch: Tham kho chng trnh hon chnh trong th mc SampleFrameAnimation trong file
nh km.

Phn 2. Layout Animation


Ging nh frame-by-frame, layout animation cng kh n gin. Ging nh tn gi ca n,
layout animation dnh ring cho mt s loi view c b tr mt cch c th. Chng hn, bn c
th s dng layout animation vi ListView v GridView (hai loi view thng dng layout b
cc). C th, bn s s dng layout animation thm cc hiu ng trc quan cho mi item
trong mt ListView hay GridView c hin th.
Trn thc t, bn c th dng loi animation ny cho tt c nhng loi c dn xut t mt
ViewGroup. Khc vi frame-by-frame, layout animation khng thng qua cc khung hnh lp li
m thay vo n thay i cc thuc tnh khc nhau ca mt view theo thi gian.
Mi mt view trong Android c mt ma trn bin i nh x view ti mn hnh. Bng cch
thay i ma trn theo mt s cch bn c th thc hin php co, php quay, tnh tin,... V d,
vic thay i trong sut ca view trong khong 0 n 1, bn c th thc hin php bin i
m ta gi l alpha animation.
Layout animation thuc kiu tweening animation.

2.1. Cc kiu Tweening Animation c bn


Scale animation (Co): Lm cho mt view nh hn hoc ln hn dc theo trc x hoc
dc theo trc y. Bn cng c th ch nh animation din ra xung quanh mt im
cht (pivot point).
Rotate animation (Quay): Quay mt view quanh mt im cht theo mt gc quay
xc nh.

Translate animation (Tnh tin): Tnh tin mt view dc theo trc x hoc dc theo
trc y.
Alpha animation (Alpha): Thay i trong sut ca mt view.

Ghi ch: Tham kho chng trnh minh ha trong th mc AndroidAnimButtons trong file
nh km.

Bn c th nh ngha nhng animation trn nh mt file XML t trong /res/anim. V d,


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>

Ta c th thy, cc tham s trong file XML trn u c "from" v "to" v chng ta phi xc nh
gi tr bt u v kt thc cho animation.

2.2. V d v layout animation


Khi c mt ListView, bn c th thm animation cho n to ra cc hiu ng trc quan cho
mi item trong ListView. V d, bn c th dng scale animation lm mt view ln dn ln t
kch thc 0 (zero) t kch thc tht ca n theo trc y. Hnh nh quan st c ging mt dng
text bt u nh mt ng k ngang v sau "to bo hn" (fatter) t n kch thc thc
t ca n.

2.3. To Activity v ListView


XML Layout nh ngha ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_view_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

on m cho Layout-Animation Activity


public class LayoutAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
setupListView();
}
private void setupListView()
{
String[] listItems = new String[] {
"Item 1", "Item 2", "Item 3",
"Item 4", "Item 5", "Item 6",
};
ArrayAdapter<String> listItemAdapter =
new ArrayAdapter<String>(this
,android.R.layout.simple_list_item_1
,listItems);
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.setAdapter(listItemAdapter);
}
}

Bn c th gi activity ny qua mt intent nh on m di y,


Intent intent = new Intent(inActivity, LayoutAnimationActivity.class);
inActivity.startActivity(intent);

Nh cc activity khc, ta cn ng k LayoutAnimationActivity trong file AndroidManifest.xml


<activity android:name=".LayoutAnimationActivity"
android:label="View Animation Test Activity">
</activity>

2.4. To animation cho ListView


nh ngha Scale Animation trong mt XML File
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>

Theo trc x, khuch i (magnification) bt u t 1 v vn gi nguyn l 1, ngha l list item


khng thay i theo trc x. Theo trc y, khuch i bt u t 0.1 v ti 1, ngha l i tng
s bt u vi kch thc nh gp 10 ln kch thc gc ca n v sau to dn ln n kch
thc gc ny.
Php co y thc hin trong 500 mili giy. im cht s nm gia (50%) theo c hng x v
y. Gi tr startOffset l s mili giy i trc khi bt u animation.
nh ngha Layout-Controller XML File
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/scale"/>

Gi s tn tp tin ny l list_layout_controller.xml. y l tp tin trung gian cn thit. N xc


nh rng cc animation trong list cn phi tin hnh ngc li (reverse) v animation cho mi
item s bt u vi s chm tr 30% i vi tng thi gian animation.
Ta cn sa file XML list_layout ListView tr ti file list_layout_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_view_id"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/list_layout_controller" />
</LinearLayout>

Cc dng thay i c in m.

File alpha.xml test Alpha Animation


<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />

Alpha animation c trch nhim kim sot s phai nht dn ca mu sc. Trong v d ny, ta
ang yu cu alpha animation i t "khng th nhn thy" (invisible color) n y mu
sc (full color) trong 1000 mili giy hay 1 giy. Thi gian cn thit ti thiu l 1 giy, nu khng
rt kh nhn ra s thay i ny. Mi khi mun thay i animation ca mt item ring l, ta
cn thay i file XML trung gian tr ti file animation mi ny.
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/alpha"/>

Dng thay i c in m.
Tip theo ta th mt animation l kt hp ca vic thay i v tr v thay i mu gradient.
Kt hp Translate v Alpha Animation
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="-100%" android:toYDelta="0"
android:duration="500" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
</set>

Translate animation s dch chuyn text t trn xung di trong khng gian hin th. Alpha
animation s thay i mu gradient t khng th nhn thy n c th thy. Thi gian thit lp l
500 cho php ngi dng nhn thc c s thay i. Tt nhin, chng ta cn thay i file
layoutAnimation trung gian ln na tham chiu n file ny. Gi s file XML kt hp trn
c tn l translate_alpha.xml, ta s thay i file layoutAnimation trung gian nh sau
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/translate_alpha"/>

Dng thay i c in m.
on m di y cho chng ta thy cch s dng rotate animation
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="0.0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500" />

on m trn th hin vic quay mi text item trong list mt vng xung quanh im gia ca
mi text item. Tip theo, bn cn thay i file layout controller XML v file ListView XML
layout v sau chy ng dng.
Ghi ch: Tham kho chng trnh hon chnh trong th mc SampleLayoutAnimation trong
file nh km.
Ngoi ra, cn c mt cng c khc lin quan n layout animation l interpolator.

Phn 3. View Animation

View animation l loi phc tp nht trong ba loi animation. View animation cho php ta to
hiu ng ng vi view ty bng vic thao tc vi ma trn bin i.

3.1. Tm hiu View Animation


Trong Android, vic hin th mt view c thng qua mt ma trn bin i. Trong cc ng
dng ha, bn s dng ma trn bin i chuyn i mt view theo mt cch no .
Qu trnh ny a u vo l tp cc ta im nh v s phi mu thnh mt tp cc ta
im nh v s phi mu mi. Sau qu trnh chuyn i, bn s thy mt bc tranh c
thay i v kch thc, v tr, hng hoc mu sc. Vi mt tp cc ta u vo, chng ta
c th biu din nhng php bin i thng qua php nhn ma trn.
Android a ra ma trn bin i ca mt view bng vic cho php bn ng k mt i
tng animation vi view .
Chng ta s xem xt mt v d. Chng hn, bn bt u vi mt activity, bn t mt
ListView vi mt vi items, tng t nh trong v d "Layout Animation". Chng ta to mt
nt bm trn cng mn hnh bt khi click vo ListView animation s bt u.

File XML Layout cho View-Animation Activity


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn_animate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation"
/>
<ListView
android:id="@+id/list_view_id"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

By gi, bn c th to activity hin th view v thit lp nt bm Start Animation.

on m cho View-Animation Activity, trc Animation


public class ViewAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
setupListView();
this.setupButton();
}
private void setupListView()
{
String[] listItems = new String[] {
"Item 1", "Item 2", "Item 3",
"Item 4", "Item 5", "Item 6",
};
ArrayAdapter<String> listItemAdapter =
new ArrayAdapter<String>(this
,android.R.layout.simple_list_item_1
,listItems);
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.setAdapter(listItemAdapter);
}
private void setupButton()
{
Button b = (Button)this.findViewById(R.id.btn_animate);
b.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v)
{
animateListView();

}
});
}
}

ng k vi file AndroidManifest.xml
<activity android:name=".ViewAnimationActivity"
android:label="View Animation Test Activity">
</activity>

v sau ta th gi activity ny t bt k menu item no qua intent


Intent intent = new Intent(this, ViewAnimationActivity.class);
startActivity(intent);

3.2. Thm Animation


Mc ch ca chng ta trong v d ny l thm animation ti ListView, bn cn mt class
ViewAnimation nhn c t android.view.animation.Animation. Sau , cn override phng
thc applyTransformation sa ma trn bin i. Khi c class ViewAnimation, bn c th
lm nh sau trong classListView
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.startAnimation(new ViewAnimation());

on m cho ViewAnimation class


public class ViewAnimation2 extends Animation
{
public ViewAnimation2(){}
@Override
public void initialize(int width, int height,
int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
setDuration(2500);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
matrix.setScale(interpolatedTime, interpolatedTime);
}
}

on m cho View-Animation Activity, bao gm Animation


public class ViewAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)

{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
setupListView();
this.setupButton();
}
private void setupListView()
{
String[] listItems = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6",
};
ArrayAdapter<String> listItemAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.setAdapter(listItemAdapter);
}
private void setupButton()
{
Button b = (Button)this.findViewById(R.id.btn_animate);
b.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v)
{
animateListView();
}
});
}
private void animateListView()
{
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.startAnimation(new ViewAnimation2());
}
}

Khi chy on m ny, bn s cm thy ci g k l. Thay v ln dn t gia mn hnh,


ListView li ln dn t gc trn bn tri. c c hiu ng mong mun, bn phi chuyn
ton b view sao cho trung tm ca view khp vi trung tm ca animation (trn cng bn tri).
Sau , p dng ma trn v di chuyn view tr li trung tm trc . Ta vit li on m thc
hin iu ny
View Animation s dng preTranslate v postTranslate
public class ViewAnimation3 extends Animation {
float centerX, centerY;
public ViewAnimation3(){}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
centerX = width/2.0f;
centerY = height/2.0f;
setDuration(2500);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
matrix.setScale(interpolatedTime, interpolatedTime);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

Cc phng thc preTranslate v postTranslate s thit lp mt ma trn trc khi scale v sau
khi scale. iu ny tng tng vi ba ma trn bin i. on m
matrix.setScale(interpolatedTime, interpolatedTime);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);

tng ng vi vic di chuyn ti mt trung tm khc, scale n v di chuyn n v trung tm


ban u.
Ghi ch: Tham kho chng trnh hon chnh trong th mc SampleViewAnimation trong file
nh km.

Tm hiu su hn v cc thuc tnh, phng thc ca i tng ma trn ti


http://developer.android.com/reference/android/graphics/Matrix.html

3.3. S dng camera cm nhn c su


Gi ha trong Android cn cung cp cho ta class Camera cho php cm nhn c su
bng cch chiu mt hnh nh 2D di chuyn trong khng gian 3D vo mt mt 2D. V d, bn c
th di chuyn mt ListView tr li t mn hnh bng vic dch 1300 pixel theo trc z v quay
360 quanh trc y.

public class ViewAnimation extends Animation {


float centerX, centerY;
public ViewAnimation(float cx, float cy)
{
centerX = cx;
centerY = cy;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
setDuration(2500);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
applyTransformationNew(interpolatedTime,t);
}
protected void applyTransformationNew(float interpolatedTime, Transformation t)
{

//Log.d("d","transform:" + interpolatedTime);
final Matrix matrix = t.getMatrix();
camera.save();
camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime));
camera.rotateY(360 * interpolatedTime);
camera.getMatrix(matrix);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
camera.restore();
}
}

3.4. Lp AnimationListener
Android s dng mt listener interface c tn AnimationListener gim st cc s kin
animation. Bn c th lng nghe cc s kin animation bng vic hin thc AnimationListener
interface v thit lp hin thc i vi class Animation.
public class ViewAnimationListener
implements Animation.AnimationListener {
private ViewAnimationListener(){}
public void onAnimationStart(Animation animation)
{
Log.d("Animation Example", "onAnimationStart");
}
public void onAnimationEnd(Animation animation)
{
Log.d("Animation Example", "onAnimationEnd");
}
public void onAnimationRepeat(Animation animation)
{
Log.d("Animation Example", "onAnimationRepeat");
}
}

Class ViewAnimationListener ch log message. Bn c th update phng thc animateListView


trong v d v view-animation t animation listener vo account
private void animateListView()
{
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
ViewAnimation animation = new ViewAnimation();
animation.setAnimationListener(new ViewAnimationListener());
lv.startAnimation(animation);
}

3.5. Mt s ch v ma trn bin i


Nh chng ta thy, ma trn l cha kha bin i view v animation. Di y l mt s
phng thc ch yu trn mt ma trn:
matrix.reset();
matrix.setScale();
matrix.setTranslate()
matrix.setRotate();
matrix.setSkew();
Phng thc u tin s reset ma trn v mt ma trn n v, n s khng gy ra thay i khi p
dng vo view. setScale thc hin vic thay i kch thc ma trn. setTranslate thc hin vic
thay i v tr m phng chuyn ng. setRotate thc hin vic thay i hng. setSkew thc
hin vic "bp mo" mt view.
Bn c th lin kt hoc nhn cc ma trn to nn cc hiu ng phc tp hn.
V d vi m1, m2, m3 l cc ma trn:
m1.setScale();
m2.setTranlate()
m3.concat(m1,m2)
Vic chuyn i mt view bi m1 c mt view mi, sau chuyn i view mi vi
m2 th cng tng ng vi vic chuyn i view ban u bi m3.
Mt preTranslate nh m1.preTranslate(m2) tng ng vi
m1.concat(m2,m1)
tng t, m1.postTranslate(m2) tng ng vi
m1.concat(m1,m2).
M rng, on code di y
matrix.setScale(interpolatedTime, interpolatedTime);
matrix.preTranslate(-centerX, -centerY);

matrix.postTranslate(centerX, centerY);
tng ng vi
Matrix matrixPreTranslate = new Matrix();
matrixPreTranslate.setTranslate(-centerX, -centerY);

Matrix matrixPostTranslate = new Matrix();


matrixPostTranslate.setTranslate(cetnerX, centerY);

matrix.concat(matrixPreTranslate,matrix);
matrix.postTranslate(matrix,matrixpostTranslate);

Ti liu tham kho


[1] Pro Android 3, Satya Komatineni, Dave MacLean , Sayed Y. Hashimi
[2] http://developer.android.com/reference/android/view/animation/package-summary.html: You
can discover various animation-related APIs here, including interpolators.
[3] http://developer.android.com/guide/topics/resources/animation-resource.html: You will find
XML tags for various animation types here.
[4] http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/:
Playing around with the Android Animation Framework
[5] http://android-er.blogspot.com/2012/02/apply-animation-on-button.html: Apply animation on

Button

You might also like