Lập trình Android: Di chuyển Image bằng chuột doc

5 566 2
Lập trình Android: Di chuyển Image bằng chuột doc

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

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Kéo thả hình ảnh Chắc các bạn đã từng sữ dụng wa ứng dụng kéo thả các Icon để sắp xếp màn hình cho smartphone. Đây là tính năng khá thú vị mà tất cả các smartphone đều hổ trợ. Sau đây mình sẽ demo cách thức hoạt động của nó. Mình sẽ tạo ra 1 ImageView và kéo thả trên màn hình Virtual 1/ Đầu tiên các bạn tạo 1 project như sau: Project name: DragNew Build Target: Android 2.3.1 Application name: DragNew Package name: com.sample.dragdrop Create Activity: MyActivity 2/ Các bạn chỉnh lại phần main.xml: <?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/vg" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/elmer" /> </LinearLayout> 3/ Code trong phần strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MyActivity!</string> <string name="app_name">DragNew</string> <string name="drag">drag</string> </resources> 4/ Code lại phần DragNew.Java như sau: package com.sample.dragdrop; Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; public class MyActivity extends Activity { private View selected_item = null; private int offset_x = 0; private int offset_y = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewGroup vg = (ViewGroup)findViewById(R.id.vg); vg.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getActionMasked()) { Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN case MotionEvent.ACTION_MOVE: int x = (int)event.getX() - offset_x; int y = (int)event.getY() - offset_y; int w = getWindowManager().getDefaultDisplay().getWidth() - 100; int h = getWindowManager().getDefaultDisplay().getHeight() - 100; if(x > w) x = w; if(y > h) y = h; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( new ViewGroup.MarginLayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); lp.setMargins(x, y, 0, 0); selected_item.setLayoutParams(lp); break; default: break; } return true; } Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN }); ImageView img = (ImageView)findViewById(R.id.img); img.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: offset_x = (int)event.getX(); offset_y = (int)event.getY(); selected_item = v; break; default: break; } return false; } }); } } Cuối cùng Debug ứng dụng và ta sẽ được: Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN Các bạn có thể kéo thả hình thoải mái và khi đến các góc của Virtual thì hình sẽ tự động thu nhỏ lại . 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 5 . android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/vg" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" . break; default: break; } return true; } Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN }); ImageView img = (ImageView)findViewById(R.id.img); img.setOnTouchListener(new. 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 5

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

Từ khóa liên quan

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

Tài liệu liên quan