android拖動圖片移動效果

來源:互聯網
上載者:User

java代碼
Java代碼 
package com.twy.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 
 
public class TestDemo extends Activity { 
    private ImageView img1; 
    private ImageView img2; 
    private int screenWidth; 
    private int screenHeight; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        img1 = (ImageView) findViewById(R.id.imageView1); 
        img2 = (ImageView) findViewById(R.id.imageView2); 
 
        DisplayMetrics dm = getResources().getDisplayMetrics(); 
        screenWidth = dm.widthPixels; 
        screenHeight = dm.heightPixels - 50; 
 
        img1.setOnTouchListener(movingEventListener); 
        img2.setOnTouchListener(movingEventListener); 
 
    } 
 
    private OnTouchListener movingEventListener = new OnTouchListener() { 
        int lastX, lastY; 
 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
            switch (event.getAction()) { 
            case MotionEvent.ACTION_DOWN: 
                lastX = (int) event.getRawX(); 
                lastY = (int) event.getRawY(); 
                break; 
            case MotionEvent.ACTION_MOVE: 
                int dx = (int) event.getRawX() - lastX; 
                int dy = (int) event.getRawY() - lastY; 
 
                int left = v.getLeft() + dx; 
                int top = v.getTop() + dy; 
                int right = v.getRight() + dx; 
                int bottom = v.getBottom() + dy; 
                // 設定不能出界 
                if (left < 0) { 
                    left = 0; 
                    right = left + v.getWidth(); 
                } 
 
                if (right > screenWidth) { 
                    right = screenWidth; 
                    left = right - v.getWidth(); 
                } 
 
                if (top < 0) { 
                    top = 0; 
                    bottom = top + v.getHeight(); 
                } 
 
                if (bottom > screenHeight) { 
                    bottom = screenHeight; 
                    top = bottom - v.getHeight(); 
                } 
 
                v.layout(left, top, right, bottom); 
 
                lastX = (int) event.getRawX(); 
                lastY = (int) event.getRawY(); 
 
                break; 
            case MotionEvent.ACTION_UP: 
                break; 
            } 
            return true; 
        } 
    }; 
} 
 
 xml 代碼
Java代碼 
<?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"> 
    <RelativeLayout android:id="@+id/relativeLayout1" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"> 
        <ImageView android:id="@+id/imageView1" 
            android:layout_width="wrap_content" android:background="@drawable/icon" 
            android:layout_height="wrap_content" android:layout_alignParentLeft="true"></ImageView> 
        <ImageView android:id="@+id/imageView2" 
            android:layout_width="wrap_content" android:background="@drawable/icon" 
            android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1" 
            android:layout_alignTop="@+id/imageView1" android:layout_alignBottom="@+id/imageView1"></ImageView> 
    </RelativeLayout> 
 
</LinearLayout> 
 
 
其中 在onTouch 代碼中 如果返回 false 就不能捕捉到ACTION_MOVE 事件。
 對於onTouchEvent 中onTouch傳回值
1 、如果return false 說明還沒有消費onTouch事件,在執行onTouch裡代碼後,onTouch事件並沒有結束。
2、如果return true 說明消費了onTouch事件 onTouch事件結束了
但在實際操作中 除了ACTION_DOWN事件以外,其餘的事件只有返回true的那個方法才能捕捉到。所以 返回false的時候只能捕捉到每次的第一個DOWN事件 後面的MOVE 和UP事件就捕捉不到了。
 
DisplayMetics 類:
Andorid.util 包下的DisplayMetrics 類提供了一種關於顯示的通用資訊,如顯示大小,解析度和字型。
為了擷取DisplayMetrics 成員,首先初始化一個對象如下:
Java代碼 
DisplayMetrics metrics = new DisplayMetrics();  
getWindowManager().getDefaultDisplay().getMetrics;  
 
//getWindowManager() 擷取顯示定製視窗的管理器 
//getDefaultDisplay() 擷取預設顯示Display對象 
//getMetrics(dm) 通過Display對象的資料來初始化一個DisplayMetrics對象 
 v.layout(left, top, right, bottom);
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass(). Derived classes with children should override onLayout. In that method, they should call layout on each of their their children.
Parameters:
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

作者“twy”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.