Bu dersimizde iki activity arasında geçiş yaparken kullanacağımız transition efeklerden biri olan explode efektini hem java ile hem de xml ile gerçekleştiricez.
Bunun için genel bir yaklaşım belirliyoruz. Açılacak olan ikinci activity’e ilgili animasyonun türünü, başlığını ve adını gönderiyoruz. Çünkü bundan sonraki derslerde farklı efekt türlerini kullanıcaz ve her biri için farklı bir activity oluşturmamıza gerek kalmayacak. Bunun için ilgili butonun tıklanma methoduna
if (Build.VERSION.SDK_INT >= 21) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this);
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(“TUR”, “EXPLODEJAVA”);
intent.putExtra(“BASLIK”, “EXPLODE ANIMASYON”);
intent.putExtra(“AD”, “Java ile Explode”);
startActivity(intent, options.toBundle());
} else {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
yazıyoruz.
Bir transition efektini xml ile gerçekleştirmek istiyorsak eğer res klasörü altına transition adında bir klasör oluşturmamız gerekiyor. Daha sonra bir transition nesnesi yaratıp buna TransitionInflater.from(this). inflateTransition(R.transition.explode) diyerek ilgili dosyayı atıyoruz. Daha sonra getWindow().setEnterTransition diyip efekti uyguluyoruz.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <transitionSet xmlns:android="http://schemas.android.com/apk/res/android"> <explode android:duration="1000" android:interpolator="@android:interpolator/cycle" > </explode> </transitionSet> |
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 112 113 114 115 |
public class Activity2 extends AppCompatActivity { String tur, baslik, ad; Button btnCikis; TextView tvAnimasyonAdi; @Override protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); ilkleme(); tur=getIntent().getStringExtra("TUR"); baslik=getIntent().getStringExtra("BASLIK"); ad=getIntent().getStringExtra("AD"); getSupportActionBar().setTitle(baslik); getSupportActionBar().setDisplayHomeAsUpEnabled(true); tvAnimasyonAdi.setText(ad); animasyonUygula(); getWindow().setAllowEnterTransitionOverlap(false); } private void animasyonUygula() { switch (tur){ case "EXPLODEJAVA": if(Build.VERSION.SDK_INT>=21) { Explode enterTransition = new Explode(); enterTransition.setDuration(1500); getWindow().setEnterTransition(enterTransition); } break; case "EXPLODEXML": if(Build.VERSION.SDK_INT>=21) { Transition enterTransition = TransitionInflater.from(this).inflateTransition(R.transition.explode); getWindow().setEnterTransition(enterTransition); } break; case "SLIDEJAVA": if(Build.VERSION.SDK_INT>=21) { Slide enterTransition=new Slide(); enterTransition.setDuration(1500); enterTransition.setSlideEdge(Gravity.LEFT); getWindow().setEnterTransition(enterTransition); } break; case "SLIDEXML": if(Build.VERSION.SDK_INT>=21) { Transition enterTransition = TransitionInflater.from(this).inflateTransition(R.transition.slide); getWindow().setEnterTransition(enterTransition); } break; case "FADEJAVA": if(Build.VERSION.SDK_INT>=21) { Fade enterTransition=new Fade(); enterTransition.setDuration(5500); enterTransition.setInterpolator(new OvershootInterpolator()); getWindow().setEnterTransition(enterTransition); } break; case "FADEXML": if(Build.VERSION.SDK_INT>=21) { Transition enterTransition = TransitionInflater.from(this).inflateTransition(R.transition.fade); getWindow().setEnterTransition(enterTransition); } break; } } private void ilkleme() { btnCikis= (Button) findViewById(R.id.btnCikis); tvAnimasyonAdi= (TextView) findViewById(R.id.tvAnimasyonAdi); btnCikis.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override public boolean onSupportNavigateUp() { if(Build.VERSION.SDK_INT>=21) { finishAfterTransition(); } else { finish(); } return true; } } |
Bu derste yazılan uygulamanın kodları aşağıdaki gibidir, tüm projeyi INDIRMEK ICIN TIKLAYIN….
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" tools:context="com.example.emrealtunbilek.materialanimation.MainActivity"> <TextView android:text="Ripple Effect with Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvRippleEffectwithBorder" android:background="?android:attr/selectableItemBackground" android:layout_marginBottom="10dp" android:textSize="22sp" /> <TextView android:text="Ripple Effect without Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvRippleEffectwithoutBorder" android:background="?android:attr/selectableItemBackgroundBorderless" android:layout_marginBottom="10dp" android:textSize="22sp" /> <TextView android:text="Custom Ripple with Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="22sp" android:background="@drawable/ripple_with_border" android:id="@+id/tvCustomRippleWithBorder" /> <TextView android:text="Custom ripple without Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="22sp" android:background="@drawable/ripple_without_border" android:id="@+id/tvCustomRippleWithoutBorder" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EXPLODE JAVA" android:layout_weight="1" android:id="@+id/btnExplodeJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="EXPLODE XML" android:layout_marginRight="10dp" android:id="@+id/btnExplodeXML" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SLIDE JAVA" android:layout_weight="1" android:id="@+id/btnSlideJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="SLIDE XML" android:layout_marginRight="10dp" android:id="@+id/btnSlideXML" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FADE JAVA" android:layout_weight="1" android:id="@+id/btnFadeJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fade XML" android:layout_marginRight="10dp" android:id="@+id/btnFadeXML" /> </LinearLayout> </LinearLayout> |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" tools:context="com.example.emrealtunbilek.materialanimation.MainActivity"> <TextView android:text="Ripple Effect with Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvRippleEffectwithBorder" android:background="?android:attr/selectableItemBackground" android:layout_marginBottom="10dp" android:textSize="22sp" /> <TextView android:text="Ripple Effect without Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvRippleEffectwithoutBorder" android:background="?android:attr/selectableItemBackgroundBorderless" android:layout_marginBottom="10dp" android:textSize="22sp" /> <TextView android:text="Custom Ripple with Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="22sp" android:background="@drawable/ripple_with_border" android:id="@+id/tvCustomRippleWithBorder" /> <TextView android:text="Custom ripple without Border" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="22sp" android:background="@drawable/ripple_without_border" android:id="@+id/tvCustomRippleWithoutBorder" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EXPLODE JAVA" android:layout_weight="1" android:id="@+id/btnExplodeJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="EXPLODE XML" android:layout_marginRight="10dp" android:id="@+id/btnExplodeXML" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SLIDE JAVA" android:layout_weight="1" android:id="@+id/btnSlideJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="SLIDE XML" android:layout_marginRight="10dp" android:id="@+id/btnSlideXML" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FADE JAVA" android:layout_weight="1" android:id="@+id/btnFadeJava" android:layout_marginRight="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fade XML" android:layout_marginRight="10dp" android:id="@+id/btnFadeXML" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="20dp" android:gravity="center" android:id="@+id/shared_element_layout"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_margin="10dp" android:id="@+id/ivStar" android:tint="@android:color/holo_blue_dark" android:src="@drawable/star"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/tvSharedElement" android:text="Shared Element"/> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@android:color/holo_orange_dark"> <item android:id="@android:id/mask" android:drawable="@android:color/white"/> </ripple> |
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@android:color/holo_orange_dark"> </ripple> |