Android 如何重新定位ImageView

Android 如何重新定位ImageView,android,android-layout,android-imageview,Android,Android Layout,Android Imageview,我的xml文件中有一些imageview。在我的代码部分,我想根据我的意愿移动一个图像。为此,我完成了event.getx()和event.getY(),然后使用imageView.layout()。这个过程不起作用。如何移动该图像?我认为这可能会有帮助: package com.example.moveimageview; import android.os.Bundle; import android.app.Activity; import android.content.Co

我的xml文件中有一些
imageview
。在我的代码部分,我想根据我的意愿移动一个图像。为此,我完成了
event.getx()
event.getY()
,然后使用
imageView.layout()
。这个过程不起作用。如何移动该图像?

我认为这可能会有帮助:

    package com.example.moveimageview;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

    ImageView im;
    RelativeLayout rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting the wrapper layout from the xml
        rl = (RelativeLayout) findViewById(R.id.relative1);

        //getting the ImageView from xml
        im = (ImageView) findViewById(R.id.myImageView);

        rl.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent e) {
                // TODO Auto-generated method stub
                if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
                {
                    //sending the new coordinates to the method
                    //that will change the view's location
                    setImageViewLocation(e.getX(), e.getY());


                return true;
                }
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void setImageViewLocation(float x, float y)
    {
        //setting the coordinates
        im.setX(x);
        im.setY(y);

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                im.invalidate();//invoking the view onDraw
            }
        });

    }
}
(编辑)如果您想查看您的图像,下面是用于此的onTouchListner代码(与之前的代码相同,只需将onTouchListner替换为此代码):


我认为这可能有帮助:

    package com.example.moveimageview;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

    ImageView im;
    RelativeLayout rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting the wrapper layout from the xml
        rl = (RelativeLayout) findViewById(R.id.relative1);

        //getting the ImageView from xml
        im = (ImageView) findViewById(R.id.myImageView);

        rl.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent e) {
                // TODO Auto-generated method stub
                if(e.getAction()==android.view.MotionEvent.ACTION_DOWN)
                {
                    //sending the new coordinates to the method
                    //that will change the view's location
                    setImageViewLocation(e.getX(), e.getY());


                return true;
                }
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void setImageViewLocation(float x, float y)
    {
        //setting the coordinates
        im.setX(x);
        im.setY(y);

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                im.invalidate();//invoking the view onDraw
            }
        });

    }
}
(编辑)如果您想查看您的图像,下面是用于此的onTouchListner代码(与之前的代码相同,只需将onTouchListner替换为此代码):

检查这个问题检查这个问题