Bu derste android property animasyonları konusuna giriş yapıyoruz. Bu animasyonu XML veya Java ile gerçekleştirebiliriz. Bu derste XML ile nasıl yaparız onu anlatmaya çalıştım.
Öncelikle bu animasyon dosyalarını res klasörü altına animator diye bir klasör yaratıp onun altında oluşturuyoruz.
Projenin sınıf ve layout kodları aşağıdadır. Tüm projeyi indirmek için lütfen burayı tıklayınız..
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="rotationX" android:duration="500" android:valueFrom="0" android:valueTo="-360" android:valueType="floatType"/> |
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="alpha" android:duration="500" android:valueFrom="1" android:valueTo="0" android:valueType="floatType"/> |
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="translationX" android:duration="500" android:valueFrom="0" android:valueTo="200" android:valueType="floatType"/> |
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="scaleX" android:duration="500" android:valueFrom="1.0" android:valueTo="2.5" android:valueType="floatType" /> |
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 |
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <objectAnimator android:propertyName="rotation" android:duration="500" android:valueFrom="0" android:valueTo="360" android:valueType="floatType"/> <set android:ordering="together" > <objectAnimator android:propertyName="scaleX" android:duration="500" android:valueFrom="1" android:valueTo="1.5" android:valueType="floatType"/> <objectAnimator android:propertyName="scaleY" android:duration="500" android:valueFrom="1" android:valueTo="1.5" android:valueType="floatType"/> </set> </set> |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
public class MainActivity extends AppCompatActivity { private ImageView mImage; private RelativeLayout rlayout; int control = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImage = (ImageView) findViewById(R.id.image); rlayout = (RelativeLayout) findViewById(R.id.main_relative_layout); rlayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator animator; if (control % 2 == 0) { animator = ObjectAnimator.ofObject(rlayout, "backgroundColor", new ArgbEvaluator(), Color.GREEN, Color.RED); } else { animator = ObjectAnimator.ofObject(rlayout, "backgroundColor", new ArgbEvaluator(), Color.RED, Color.GREEN); } animator.setDuration(3000); animator.start(); control++; } }); } public void rotateAnimation(View view) { /* Animator anim = AnimatorInflater .loadAnimator(this, R.animator.rotate); anim.setTarget(mImage); anim.start();*/ ObjectAnimator anim = ObjectAnimator.ofFloat(mImage, "rotation", 0f, 360.0f); anim.setDuration(500); anim.start(); } public void scaleAnimation(View view) { /* Animator anim = AnimatorInflater .loadAnimator(this, R.animator.scale); anim.setTarget(mImage); anim.start();*/ ObjectAnimator anim = ObjectAnimator.ofFloat(mImage, "scaleX", 1.0f, 2.0f); anim.setDuration(500); anim.start(); } public void translateAnimation(View view) { /* Animator anim = AnimatorInflater .loadAnimator(this, R.animator.translate); anim.setTarget(mImage); anim.start();*/ ObjectAnimator anim = ObjectAnimator.ofFloat(mImage, "translationX", 0.0f, 100f); anim.setDuration(500); anim.start(); } public void alphaAnimation(View view) { /* Animator anim = AnimatorInflater .loadAnimator(this, R.animator.alpha); anim.setTarget(mImage); anim.start();*/ ObjectAnimator anim = ObjectAnimator.ofFloat(mImage, "alpha", 1f, 0f); anim.setDuration(500); anim.start(); } public void setAnimation(View view) { /* Animator anim = AnimatorInflater .loadAnimator(this, R.animator.set); anim.setTarget(mImage); anim.start();*/ AnimatorSet rootSet = new AnimatorSet(); ObjectAnimator rotate = ObjectAnimator.ofFloat(mImage, "rotation", 0f, 360f); rotate.setDuration(500); AnimatorSet childSet = new AnimatorSet(); ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImage, "scaleX", 1.0f, 2.0f); scaleX.setDuration(300); ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImage, "scaleY", 1.0f, 2.0f); scaleY.setDuration(300); rootSet.play(rotate).before(childSet); childSet.play(scaleX).with(scaleY); rootSet.start(); } } |
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 |
<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="#ff1aff64" android:id="@+id/main_relative_layout" tools:context=".MainActivity"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/image" android:src="@drawable/tiling" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="100dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="100" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="Rotate" android:onClick="rotateAnimation" android:layout_weight="25" /> <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:layout_weight="25" android:text="Alpha" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="setAnimation" android:text="set" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/image" android:layout_alignStart="@+id/image"/> </RelativeLayout> |