Adăugarea animațiilor în interfața aplicației crește șansa ca utilizatorii să o îndrăgească și mai mult, acestea sugerînd o impresie de calitate și o diferențiere de alte sute de aplicații.
Crearea unei animații este defapt foarte simplă, avem nevoie doar de puține bucăți de cod și crearea cîtorva fișiere XML. (utilizînd Eclipse)Animații GIF
1. Creăm un fișier XML în dosarul drawable. (ex: animation1.xml)
<?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/r1" android:duration="120" />
<item android:drawable="@drawable/r2" android:duration="120" />
<item android:drawable="@drawable/r3" android:duration="120" />
<item android:drawable="@drawable/r4" android:duration="120" />
<item android:drawable="@drawable/r5" android:duration="120" />
</animation-list>
duration - durata de vizualizare a imaginii
r1, r2, r3... - sunt imaginile din care se creează animația
2. Implementăm animația în Activitate
public class MainActivity extends Activity implements AnimationListener {
Animation animation1; private AnimationDrawable Animatie; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.animation_iv); imageView.setBackgroundResource(R.drawable.animation1); Animatie = (AnimationDrawable) imageView.getBackground(); }
@Override public void onWindowFocusChanged(boolean hasFocus) { if(hasFocus){
Animatie.start();
} }
3. Adăugăm imaginea în Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/animation_iv" android:layout_width="wrap_content" android:layout_height="wrap_content"/></FrameLayout>
Animația unui Obiect (buton, text...)
(Fade In; Fade Out; Blink; Zoom In; Zoom Out; Rotate; Move; Slide Up; Slide Down; Bounce)
1. Creăm un fișier XML în dosarul drawable. (ex: fade_in.xml)
<?xml version="1.0" encoding="utf-8"?>2. Implementăm animația în Activitate
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
public class MainActivity extends Activity implements OnClickListener, AnimationListener { Button but1; Animation anim1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.button1); but1.setOnClickListener(this);
anim1 = AnimationUtils.loadAnimation(getApplicationContext(), R.drawable.anim1); anim1.setAnimationListener(this); } @Override public void onWindowFocusChanged(boolean hasFocus) { if(hasFocus){ but1.setVisibility(View.VISIBLE); but1.startAnimation(anim1); } }
3. Adăugăm Obiectul în Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"Asta este tot de ce aveți nevoie pentru a face animații, mai jos vedeți codul pentru mai multe animații, jucațivă cu ele pentru a le înțelege sensul.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/imaginea1" />
</FrameLayout>
Fade In
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
alpha
android:duration
=
"1000"
android:fromAlpha
=
"0.0"
android:interpolator
=
"@android:anim/accelerate_interpolator"
android:toAlpha
=
"1.0"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
alpha
android:duration
=
"1000"
android:fromAlpha
=
"1.0"
android:interpolator
=
"@android:anim/accelerate_interpolator"
android:toAlpha
=
"0.0"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
alpha
android:fromAlpha
=
"0.0"
android:toAlpha
=
"1.0"
android:interpolator
=
"@android:anim/accelerate_interpolator"
android:duration
=
"600"
android:repeatMode
=
"reverse"
android:repeatCount
=
"infinite"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
scale
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:duration
=
"1000"
android:fromXScale
=
"1"
android:fromYScale
=
"1"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:toXScale
=
"3"
android:toYScale
=
"3"
>
</
scale
>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
scale
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:duration
=
"1000"
android:fromXScale
=
"1.0"
android:fromYScale
=
"1.0"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:toXScale
=
"0.5"
android:toYScale
=
"0.5"
>
</
scale
>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
rotate
android:fromDegrees
=
"0"
android:toDegrees
=
"360"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:duration
=
"600"
android:repeatMode
=
"restart"
android:repeatCount
=
"infinite"
android:interpolator
=
"@android:anim/cycle_interpolator"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:interpolator
=
"@android:anim/linear_interpolator"
android:fillAfter
=
"true"
>
<
translate
android:fromXDelta
=
"0%p"
android:toXDelta
=
"75%p"
android:duration
=
"800"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
scale
android:duration
=
"500"
android:fromXScale
=
"1.0"
android:fromYScale
=
"1.0"
android:interpolator
=
"@android:anim/linear_interpolator"
android:toXScale
=
"1.0"
android:toYScale
=
"0.0"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
scale
android:duration
=
"500"
android:fromXScale
=
"1.0"
android:fromYScale
=
"0.0"
android:interpolator
=
"@android:anim/linear_interpolator"
android:toXScale
=
"1.0"
android:toYScale
=
"1.0"
/>
</
set
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
android:interpolator
=
"@android:anim/bounce_interpolator"
>
<
scale
android:duration
=
"500"
android:fromXScale
=
"1.0"
android:fromYScale
=
"0.0"
android:toXScale
=
"1.0"
android:toYScale
=
"1.0"
/>
</
set
>
Ne găsiți pe Google Play sau www.fb.com/todgemphis