Lập trình Android: Chữ chạy (TweenAnimation) ppt

4 1K 3
Lập trình Android: Chữ chạy (TweenAnimation) ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Cá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:id="@+id/animatedText" 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: Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN <?xml version="1.0" encoding="UTF-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:toYScale="0.0" android:pivotX="40%" android:pivotY="30%" android:duration="2000" /> 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); Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN LinearLayout 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: Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN Bạn có thể bấm vào bất cứ vùng bào trong màn hình Virtual để xem cách ứng dụng hoạ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 ^^ Lập trình Android – http://laptrinhdidong.vn Page 4 . xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:toYScale="0.0" android:pivotX="40%" android:pivotY="30%" android:duration="2000". 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:id="@+id/animatedText" . > <TextView android:id="@+id/animatedText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 3/

Ngày đăng: 07/08/2014, 08:22

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan