根据按钮单击移动图像

时间:2013-04-06 09:09:51

标签: android

我使用了四个按钮(向上,向下,向左,向右),我也有图像。我必须相应地移动图像,当我按下向上按钮我应该向上移动图像,当我按左侧同样保持两个方向它应该向左移动图像。我使用onclick听众然后我试图移动图像使用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);


    }

}

HI,     我已经更新了以下代码,请检查它。

package 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;
               }


}
    }
 }

1 个答案:

答案 0 :(得分:0)

我认为问题是(int)getRawx();(int)getRawy();,你在Activity上调用这些方法,我不确定你真的需要这些值。

你可以尝试:

mParams.leftMargin += 50;

int x = mParams.leftMargin;
mParams.leftMargin = x + 50