Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 實現控件的拖拽,實現控件拖拽

實現控件的拖拽,實現控件拖拽

編輯:關於android開發

實現控件的拖拽,實現控件拖拽


如圖是效果圖,移動ImageView時ImageView的位置會發生改變,並且雙擊的時候ImageView會水平居中

 

package com.org.demo.youku;

import com.org.wangfeng.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainDrag extends Activity {
    private TextView tv_top;
    private TextView tv_bottom;
    private ImageView iv_drag;
    // private ViewPager dPager;

    private int startX;
    private int startY;
    private SharedPreferences sPreferences;
    long[] mHits = new long[2];// 數組長度表示要點擊的次數

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maindrag);
        tv_top = (TextView) findViewById(R.id.tv_top);
        tv_bottom = (TextView) findViewById(R.id.tv_bottom);
        iv_drag = (ImageView) findViewById(R.id.iv_drag);

        // 計算手機屏的寬和高
        final int winWidth = getWindowManager().getDefaultDisplay().getWidth();
        final int winHeight = getWindowManager().getDefaultDisplay()
                .getHeight();

        // 用來記錄坐標 下次進入實現移動後的坐標
        sPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);
        int getLastX = sPreferences.getInt("lastX", 0);
        int getLastY = sPreferences.getInt("lastY", 0);
        // onMeasure(測量view), onLayout(安放位置), onDraw(繪制)
        // ivDrag.layout(getLastX, getLastY, getLastX + ivDrag.getWidth(),
        // getLastY + ivDrag.getHeight());//不能用這個方法,因為還沒有測量完成,就不能安放位置
        if (getLastY > winHeight / 2) {// 上邊顯示,下邊隱藏
            tv_top.setVisibility(View.VISIBLE);
            tv_bottom.setVisibility(View.INVISIBLE);
        } else {
            tv_top.setVisibility(View.INVISIBLE);
            tv_bottom.setVisibility(View.VISIBLE);
        }
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) iv_drag
                .getLayoutParams();
        layoutParams.leftMargin = getLastX;// 設置左邊距
        layoutParams.topMargin = getLastY;// 設置top邊距
        iv_drag.setLayoutParams(layoutParams); // 重新設置位置

        // 設置雙擊居中的事件
        iv_drag.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 開機後開始計算的時間
                if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
                    // 把圖片居中
                    System.out.println("雙擊了 要居中");
                    iv_drag.layout(winWidth / 2 - iv_drag.getWidth() / 2,
                            iv_drag.getTop(), winWidth / 2 + iv_drag.getWidth()
                                    / 2, iv_drag.getBottom());
                    
                }

            }
        });
        // 設置觸摸監聽事件
        iv_drag.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 初始化起點的坐標
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();

                    break;
                case MotionEvent.ACTION_MOVE:
                    // 移動後的坐標
                    int endX = (int) event.getRawX();
                    int endY = (int) event.getRawY();

                    // 計算移動的偏移量
                    int dx = endX - startX;
                    int dy = endY - startY;
                    // 更新後左上右下的距離
                    int l = iv_drag.getLeft() + dx;
                    int t = iv_drag.getTop() + dy;
                    int r = iv_drag.getRight() + dx;
                    int b = iv_drag.getBottom() + dy;

                    // 判斷是否超出屏幕邊界, 注意狀態欄的高度
                    if (l < 0 || r > winWidth || t < 0 || b > winHeight) {
                        System.out.println("不符要求");
                        break;
                    }

                    // 根據圖片位置,決定提示框顯示和隱藏
                    if (t > winHeight / 2) {// 上邊顯示,下邊隱藏
                        tv_top.setVisibility(View.VISIBLE);
                        tv_bottom.setVisibility(View.INVISIBLE);
                    } else {
                        tv_top.setVisibility(View.INVISIBLE);
                        tv_bottom.setVisibility(View.VISIBLE);
                    }
                    // 更新界面
                    iv_drag.layout(l, t, r, b);

                    // 重新初始化起點坐標
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();
                    System.out.println("ACTION_MOVE坐標x" + startX
                            + "---iv_drag.getLeft()---" + iv_drag.getLeft());
                    break;
                case MotionEvent.ACTION_UP:
                    // 記錄移動後的坐標
                    System.out.println("ACTION_UP" + "iv_drag.getLeft()---"
                            + iv_drag.getLeft() + "---iv_drag.getTop()---"
                            + iv_drag.getTop());
                    Editor edit = sPreferences.edit();
                    edit.putInt("lastX", iv_drag.getLeft());
                    edit.putInt("lastY", iv_drag.getTop());
                    edit.commit();
                    break;

                default:
                    break;
                }
                return false;// 事件要向下傳遞,讓onclick(雙擊事件)可以響應
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/png_3"
        android:gravity="center"
        android:padding="10dp"
        android:text="按住提示框拖到任意位置\n按手機返回鍵立即生效"
        android:textColor="#000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/png_3"
        android:gravity="center"
        android:padding="10dp"
        android:text="按住提示框拖到任意位置\n按手機返回鍵立即生效"
        android:textColor="#000"
        android:textSize="16sp"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/iv_drag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:contentDescription="@null"
        android:src="@drawable/drag" />

</RelativeLayout>

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved