如何重新定位ImageView

时间:2012-04-25 10:56:27

标签: android android-layout android-imageview

我的xml文件中有一些ImageViews。在我的代码部分,我想根据自己的意愿移动图像。为此,我已完成event.getx()event.getY(),然后使用imageView.layout()。此过程无效。我该如何移动该图像?

1 个答案:

答案 0 :(得分:1)

我认为这可能会有所帮助:

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

    }
}

(编辑)如果你想要你的ImageView,这里是onTouchListner代码(与之前的代码相同,只需用此替换OnTouchListner):

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.getRawX(), e.getRawY());


            return true;
            }

            if(e.getAction()==android.view.MotionEvent.ACTION_MOVE)
            {
                setImageViewLocation(e.getRawX(), e.getRawY());
                return true;
            }
            return false;
        }
    });