Android在superview上拖拽许多视图

时间:2011-11-22 17:34:05

标签: java android

我开始认为缺少OpenGL我无法拖拽简单的视图。说真的,我已经阅读了10-15个用于拖动视图的教程,每个教程都会向您展示如何在视图中拖动绘图,或者拖动图像视图的INSIDE(通过矩阵变换)。但是没有教程告诉你如何在superview中拖动一个简单的视图。真的那么简单。我有一个superview,我添加了10个子视图。无论我的手指触摸的是什么子视图。我为iOS http://www.facebook.com/MakeASnowman创建了一个应用程序(任何一天点击应用程序商店),我正在尝试为Android构建它。但是在iOS中做这么简单的事情没有简单的方法(两个手指旋转,拖动视图等)。这就是我想要的:我有一个自定义视图,可以将图像绘制到画布上。然后,我将一个或多个自定义视图实例化为“superview”。我可以拖动/操纵这些视图(意思是我不会在这些视图中翻译drawable,而是实际视图本身)。

这是我的代码具有正确的操作,除了它在视图中执行drawable:

package com.spentaklabs.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;


public class MAView extends View
{
Drawable image;
private String imgName;
private float mPosX;
private float mPosY;

private float mLastTouchX;
private float mLastTouchY;

private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;

private ScaleGestureDetector mScaleDetector;

private float mScaleFactor = 1.f;

public MAView(Context context)
{
    super(context);
}

public MAView(Context context,String imgName)
{
    this(context);
    this.imgName = imgName;
    image = context.getResources().getDrawable(R.drawable.hatshairears0);
    image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

public MAView(Context context, AttributeSet attrs, int defStyle)
{
    super(context,attrs,defStyle);
}

@Override
public void onDraw(Canvas canvas)
{
    canvas.save();
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);
    image.draw(canvas);
    canvas.restore();
}

@Override
public boolean onTouchEvent(MotionEvent ev)
{
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();

    switch (action)
    {
        case MotionEvent.ACTION_DOWN:
        {
            final float x = ev.getX();
            final float y = ev.getY();

            mLastTouchX = x;
            mLastTouchY = y;

            mActivePointerId = ev.getPointerId(0);
            break;
        }

        case MotionEvent.ACTION_MOVE:
        {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

         // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;

        }

        case MotionEvent.ACTION_UP: 
        {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: 
        {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP:
        {
            // Extract the index of the pointer that left the touch sensor
            final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);

            if (pointerId == mActivePointerId)
            {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
    }
    return true;
}


private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

}

1 个答案:

答案 0 :(得分:3)

以下是一些代码,可让您在屏幕上任意位置拖动视图...

package com.matthieu;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout.LayoutParams;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.Arrays;

public class DragActivity extends Activity implements View.OnTouchListener, AdapterView.OnItemLongClickListener
{
    private static final String TAG="DragActivity";

    private static final int NOT_DRAGGING = 0;
    private static final int DRAGGING = 1;

    private int state=NOT_DRAGGING;
    private ImageView draggable =null;
    private int dragged_position;

    float current_x, current_y;
    int current_icon = R.drawable.notepad;

    private ArrayList<String> names = new ArrayList<String>(Arrays.asList("Matt", "Xiaohui", "Yong", "Hunt", "Andy", "Ivy", "Guanglong", "Zeyan", "Yanxia",
            "Chris", "Mark", "Matthieu"));
    private ArrayList<Integer> icons = new ArrayList<Integer>(Arrays.asList( R.drawable.glasses, R.drawable.monkey, R.drawable.normal, R.drawable.smile, R.drawable.wink));
    private ArrayList<Integer> matching;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupListContent();

        ListView list = (ListView) findViewById(R.id.main_list);
        list.setAdapter(new DragListAdapter());
        list.setOnItemLongClickListener(this);

        list.setOnTouchListener(this);

        ImageView image = (ImageView) findViewById(R.id.main_image);
        image.setImageResource(current_icon);
    }

    private void setupListContent() {
        matching = new ArrayList<Integer>();
        for (int i=0; i<names.size(); i++) {
            matching.add((int) (icons.size() * Math.random()));
        }
    }

    @SuppressWarnings("unchecked")
    private class DragListAdapter extends ArrayAdapter {
        public DragListAdapter() {
            super(DragActivity.this, R.layout.list_item, names);

        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent, false);
            }

            row.setDrawingCacheEnabled(true);
            TextView name = (TextView) row.findViewById(R.id.item_text);
            ImageView icon = (ImageView) row.findViewById(R.id.item_icon);

            name.setText(names.get(position));
            icon.setImageResource(icons.get(matching.get(position)));

            return row;
        }
    }

    private boolean checkOnDropIcon(MotionEvent me) {
        ImageView drop_icon = (ImageView) findViewById(R.id.main_image);
        Rect icon_rect = new Rect();
        drop_icon.getGlobalVisibleRect(icon_rect);
        Log.d(TAG, "icon at " + icon_rect.left + "<- ->" + icon_rect.right + ", " +
                icon_rect.top + " ^ v" + icon_rect.bottom);
        if ((me.getRawX()<icon_rect.left) || (me.getRawX()>icon_rect.right) ||
                (me.getRawY()<icon_rect.top) || (me.getRawY()>icon_rect.bottom)) {
            return false;
        }
        else {
            return true;
        }
    }

    private void checkOnDrop(MotionEvent me) {
        boolean onDropIcon = checkOnDropIcon(me);
        ImageView image = (ImageView) findViewById(R.id.main_image);
        if ((onDropIcon) && (current_icon==R.drawable.notepad)) {
            current_icon = R.drawable.exit;
            image.setImageResource(current_icon);
            image.invalidate();
            return;
        }
        if ((!onDropIcon) && (current_icon==R.drawable.exit)) {
            current_icon = R.drawable.notepad;
            image.setImageResource(current_icon);
            image.invalidate();
            return;
        }
    }

    public boolean onTouch(View view, MotionEvent me) {
        if (state == NOT_DRAGGING) {
            // get the position of the touch so we know where to place the dragging item if it is a long press
            current_x = me.getRawX();
            current_y = me.getRawY();
            return false;
        }
        else {
            FrameLayout frame = (FrameLayout) findViewById(R.id.drag_space);

            if (me.getAction()==MotionEvent.ACTION_UP) {
                frame.removeAllViews();
                draggable=null;
                frame.setVisibility(View.GONE);
                state=NOT_DRAGGING;

                // check if we dropped a name
                if (checkOnDropIcon(me)) {
                    names.remove(dragged_position);
                    matching.remove(dragged_position);

                    ListView list = (ListView) findViewById(R.id.main_list);
                    DragListAdapter adapter = (DragListAdapter) list.getAdapter();
                    adapter.notifyDataSetChanged();
                }

                // restore the icon
                ImageView image = (ImageView) findViewById(R.id.main_image);

                current_icon = R.drawable.notepad;
                image.setImageResource(current_icon);
                image.invalidate();
            }
            if (me.getAction()==MotionEvent.ACTION_MOVE) {
                int frame_position[] = new int[2];
                frame.getLocationOnScreen(frame_position);

                draggable.setPadding(
                        (int) me.getRawX()-frame_position[0]-(draggable.getDrawable().getIntrinsicWidth()/2),
                        (int) me.getRawY()-frame_position[1]-(draggable.getDrawable().getIntrinsicHeight()/2),
                        0, 0);
                draggable.invalidate();

                checkOnDrop(me);
            }

            return true;
        }
    }

    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (state == DRAGGING) {
            Log.d(TAG, "already have an object moving... ?");
            return false;
        }

        FrameLayout frame = (FrameLayout) findViewById(R.id.drag_space);
        int frame_position[] = new int[2];
        frame.getLocationOnScreen(frame_position);

        // setup everything for dragging
        state = DRAGGING;
        dragged_position = i;

        draggable = new ImageView(this);
        Bitmap bm = view.getDrawingCache();
        draggable.setImageBitmap(bm);
        draggable.setAlpha(150);
        draggable.setScaleType(ImageView.ScaleType.CENTER);
        draggable.setDrawingCacheEnabled(true);
        draggable.setPadding((int) current_x-frame_position[0]-(bm.getWidth()/2), (int) current_y-frame_position[1]-(bm.getHeight()/2), 0, 0);

        frame.setVisibility(View.VISIBLE);
        frame.addView(draggable, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        return true;
    }
}

这是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/main_frame"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        <ListView
                android:id="@+id/main_list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                />
        <ImageView
                android:id="@+id/main_image"
                android:layout_width="fill_parent"
                android:layout_height="32sp"
                />
    </LinearLayout>
    <FrameLayout
            android:id="@+id/drag_space"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone"/>
</FrameLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">
    <ImageView
            android:id="@+id/item_icon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_margin="3sp"/>
    <TextView
            android:id="@+id/item_text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"/>
</LinearLayout>

希望有所帮助。