8

我只想要一个简单的东西。我有一个imageview,我可以用触摸移动它

这是我的代码,如果错了,我很抱歉,因为我自己试过了

img.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
            status = StartDrag;
        } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
            status = StopDrag;
        }
        return false;
    }
});

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    if (status == StartDrag) {
        params.leftMargin = (int) event.getX();
        params.topMargin = (int) event.getY();
        img.setLayoutParams(params);
    }
    return super.onTouchEvent(event);
}

你能告诉我正确的方法吗?

4

6 回答 6

25

我已经这样做了:

private float xCoOrdinate, yCoOrdinate;

onCreate()

ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                xCoOrdinate = view.getX() - event.getRawX();
                yCoOrdinate = view.getY() - event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                view.animate().x(event.getRawX() + xCoOrdinate).y(event.getRawY() + yCoOrdinate).setDuration(0).start();
                break;
            default:
                return false;
            }
       return true;
    }
});

完毕

于 2016-02-01T11:15:33.913 回答
20

onTouch拖动视图的事件非常适合RelativeLayout和的子视图FrameLayout

这是一个例子:

@Override
public boolean onTouch(View v, MotionEvent event){
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN :
            {
                x = event.getX();
                                    y = event.getY();
                dx = x-myView.getX();
                dy = y-myView.getY();
            }
            break;
            case MotionEvent.ACTION_MOVE :
            {
                myView.setX(event.getX()-dx);
                myView.setY(event.getY()-dy);
            }
            break;
            case MotionEvent.ACTION_UP :
            {
                //your stuff
            }
        return true;
}

现在要做dxdy是,在ACTION_DOWN它上记录您在视图上触摸的位置,并与视图的左侧 (x) 和顶部 (y) 取得差异,以在ACTION_MOVE.

true如果您参加,则必须返回触摸事件。

更新:对于 API 8

在 API 8 的情况下,getX()andgetY()方法没有给出正确的结果,所以你可以使用的是getRawX()andgetRawY()方法。

例子 :

RelativeLayout.LayoutParams parms;
LinearLayout.LayoutParams par;
float dx=0,dy=0,x=0,y=0;

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN :
        {
            parms = (LayoutParams) myView.getLayoutParams();
            par = (LinearLayout.LayoutParams) getWindow().findViewById(Window.ID_ANDROID_CONTENT).getLayoutParams();
            dx = event.getRawX() - parms.leftMargin;
            dy = event.getRawY() - parms.topMargin;
        }
        break;
        case MotionEvent.ACTION_MOVE :
        {
            x = event.getRawX();
            y = event.getRawY();
            parms.leftMargin = (int) (x-dx);
            parms.topMargin = (int) (y - dy);
            myView.setLayoutParams(parms);
        }
        break;
        case MotionEvent.ACTION_UP :
        {

        }
        break;
    }
    return true;
}
于 2013-02-11T16:08:55.117 回答
1

在此处输入图像描述

我遇到了同样的问题并设法解决了这个问题。也就是说,在屏幕上拖动图像,除了放大和缩小并以编程方式在android中的framelayout上旋转图像。试试这个博客文章中的源代码

Android在imageview中旋转缩放拖动图像触摸示例...

于 2017-01-30T08:19:28.887 回答
0
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            StickerRelativeLayout = (RelativeLayout) findViewById(R.id.shsticker_relativeLayout1);
            RelativeLayout.LayoutParams layoutParams;
            layoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            final ImageView view = new ImageView(getApplicationContext());
            view.setPadding(10, 10, 10, 10);
            view.setImageResource(shstickerBaseAdaptor.shstickerImage[arg2]);
            view.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    windowwidth = getWindowManager().getDefaultDisplay()
                            .getWidth();
                    windowheight = getWindowManager().getDefaultDisplay()
                            .getHeight();

                    android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                            .getLayoutParams();
                    switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        Toast.makeText(ImageEditingClass.this,
                                "hii rma jee", Toast.LENGTH_SHORT).show();
                        break;
                    case MotionEvent.ACTION_MOVE:

                        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) view
                                .getLayoutParams();
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();
                        if (x_cord > windowwidth) {
                            x_cord = windowwidth;
                        }
                        if (y_cord > windowheight) {
                            y_cord = windowheight;
                        }

                        layoutParams.leftMargin = x_cord - 15;

                        layoutParams.topMargin = y_cord -100;

                        layoutParams.rightMargin = x_cord - 50;
                        layoutParams.bottomMargin = x_cord - 50;
                        view.setLayoutParams(layoutParams);
                        break;
                    default:
                        break;
                    }
                    return true;
                }
            });
            // setText(++ViewGenerate + " hii this new");

            StickerRelativeLayout.addView(view, layoutParams);
            // }
            Toast.makeText(ImageEditingClass.this, "you click" + arg2,
                    Toast.LENGTH_SHORT).show();

        }
于 2013-07-11T08:59:41.193 回答
0

如果您想向任何方向移动图像,您需要确定方向,您应该按照以下步骤步骤 -1 float dx = 0, dy = 0, x = 0, y = 0, z = 0, w = 0 , dz = 0, dw = 0; {这你们四个坐标}

public void StickerPopulate(){
    LinerLayoutInfate.removeAllViews();

    shstickerBaseAdaptor = new ShstickerBaseAdaptor(getApplicationContext());
    View shstickerView = getLayoutInflater().inflate(R.layout.shareimage,
            null);
    Gallery shsticker_gallery = (Gallery) shstickerView
            .findViewById(R.id.shsticker_gallery);
    shsticker_gallery.setAdapter(new ShstickerBaseAdaptor(
            ImageEditingClass.this));

    shsticker_gallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            StickerRelativeLayout = (RelativeLayout) findViewById(R.id.shsticker_relativeLayout1);
            RelativeLayout.LayoutParams layoutParams;
            layoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            final ImageView crossBtn = new ImageView(
                    getApplicationContext());
            crossBtn.setBackgroundResource(R.drawable.cross);

            crossBtn.setMaxWidth(300);
            crossBtn.setMaxHeight(200);
            crossBtn.setPadding(10, 10, 10, 10);
            final ImageView view = new ImageView(getApplicationContext());

            view.setImageResource(shstickerBaseAdaptor.shstickerImage[arg2]);

            view.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    windowwidth = getWindowManager().getDefaultDisplay()
                            .getWidth();

                    windowheight = getWindowManager().getDefaultDisplay()
                            .getHeight();

                    android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                            .getLayoutParams();
                    switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        dx = event.getRawX() - layoutParams.leftMargin;
                        dy = event.getRawY() - layoutParams.topMargin;
                        dz = event.getRawX() - layoutParams.bottomMargin;
                        dw = event.getRawX() - layoutParams.rightMargin;

                        break;
                    case MotionEvent.ACTION_MOVE:

                        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) view
                                .getLayoutParams();

                        x = event.getRawX();
                        y = event.getRawY();
                        layoutParams.leftMargin = (int) (x - dx);

                        layoutParams.topMargin = (int) (y - dy);
                        layoutParams.bottomMargin = (int) (z - dz);
                        layoutParams.rightMargin = (int) (w - dw);

                        view.setLayoutParams(layoutParams);

                        break;

                    default:
                        break;
                    }
                    return true;
                }
            });
            StickerRelativeLayout.addView(crossBtn, layoutParams);

            StickerRelativeLayout.addView(view, layoutParams);
            // }
            Toast.makeText(ImageEditingClass.this, "you click" + arg2,
                    Toast.LENGTH_SHORT).show();

        }

    });
    LinerLayoutInfate.addView(shstickerView);
}
于 2013-07-18T08:48:54.110 回答
0

以下代码允许拖动和移动视图。

import android.view.*;
import android.view.View.*;

public class DragMove<T extends View> implements OnTouchListener
{
    private final  T view;
    private int leftOffset;
    private int topOffset;

    public DragMove(T dragable)
    {
        this.view = dragable;
        this.view.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View p1, MotionEvent event)
    {
        /*
        public static int pxFromDp(double dp, View v)
        {
            Activity context = (Activity)v.getContext();
            return pxFromDp(dp, context);
        }

        public static int pxFromDp(double dp, Activity context)
        {
            float logicalDensity = getDensity(context);
            int px = (int) Math.round(dp * logicalDensity);
            return px;
        }
        */

        int x = GuiUtils.pxFromDp(event.getRawX(), view);
        int y = GuiUtils.pxFromDp(event.getRawY(), view);

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN :
                {
                    // calculates offset to current coordinate,
                    // keeps position relative (from jumping) during move
                    ViewGroup.LayoutParams parms = view.getLayoutParams();
                    // e.g. = x - params.leftMargin
                    leftOffset = x - Layout.readLeftMargin(parms);
                    topOffset = y - Layout.readTopMargin(parms);
                }
                return true;
            case MotionEvent.ACTION_MOVE :
                {
                    // adjust by touch position retaining touch
                    // offset at start
                    ViewGroup.LayoutParams parms = view.getLayoutParams();
                    // e.g params.leftMargin = x - leftOffset
                    Layout.writeLeftMargin(parms, x - leftOffset);
                    Layout.writeTopMargin(parms, y - topOffset);
                    view.setLayoutParams(parms);
                }
                return true;
        }
        return false;
    }
}
于 2017-07-12T22:23:56.287 回答