为什么我在屏幕上拖动的物体如此剧烈?

时间:2018-07-02 20:23:30

标签: java android ontouchlistener

这看起来像。它以20fps录制。实际上,它动摇了很多。 (另一个问题是在右边和底部有一个看不见的边界。)我为每个指针分配了一个永久ID,因此当在屏幕上放置第二个指针时,该对象不会跳来跳去。

onTouch()的实现是从Android dev webpage复制而来的。嵌套的类结构取自此Youtube Video

.gif of the object shaking when I am dragging it.

这是我的代码:

java

public class MainActivity extends AppCompatActivity{
private ImageView imageView;
private TextView textView;
private ViewGroup rootLayout;

private float mLastTouchX,mLastTouchY;
private final int INVALID_POINTER_ID=-1;
private float mPosX,mPosY;
int i=0;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView=(TextView) findViewById(R.id.text);

    rootLayout=(ViewGroup)findViewById(R.id.rootView);

    imageView=(ImageView) findViewById(R.id.image);
    //imageView.setOnClickListener(this);//this thing doesn't work now
    imageView.setOnTouchListener(new ChoiceTouchListener());
}

private final class ChoiceTouchListener implements View.OnTouchListener {
    private int mActivePointerId=INVALID_POINTER_ID;
    public boolean onTouch(View view, MotionEvent event){
        final int action= MotionEventCompat.getActionMasked(event);
        switch(action){
            case MotionEvent.ACTION_DOWN: {//first finger down on screen
                final int pointerIndex = MotionEventCompat.getActionIndex(event);
                final float x=MotionEventCompat.getX(event,pointerIndex);
                final float y=MotionEventCompat.getY(event,pointerIndex);

                mLastTouchX=x;
                mLastTouchY=y;

                mActivePointerId=MotionEventCompat.getPointerId(event,0);
                break;
            }
            case MotionEvent.ACTION_MOVE:{
                final int pointerIndex=MotionEventCompat.findPointerIndex(event,mActivePointerId);
                final float x=MotionEventCompat.getX(event,pointerIndex);
                final float y=MotionEventCompat.getY(event,pointerIndex);

                final float dx=x-mLastTouchX;
                final float dy=y-mLastTouchY;

                mPosX+=dx;
                mPosY+=dy;
                imageView.setLeft(Math.round(mPosX));
                imageView.setTop(Math.round(mPosY));

                rootLayout.invalidate();

                mLastTouchX=x;
                mLastTouchY=y;
                break;
            }
            case MotionEvent.ACTION_UP:{//last pointer leaves the screen
                mActivePointerId=INVALID_POINTER_ID;
                break;
            }
            case MotionEvent.ACTION_CANCEL:{
                mActivePointerId=INVALID_POINTER_ID;
                break;
            }
            case MotionEvent.ACTION_POINTER_UP:{//when one finger leaves the screen
                final int pointerIndex=MotionEventCompat.getActionIndex(event);
                final int pointerId=MotionEventCompat.getPointerId(event,pointerIndex);
                if(pointerId==mActivePointerId){
                    final int newPointerIndex=pointerIndex==0?1:0;
                    mLastTouchX=MotionEventCompat.getX(event,newPointerIndex);
                    mLastTouchY=MotionEventCompat.getY(event,newPointerIndex);
                    mActivePointerId=MotionEventCompat.getPointerId(event, newPointerIndex);
                }
                break;
            }
        }//switch
        return true;
    }//onTouch() ends
}//ChoiceTouchListener class
}

编辑:--------------------------------------------- ----

我按照Gabe Sechan的建议做了。现在我什至不能移动广场。这是我的代码:

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

final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;

if((dx*dx+dy*dy)==25){
    mPosX += dx;
    mPosY += dy;
    imageView.setLeft(Math.round(mPosX));
    imageView.setTop(Math.round(mPosY));
}
rootLayout.invalidate();

mLastTouchX = x;
mLastTouchY = y;
break;
}

我还创建了一个TextView来显示dx和dy。但是,一旦我在TextView.setText()行中插入了方块,便无法移动:

.gif of the TextView

这是我的代码:

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

final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
textView.setText("dx="+dx+" dy="+dy);

mPosX += dx;
mPosY += dy;
imageView.setLeft(Math.round(mPosX));
imageView.setTop(Math.round(mPosY));

rootLayout.invalidate();

mLastTouchX = x;
mLastTouchY = y;
break;
}

0 个答案:

没有答案