Cách cho TextView di chuyển khi bấm vàomàn hình Sau đây là các bước để tạo 1 cái TextView di động TweenAnimation: 1/ Các bạn tạo 1 project như sau: Project name: TweenAnimationExample Bu
Trang 1Cách cho TextView di chuyển khi bấm vào
màn hình
Sau đây là các bước để tạo 1 cái TextView di động (TweenAnimation):
1/ Các bạn tạo 1 project như sau:
Project name: TweenAnimationExample
Build Target: Android 2.3.3
Application name: TweenAnimationExample
Package name: org.example.TweenAnimationExample
Create Activity: TweenAnimationExample
2/ Các bạn chỉnh lại nội dung file main.xml như sau:
<? xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root"
>
< TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</ LinearLayout >
3/ Các bạn code file strings.xml như sau:
<? xml version="1.0" encoding="utf-8"?>
< resources >
< string name="hello"> TweenAnimationExample! Bấm vào để chạy chữ </ string >
< string name="app_name"> TweenAnimationExample </ string >
</ resources >
4/ Các bạn thêm vào phần res folder anim Sau đó thêm file rotate.xml vào folder anim mới tạo File này sẽ quy định cách thức dòng TextView di chuyển Code file rotate.xml như sau:
Trang 2<? xml version="1.0" encoding="UTF-8"?>
< rotate xmlns:android="http://schemas.android.com/apk/res/android"
5/ Bạn code lại file TweenAnimationExample.java như sau:
package org.example.TweenAnimationExample;
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.LinearLayout;
import android.widget.TextView;
public class TweenAnimationExample extends Activity {
/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate); a.reset();
final TextView rText = (TextView) findViewById(R.id.animatedText);
Trang 3LinearLayout layout = (LinearLayout) findViewById(R.id.root);
layout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
rText.startAnimation(a);
}
});
}
}
Cuối cùng, Debug ứng dụng và xem kết quả như sau:
Trang 4hoạt động Bạn nào có ý kiến đóng góp vui lòng post bài vào forum trang web
www.laptrinhdidong.vn Mình sẽ theo dõi forum thường xuyên ^^