Android 根据按钮单击移动图像

Android 根据按钮单击移动图像,android,Android,我用了四个按钮(上、下、左、右),还有图像。我必须相应地移动图像,当我按下“向上”按钮时,我应该向上移动图像,当我按下“左”按钮时,同样保持两个方向,它应该向左移动图像。我使用了onclick listener,然后我尝试使用X,Y坐标移动图像。我不知道如何获取X,Y坐标 这是代码。 public class MainActivity extends Activity implements OnClickListener { Button up,left,right,down;

我用了四个按钮(上、下、左、右),还有图像。我必须相应地移动图像,当我按下“向上”按钮时,我应该向上移动图像,当我按下“左”按钮时,同样保持两个方向,它应该向左移动图像。我使用了onclick listener,然后我尝试使用X,Y坐标移动图像。我不知道如何获取X,Y坐标

这是代码。

public class MainActivity extends Activity implements OnClickListener {

    Button up,left,right,down; 

         ImageView i1;

    protected void onCreate(Bundle savedInstanceState) {

                 super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);

            up=(Button)findViewById(R.id.button1);

            left=(Button)findViewById(R.id.button2);

            right=(Button)findViewById(R.id.button3);

            down=(Button)findViewById(R.id.button4);

            i1=(ImageView)findViewById(R.id.imageView1);

            up.setOnClickListener(this);
     }

        public void onClick(View arg0) {

    Toast.makeText(getApplication(),"UP",5000).show();  

        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) 

        i1.getLayoutParams();

        int x = (int)getRawx();

        int y = (int)getRawY();

        mParams.leftMargin = x-50;

        mParams.topMargin = y-50;

       i1.setLayoutParams(mParams);


    }

}
嗨, 我已经更新了下面的代码,请检查

包com.example.motion

public class MainActivity extends Activity implements OnClickListener {

Button up,left,right,down; 
ImageView i1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    up=(Button)findViewById(R.id.button1);
    left=(Button)findViewById(R.id.button2);
    right=(Button)findViewById(R.id.button3);
    down=(Button)findViewById(R.id.button4);
    i1=(ImageView)findViewById(R.id.imageView1);
    up.setOnClickListener(this);
    down.setOnClickListener(this);
    left.setOnClickListener(this);
    right.setOnClickListener(this);
}



public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1:
        {
        Toast.makeText(getApplication(),"UP",Toast.LENGTH_SHORT).show();    
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) 
        i1.getLayoutParams();
        mParams.topMargin -= 20;
        i1.setLayoutParams(mParams);
        break;
        }


    case R.id.button4:
           {
        Toast.makeText(getApplication(),"DOWN",Toast.LENGTH_SHORT).show();  


        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) 
                i1.getLayoutParams();
                mParams.topMargin += 20;
                i1.setLayoutParams(mParams);
                break; 
           }


    case R.id.button2:
               {
        Toast.makeText(getApplication(),"LEFT",Toast.LENGTH_SHORT).show();              
        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) 
                i1.getLayoutParams();
                mParams.leftMargin -= 20;
                i1.setLayoutParams(mParams);    
                break;  
               }

    case R.id.button3:
               {
        Toast.makeText(getApplication(),"RIGHT",Toast.LENGTH_SHORT).show(); 

        RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) 
            i1.getLayoutParams();
            mParams.leftMargin += 20;
            i1.setLayoutParams(mParams);
            break;
               }


}
    }
 }

我认为问题在于
(int)getRawx()
(int)getRawy(),您正在对活动调用此方法,我不确定您是否真的需要这些值

你可以试试:

mParams.leftMargin += 50;


任何人请回答我哪里出了问题?这是否可能使用onclick Listener移动图像?为什么按向上按钮时要更改x?不应该只改变y吗?好的,但我在int x=(int)getRawX()处得到错误;我应该用哪种方法来调用我的逻辑是正确的我已经得到了什么替代方法使用了上面的代码,但它不起作用。你们能帮帮我吗?提前谢谢。@Subrat我要做的第一件事是更改xml布局中的边距,看看是否可以将图像移动到那里,只是为了确保问题在onClick中,而不是布局安排中。您好Subrat,我已经更新了答案,请检查并回复我
int x = mParams.leftMargin;
mParams.leftMargin = x + 50