在滚动视图中回收视图为android

时间:2015-01-12 16:22:19

标签: android android-activity touch drag

我在这里写了这段代码,我写过这应该允许你在屏幕上移动对象。

我似乎遇到了麻烦,因为物体会移动但不会变得苍白,当你按住手指时它会跳起来。

请帮忙。

VVVVVVVVVVVVV 码 VVVVVVVVVVVVV

package com.example.alex.alexstouch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    TextView touchMove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        touchMove = (TextView)findViewById(R.id.pike);
        touchMove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_MOVE:
                        touchMove.setTranslationX(event.getX());
                        touchMove.setTranslationY(event.getY());
                        break;

                    case MotionEvent.ACTION_DOWN:
                        Log.d("pike pike", "broken");
                        break;



                }

                return true;
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

如果你想在屏幕上移动某个对象,例如,拖放按钮,你应该进行根布局 - 相对布局和更改代码中的对象边距(当调用触摸回调时)。
您可以像这样更改使用layoutParams的边距

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = layoutParams.rightMargin + x
layoutParams.bottomMargin = layoutParams.bottomMargin + y;
view.setLayoutParams(layoutParams);

我的代码浮动按钮(可以通过长按移动)

private class PlusButtonTouchListener implements View.OnTouchListener {
    private final int MIN_X = 0;
    private final int MIN_Y = 0;
    private float startX;
    private float startY;
    private int MAX_X;
    private int MAX_Y;

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();

                MAX_X = ((View) mPlusButton.getParent()).getWidth() - mPlusButton.getWidth();
                MAX_Y = ((View) mPlusButton.getParent()).getHeight() - mPlusButton.getHeight();

                mPlusButton.setAlpha(0.7f);
                break;
            case MotionEvent.ACTION_UP:
                mPlusButton.setAlpha(1f);
                savePlusButtonCoordinates();
                isMoved = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isMoved) {
                    float deltaX = startX - event.getX();
                    float deltaY = startY - event.getY();
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    layoutParams.rightMargin = layoutParams.rightMargin + (int) deltaX;
                    layoutParams.bottomMargin = layoutParams.bottomMargin + (int) deltaY;

                    if (layoutParams.rightMargin > MAX_X) {
                        layoutParams.rightMargin = MAX_X;
                    } else if (layoutParams.rightMargin < MIN_X) {
                        layoutParams.rightMargin = MIN_X;
                    }
                    if (layoutParams.bottomMargin > MAX_Y) {
                        layoutParams.bottomMargin = MAX_Y;
                    } else if (layoutParams.bottomMargin < MIN_Y) {
                        layoutParams.bottomMargin = MIN_Y;
                    }

                    view.setLayoutParams(layoutParams);
                }
                break;
        }
        mPlusButton.invalidate();
        return false;
    }
}