0

我试图在单击时移动图像按钮的位置。我尝试了以下操作,但它在“rlOut.addView(imgP, p);”处崩溃 在 onCreate() 中运行它们时我没有问题。

公共类 Delete3Activity 扩展 Activity {

RelativeLayout rlOut;
ImageButton imbBtn;

EditText et1, et2;
ImageView ivB;
ImageButton imgP;
RelativeLayout.LayoutParams p;
RelativeLayout.LayoutParams params;

int mX, mY;

@覆盖

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);

imbBtn = (ImageButton)findViewById(R.id.imgBtn);
rlOut = (RelativeLayout)findViewById(R.id.rlOut);
imgP = (ImageButton)findViewById(R.id.imgP);

ivB = new ImageView(this);
ivB.setBackgroundColor(Color.RED);

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

width = width/2-62;
height = height/2-62;
p = new RelativeLayout.LayoutParams(62, 62);    

imbBtn.setOnTouchListener(new View.OnTouchListener() {



    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        LayoutParams par = (LayoutParams)imbBtn.getLayoutParams();

        if(event.getAction()==MotionEvent.ACTION_DOWN)
        {
            Log.d("ok","down");
            mX = (int) event.getX() - imbBtn.getWidth() / 2;
            mY = (int) event.getY() - imbBtn.getHeight() / 2;
            et1.setText("mX down: "+String.valueOf(mX));
            et2.setText("mY down: "+String.valueOf(mY));


        }
        else if (event.getAction()==MotionEvent.ACTION_UP)
        {
            Log.d("ok","up");
            rlOut.addView(ivB, params);
            p.leftMargin=4;
            params.leftMargin = 60;
            params.topMargin = 20;
            rlOut.addView(imgP, p);
            Log.d("ok","p");
        }
        else if (event.getAction()==MotionEvent.ACTION_MOVE)
        {
            Log.d("ok","move");
            mX = (int) event.getX() - imbBtn.getWidth() / 2;
            mY = (int) event.getY() - imbBtn.getHeight() / 2;
            et1.setText("mX move: "+String.valueOf(mX));
            et2.setText("mY move: "+String.valueOf(mY));
        }
        return false;
    }
});

} }

4

1 回答 1

0

OnCreate方法是你设置的地方setContentView(R.layout.main)。我想变量rlOut代表RelativeLayout可能在您的主布局中。设置 contentView 并使用初始化变量后,在 OnCreate 方法中调用上述函数。或者如果变量在其他布局文件中,那么您必须首先使用布局充气器充气该布局,然后使用 方法找到视图。rlOutfindViewByIdrlOutfindViewById

希望这可以帮助。

编辑

以下代码应该是您正在寻找的解决方案:

imbBtn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            LayoutParams par = (LayoutParams)imbBtn.getLayoutParams();

            if (event.getAction()==MotionEvent.ACTION_MOVE)
            {
                Log.d("ok","move");
                mX = (int) event.getX() - imbBtn.getWidth() / 2;
                mY = (int) event.getY() - imbBtn.getHeight() / 2;
                par.leftMargin = imbBtn.getLeft() + mX;
                par.topMargin = imbBtn.getTop() + mY;
                imbBtn.setLayoutParams(par);
                et1.setText("mX move: "+String.valueOf(mX));
                et2.setText("mY move: "+String.valueOf(mY));
            }
            return false;
        }
    });

MotionEvent.ACTION_MOVE是您需要捕获的唯一事件。希望此解决方案对您有所帮助。

于 2012-06-11T04:35:51.203 回答