Trung tâm Tin học – ĐH KHTN
Tạo ứng dụng hiển thị tin nhắn
Sau đây mình sẽ tạo 1 ứng dụng nho nhỏ demo cách hiển thị tin nhắn trên Android
Các bạn tạo 1 Project như sau:
Project name: myanimation
Build Target: Android 2.3.3
Application name: Myanimation
Package name: com.dac.myanimation
Create Activity: MyanimationActivity
Tiếp theo các bạn tạo 1 foder anim trong res/ và tạo 1 file animated.xml để xử lý hoạt
động cho ứng dụng
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android=" />android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="5000" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="3000" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="3000" />
<scale
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="3000"
android:fillBefore="false" />
</set>
Và các bạn tạo giao diện trong file main.xml như sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" />Lập trình Android – Page 1
Trung tâm Tin học – ĐH KHTN
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/myanimated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mail"
/>
<Button
android:id="@+id/startAnimated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="you’ve got a mail"
/>
</LinearLayout>
Các bạn lưu ý là phải bỏ 1 hình lá thư vào folder drawable-mdpi/ (hình cỡ trung bình
phù hợp với ứng dụng) và đổi tên hình vừa thêm thành mail
Và cuối cùng các bạn code file MyanimationActivity.java như sau:
package com.dac.myanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MyanimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView im
= (ImageView) this .findViewById(R.id.myanimated);
Lập trình Android – Page 2
Trung tâm Tin học – ĐH KHTN
final Animation an
= AnimationUtils.loadAnimation(this, R.anim.animated);
im.setVisibility(View.INVISIBLE);
Button bt = (Button) this.findViewById(R.id.startAnimated);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View view){
im.setVisibility(View.VISIBLE);
im.startAnimation(an);
}
});
}
}
Khi chạy ứng dụng, các bạn phải bấm vào button thì mới kích hoạt được hình động
Lập trình Android – Page 3