Lập trình di động phần 2

270 11 0
Lập trình di động phần 2

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LẬP TRÌNH DI ĐỘNG Bài 8: Intent & Broadcast Receicer Nhắc lại trước  Các điều khiển hữu ích: ProgressBar, Progress Dialog, AutoComplete TextView, TimePicker, TimePicker Dialog, DatePicker, DatePicker Dialog, ListView, Spinner, WebView  Quá trình xây dựng giao diện: Thiết lập giao diện XML Cái khơng dùng XML viết onCreate Thiết lập liệu cho điều khiển Viết hàm xử lý kiện cho điều khiển  Vài kiểu viết mã xử lý kiện TRƯƠNG XUÂN NAM Nội dung Intent Intent filter Intent tường minh vs ngầm định Các thành phần intent Sử dụng intent để trao đổi liệu Broadcast Receiver Vòng đời boardcast receiver Tự tạo tín hiệu broadcast Viết receiver xử lý tín hiệu broadcast TRƯƠNG XUÂN NAM Phần Intent TRƯƠNG XUÂN NAM Intent & Intent Service  Intent chuẩn giao tiếp thành phần Android OS (activity, service, provider, receiver)  Intent giống thư: thơng tin cần thiết đóng gói bên intent (địa + nội dung)  Intent service dịch vụ hệ thống, vai trò người đưa thư: chuyển intent tới thành phần nhận phù hợp (chiếu theo địa ghi intent) TRƯƠNG XUÂN NAM Ví dụ sử dụng Intent Intent x = new Intent(this, Login.class); x.putExtra("loginname", "abcxyz"); startActivity(x); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("http://txnam.net")); startActivity(i); mở trang web txnam.net mở activity Login với liệu gửi kèm loginname abcxyz mở activity quay số với số 0912102165 điền sẵn Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0912102165")); startActivity(dialIntent); TRƯƠNG XUÂN NAM Dùng Intent nhận liệu trả bật ứng dụng camera chụp ảnh trả ảnh chụp TRƯƠNG XUÂN NAM Phần 1.1 Intent filter TRƯƠNG XUÂN NAM Intent filter (bộ lọc intent)  Activity, Service Broadcast receiver sử dụng intent filter để thông báo cho hệ thống biết dạng intent mà xử lý  Phân giải intent (intent resolution): nhận intent, hệ thống tiến hành chọn activity phù hợp với intent theo ưu tiên sau  Action intent  Chuỗi tham số (URI phần data)  Category intent TRƯƠNG XUÂN NAM Intent filter (bộ lọc intent) Đoạn mã XML đăng kí với hệ thống activity “lắng nghe” việc người dùng muốn thực gọi cách định thuộc tính action intent-filter TRƯƠNG XUÂN NAM 10 Kinh nghiệm làm việc với sensor  Nhất thiết phải giải phóng sensor không cần thiết, không ứng dụng hao pin  Hệ thống không tự động tắt sensor kể tắt hình  Chú ý làm việc với thơng số 3D: chiều bị hốn đổi vị trí người sử dụng đặt thiết bị theo chiều âm (ví dụ: máy bị lật úp)  Nên kiểm thử thiết bị thật hiệu chỉnh độ nhạy  Kết hợp nhiều sensor để thiết bị “nhạy cảm” TRƯƠNG XUÂN NAM 26 Kinh nghiệm làm việc với sensor TRƯƠNG XUÂN NAM 27 Phần 2.1 Giới thiệu animation TRƯƠNG XUÂN NAM 28 Animations – nguyên tắc  Animation: tập hợp API cho phép biến đổi view thời gian ngắn (từ android 3.0)  Tạo animation theo nhiều cách:  Định nghĩa XML (folder “res/anim/”) nạp câu lệnh AnimationUtils.loadAnimation  Tạo code: tạo biến đối tượng phù hợp  Các animation ghép với thành để thực nhiều hiệu ứng (gọi AnimationSet)  (advanced) Tự tạo animation: kế thừa Animation viết lại applyTransformation 29 Animations – nguyên tắc  Các animation cung cấp Android  AlphaAnimation: Thay đổi độ suốt đối tượng • Fade In / Fade Out • Blink  RotateAnimation: Xoay đối tượng  ScaleAnimation: Thay đổi kích thước • Zoom In / Zoom Out • Slide Up / Slide Down • Bounce  TranslateAnimation: Dịch chuyển đối tượng  Bằng cách điều chỉnh tham số ta tạo animation khác 30 Phần 2.2 Tạo animation XML Code TRƯƠNG XUÂN NAM 31 Animations – XML vs Code  Ví dụ XML: res/animator/fadein.xml // nạp animation từ XML để sử dụng Animation ani = AnimationUtils.loadAnimation(this, R.animator.fadein);  Ví dụ tạo animation code: Animation ani = new AlphaAnimation(1, 0); ani.setInterpolator(new AccelerateInterpolator()); ani.setDuration(1000);  Sử dụng animation: myView.startAnimation(ani); 32 Animations – XML vs Code Một số thuộc tính quan trọng  android:duration: thời gian chạy hiệu ứng (ms)  android:startOffset: thời điểm bắt đầu chạy (ms)  android:fillAfter: có giữ lại trạng thái sau hiệu ứng không (true/false)  android:repeatCount: số lần lặp lại hiệu ứng  android:repeatMode: chế độ lặp (RESTART | REVERSE)  android:interpolator: kiểu diễn biến hiệu ứng • "@android:anim/accelerate_interpolator“ • “@android:anim/ accelerate_decelerate_interpolator” •… 33 Animations – XML vs Code Có thể xử lý kiện hiệu ứng bắt đầu, kết thúc lặp lại AnimationListener rotAni.setAnimationListener(new AnimationListener() { // kết thúc hiệu ứng public void onAnimationEnd(Animation arg0) { layerImage.setVisibility(View.INVISIBLE); layerButtons.setVisibility(View.VISIBLE); } // chuẩn bị lặp lại hiệu ứng public void onAnimationRepeat(Animation arg0) { } // bắt đầu hiệu ứng public void onAnimationStart(Animation arg0) { } }); 34 Animations – Interpolator  Interpolator: trình điều khiển trình thực hiệu ứng  Một số Interpolator thông dụng:      AccelerateDecelerateInterpolator: chậm-nhanh-chậm AccelerateInterpolator: nhanh dần DecelerateInterpolator: chậm dần CycleInterpolator: lặp lại animation theo chu kì LinearInterpolator: tuyến tính, khơng đổi  Có thể tự viết interpolator riêng muốn (phải tìm hiểu thêm Interpolator.Result) TRƯƠNG XUÂN NAM 35 Animations – set of animations 36 Animations – example public class MainActivity extends Activity { Animation x; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); x = AnimationUtils.loadAnimation(this, R.animator.abc); } public void btnAni(View v) { View t = findViewById(R.id.editText1); t.startAnimation(x); } } TRƯƠNG XUÂN NAM 37 Phần 2.3 Drawable animation TRƯƠNG XUÂN NAM 38 Drawable Animation  kiểu animations Android:  Property animation: gốc animation  View animation: animation view sử dụng image  Drawable animation: animation view sử dụng dãy image  Drawable Animation: hoạt hình dãy ảnh TRƯƠNG XUÂN NAM 39 Drawable Animation: ví dụ AnimationDrawable rocketAnimation; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); rocketImage.setBackgroundResource(R.drawable.rocket_thrust); rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_DOWN) return super.onTouchEvent(event); rocketAnimation.start(); return true; } TRƯƠNG XUÂN NAM 40 ... ProgressBar, Progress Dialog, AutoComplete TextView, TimePicker, TimePicker Dialog, DatePicker, DatePicker Dialog, ListView, Spinner, WebView  Quá trình xây dựng giao di? ??n: Thiết lập giao di? ??n XML Cái... loginname abcxyz mở activity quay số với số 09 121 021 65 điền sẵn Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:09 121 021 65")); startActivity(dialIntent); TRƯƠNG XUÂN NAM Dùng Intent... setResult để thiết lập liệu trả cho activity cha TRƯƠNG XUÂN NAM 25 Phần Broadcast Receivers TRƯƠNG XUÂN NAM 26 Broadcast Receiver  Broadcast receiver (gọi tắt receiver): bốn loại thành phần ứng dụng

Ngày đăng: 21/03/2021, 18:29

Mục lục

  • Lap trinh di dong - 08.pdf

  • Lap trinh di dong - 09.pdf

  • Lap trinh di dong - 10.pdf

  • Lap trinh di dong - 11.pdf

  • Lap trinh di dong - 12.pdf

  • Lap trinh di dong - 13.pdf

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

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

Tài liệu liên quan