Tuesday, May 6, 2014

Ứng dụng hiệu ứng để chuyển activity trong Android





Một ví dụ nhỏ để hình dung animation trong Android

Tạo hiệu ứng chuyển activity

- Khi chuyển activity thay vì lật đơn giản chúng ta tạo hiệu ứng zoom-in và zoom-out để chuyển activity.

Bước 1. Tạo các hoạt cảnh với XML file

Tạo file in.xml và out.xml trong thư mục anim (lưu ý chính xác tên thư mục)

- Nôi dung in.xml

1 <set xmlns:android="http://schemas.android.com/apk/res/android" >
2 <alpha
3 android:duration="3000"
4 android:fillAfter="true"
5 android:fromAlpha="0.0"
6 android:toAlpha="1.0" >
7 </alpha>
8 </set>

- Nội dung out.xml


1 <set xmlns:android="http://schemas.android.com/apk/res/android" >
2 <alpha
3 android:duration="4000"
4 android:fillAfter="true"
5 android:fromAlpha="1.0"
6 android:toAlpha="0.0" >
7 </alpha>
8
9 <rotate
10 android:duration="3000"
11 android:fillAfter="true"
12 android:fromDegrees="0"
13 android:interpolator="@android:anim/accelerate_decelerate_interpolator"
14 android:pivotX="50%"
15 android:pivotY="50%"
16 android:toDegrees="360" >
17 </rotate>
18 </set>

Bước 2. Gán hình nền cho màn hình chào

Mở lại layout màn hình chào và gán thuộc tính background và đặt tên cho RelativeLayout. Hình s_1 của background là hình nằm trong thư mục drawable

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/bg"
4 android:background="@drawable/s_1"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:paddingBottom="@dimen/activity_vertical_margin"
8 android:paddingLeft="@dimen/activity_horizontal_margin"
9 android:paddingRight="@dimen/activity_horizontal_margin"
10 android:paddingTop="@dimen/activity_vertical_margin"
11 tools:context="vn.cusc.animation.Chao$PlaceholderFragment" >
12 </RelativeLayout>

Bước 3. Gán hiệu ứng rõ dần cho màn hình chào và hiệu ứng chuyển sang giao diện chính


1 public class Chao extends Activity {
2 RelativeLayout rl;
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_chao);
7 // tham chi?u d?n background activity
8 rl = (RelativeLayout)findViewById(R.id.bg);
9 // load ho?t hình cho màn hinh n?n
10 Animation a = AnimationUtils.loadAnimation(this, R.anim.in);
11 // ch?y hi?u ?ng rõ d?n
12 rl.setAnimation(a);
13 CountDownTimer t = new CountDownTimer(2000,1000) {
14 @Override
15 public void onTick(long millisUntilFinished) {
16 // c?u trúc màu ARGB trong dó A là alpha là d? trong su?t
17 }
18 @Override
19 public void onFinish() {
20 Intent i = new Intent(getApplicationContext(), Main.class);
21 startActivity(i);
22 overridePendingTransition(R.anim.in, R.anim.out);
23 finish();
24 }
25 };
26 t.start();
27 }
28 }
Bây giờ chạy lại ứng dụng quan sát hiệu ứng

No comments:

Post a Comment

Translate