【问题标题】:how to move an image on xml layout when user on touch event?用户在触摸事件时如何在 xml 布局上移动图像?
【发布时间】:2012-03-10 04:31:21
【问题描述】:

我已经实现了一个带有图像的应用程序。在我的应用程序中,当用户触摸图像时,我在图像上使用过我想随着他的手指触摸移动图像。我已按如下方式实现了我的应用程序:

((ImageView)findViewById(R.id.imageView1)).setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction()) {
         case MotionEvent.ACTION_MOVE:
            //I would like to Move image along with user finger touch code
            break;

         default:
            break;
      }
      return false;
   }
});

从上面的代码中,我无法将图像与用户手指一起移动。

【问题讨论】:

    标签: android image imageview move ontouchlistener


    【解决方案1】:

    有一个示例可以实现这一点:

    http://blahti.wordpress.com/2011/01/17/moving-views-part-2/

    【讨论】:

    【解决方案2】:

    只是一个建议,它对我有用:

    使onTouch()方法的return false为true

    【讨论】:

      【解决方案3】:

      我正在使用此代码来实现此目的

      package mani.droid.touchdrag;
      
      import android.os.Bundle;
      import android.app.Activity;
      import android.content.Context;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.graphics.Canvas;
      import android.util.Log;
      import android.view.MotionEvent;
      import android.view.View;
      
      public class MainActivity extends Activity {
      
      Bitmap img;
      float x;
      float y;
      boolean isStarted = false;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
      
          setContentView(new MyScreen(this));
      }
      
      public class MyScreen extends View {
      
          Context context;
      
          public MyScreen(Context context) {
              super(context);
              // TODO Auto-generated constructor stub
              this.context = context;
          }
      
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              // TODO Auto-generated method stub
              switch(event.getAction())
              {
                  case MotionEvent.ACTION_DOWN:
                      float xdiff = Math.abs( x - event.getX());
                      float ydiff =  Math.abs( y - event.getY());
                      if( xdiff < 23 || ydiff < 23 ){
                          isStarted = true;
                      }
                      break;
      
                  case MotionEvent.ACTION_MOVE:
                      if(isStarted)
                      {
                          x = event.getX() - img.getWidth()/2;
                          y = event.getY() - img.getHeight()/2;
                          Log.v("X:" + x, "Y: " + y);
                          this.invalidate();
                      }
                      break;
      
                  case MotionEvent.ACTION_UP:
                      isStarted = false;
      
              }
              return true;
          }
      
          @Override
          protected void onDraw(Canvas canvas) {
              // TODO Auto-generated method stub
              super.onDraw(canvas);
      
              canvas.drawBitmap(img, x, y, null);
          }
      }
      }
      

      它的工作完美......

      【讨论】:

        猜你喜欢
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-26
        相关资源
        最近更新 更多