Bu derste tween animasyonların ne olduğundan çeşitlerinden ve nasıl uygulandığından bahsedicez.
Android’de animasyonlar View ve Property Animasyon olarak ikiye ayrılır.
View Animasyonlar da Tween ve Frame Animasyon olarak ikiye ayrılır.. View animasyonda animasyon sonucu oluşan değerler geçicidir, property de kalıcıdıır. Siz bir animasyonla bir viewın rengini değiştirdiyseniz, view animasyonda bu işlem sonrası eski haline geri dönerken, property animasyonda da ise ilk haline geri dönmez.
Tween animasyon olarak alpha, rotate, translate ve scale işlemlerini göreceğiz.
Tween animasyonları res klasörü altına anim diye klasör açıp onun altında xml olarak tanımlıyoruz.
Tüm projeyi indirmek için BURAYI TIKLAYIN….
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" android:repeatMode="reverse" android:repeatCount="2" /> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="-180" android:duration="1000" android:pivotX="50%" android:pivotY="50%" android:repeatMode="reverse" android:repeatCount="2" /> |
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="150" android:fromYDelta="0" android:toYDelta="0" android:duration="1000" /> |
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:toXScale="1.5" android:fromYScale="1.0" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener { private ImageView mImageView; private Animation mRotateAnim; private Animation mScaleAnim; private Animation mTranslateAnim; private Animation mAlphaAnim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.volleyball); } public void scaleAnimation(View view) { mScaleAnim = AnimationUtils.loadAnimation(this, R.anim.scale_anim); mScaleAnim.setAnimationListener(this); mImageView.startAnimation(mScaleAnim); } public void translateAnimation(View view) { mTranslateAnim = AnimationUtils.loadAnimation(this, R.anim.translate_anim); mTranslateAnim.setAnimationListener(this); mImageView.startAnimation(mTranslateAnim); } public void alphaAnimation(View view) { mAlphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim); mAlphaAnim.setAnimationListener(this); mImageView.startAnimation(mAlphaAnim); } public void rotateAnimation(View view) { mRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim); mRotateAnim.setAnimationListener(this); mImageView.startAnimation(mRotateAnim); } @Override public void onAnimationStart(Animation animation) { // if(animation==mRotateAnim) Toast.makeText(this, "Animasyon Başladı", Toast.LENGTH_LONG).show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(this, "Animasyon Bitti", Toast.LENGTH_LONG).show(); } @Override public void onAnimationRepeat(Animation animation) { Toast.makeText(this, "Animasyon Tekrar Etti", Toast.LENGTH_LONG).show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<RelativeLayout 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" android:background="#ff37ff23" tools:context=".MainActivity"> <ImageView android:layout_width="150dp" android:layout_height="150dp" android:id="@+id/volleyball" android:src="@drawable/volleyball" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="100dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="100" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="Rotate" android:onClick="rotateAnimation" android:layout_weight="25" android:id="@+id/button2"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="scaleAnimation" android:layout_weight="25" android:text="Scale"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="translateAnimation" android:layout_weight="25" android:text="Trans" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="alphaAnimation" android:text="Alpha" android:layout_weight="25" android:id="@+id/button" /> </LinearLayout> </RelativeLayout> |