本文介绍了如何顺利地移动图像 - 视图与用户的手指在Android模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一件事情是罚款,第一次当我移动的ImageView 在屏幕上,但在第二次的ImageView 没有按T移至正常。

这是我迄今所做的。

  img.setOnTouchListener(新View.OnTouchListener(){
    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        // TODO自动生成方法存根
        INT EID = event.getAction();
        开关(EID){
            案例MotionEvent.ACTION_MOVE:
                FrameLayout.LayoutParams mParams =(FrameLayout.LayoutParams)img.getLayoutParams();
                INT X =(int)的event.getRawX();
                INT Y =(INT)event.getRawY();
                mParams.leftMargin = X-50;
                mParams.topMargin = Y-50;
                img.setLayoutParams(mParams);
                打破;
            案例MotionEvent.ACTION_DOWN:
                X1 = img.getX();
                Y1 = img.getY();
                打破;
            案例MotionEvent.ACTION_UP:
                img.setX(×1);
                img.setY体(y1);
                打破;
            默认:
                打破;
            }
        返回true;
    }
});
 

解决方案

下面的话我觉得它可能工作。

我看到你在使用 img.getX() img.getY(),所以我假设你正在使用API​​级别11或以上。

和我假设你的 IMG 的ImageView 的实例。(对 FrameLayout.LayoutParams 的ImageView的用法是怪异,虽然...)

下面是如何使之正常:

  img.setOnTouchListener(新OnTouchListener()
{
    的PointF DownPT =新的PointF(); //记录鼠标的位置在pressed向下
    的PointF StartPT =新的PointF(); IMG的//记录开始位置

    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件)
    {
        INT EID = event.getAction();
        开关(EID)
        {
            案例MotionEvent.ACTION_MOVE:
                的PointF MV =新的PointF(event.getX() -  DownPT.x,event.getY() -  DownPT.y);
                img.setX((int)的(StartPT.x + mv.x));
                img.setY((int)的(StartPT.y + mv.y));
                StartPT =新的PointF(img.getX(),img.getY());
                打破;
            案例MotionEvent.ACTION_DOWN:
                DownPT.x = event.getX();
                DownPT.y = event.getY();
                StartPT =新的PointF(img.getX(),img.getY());
                打破;
            案例MotionEvent.ACTION_UP:
                //没有什么需要做的
                打破;
            默认 :
                打破;
        }
        返回true;
    }
});
 




================================================== ======================
========================== [2013年5月15号新增] ===== =============
================================================== ======================

下面psented的新对象$ P $是的PointF 。请使用以下code导入的PointF 目标:

 进口android.graphics.PointF;
 

而实际上,这只是一个对象记录浮动x和浮动年。如果你真的无法导入对象,写一个自己这样的:

 公共类的PointF
{
  公众持股量X = 0;
  公众持股量Y = 0;
  公共的PointF(){};
  公众的PointF(浮_x,浮_y){X = _x; Y = _y; }
}
 

Every thing is fine in first time when I move ImageView on the screen, but in second time ImageView doesn't move properly.

This is what I have done so far.

img.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        int eid = event.getAction();
        switch (eid) {
            case MotionEvent.ACTION_MOVE:
                FrameLayout.LayoutParams mParams = (FrameLayout.LayoutParams) img.getLayoutParams();
                int x = (int) event.getRawX();
                int y = (int) event.getRawY();
                mParams.leftMargin = x-50;
                mParams.topMargin = y-50;
                img.setLayoutParams(mParams);
                break;
            case MotionEvent.ACTION_DOWN:
                x1=img.getX();
                y1=img.getY();
                break;
            case MotionEvent.ACTION_UP:
                img.setX(x1);
                img.setY(y1);
                break;
            default:
                break;
            }
        return true;
    }
});
解决方案

The following is the case what I think it might work.

I saw you're using img.getX(), img.getY(),so I assume you're using API Level 11 or above.

And I assume your img is the instance of ImageView.( The usage of FrameLayout.LayoutParams for ImageView is wierd though... )

The following is how to make it properly:

img.setOnTouchListener(new OnTouchListener()
{
    PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
    PointF StartPT = new PointF(); // Record Start Position of 'img'

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        int eid = event.getAction();
        switch (eid)
        {
            case MotionEvent.ACTION_MOVE :
                PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                img.setX((int)(StartPT.x+mv.x));
                img.setY((int)(StartPT.y+mv.y));
                StartPT = new PointF( img.getX(), img.getY() );
                break;
            case MotionEvent.ACTION_DOWN :
                DownPT.x = event.getX();
                DownPT.y = event.getY();
                StartPT = new PointF( img.getX(), img.getY() );
                break;
            case MotionEvent.ACTION_UP :
                // Nothing have to do
                break;
            default :
                break;
        }
        return true;
    }
});




========================================================================
========================== [2013/05/15 Added ] =============================
========================================================================

The new object presented here is PointF.Please use the following code to import PointF object :

import android.graphics.PointF;

And actually, this is just an object for recording float x and float y.If you really can not import that object, write one yourself like the following :

public class PointF
{
  public float x = 0;
  public float y = 0;
  public PointF(){};
  public PointF( float _x, float _y ){ x = _x; y = _y; }
}

这篇关于如何顺利地移动图像 - 视图与用户的手指在Android模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:09