在安卓系统中拖放图片

8

我正在开发一个单词拼字游戏,允许用户移动图像到图像...如果图像不匹配,则应该回到它被拖动的原始位置。我已经编写了一个样例代码来移动图像,但问题在于,如果我移动一个图像,邻近的图像也开始移动...以下是样例代码:

 /** Touchmoveimage.java*/
     package com.examples.Touchmoveimage;
     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.MotionEvent;
     import android.view.View;
     import android.view.View.OnTouchListener;
     import android.widget.ImageView;
     import android.widget.LinearLayout.LayoutParams;
     public class Touchmoveimage extends Activity implements OnTouchListener{

int windowwidth;
int windowheight;

   private LayoutParams layoutParams ;
   private LayoutParams layoutParams1 ;
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

  windowwidth = getWindowManager().getDefaultDisplay().getWidth();
  windowheight = getWindowManager().getDefaultDisplay().getHeight();

  ImageView ball= (ImageView)findViewById(R.id.ball);
  ball.setOnTouchListener(this);

  ImageView ball1 = (ImageView)findViewById(R.id.ball1);
  ball1.setOnTouchListener(this); 

 }   
 public boolean onTouch(View v,MotionEvent event) {
 switch(v.getId())
 {
 case R.id.ball:
   ImageView ball= (ImageView)findViewById(R.id.ball);
   layoutParams = (LayoutParams) ball.getLayoutParams();
      switch(event.getAction())
      {
      case MotionEvent.ACTION_DOWN:   
             break;
      case MotionEvent.ACTION_MOVE:
                   int x_cord = (int)event.getRawX();
                   int y_cord = (int)event.getRawY();

                   if(x_cord>windowwidth){x_cord=windowwidth;}
                   if(y_cord>windowheight){y_cord=windowheight;}

                  layoutParams.leftMargin = x_cord -25;
                 layoutParams.topMargin = y_cord - 75;

                 ball.setLayoutParams(layoutParams);
             break;
             default : break;
      }
 case R.id.ball1:
     ImageView ball1= (ImageView)findViewById(R.id.ball1);
       layoutParams1 = (LayoutParams) ball1.getLayoutParams();
          switch(event.getAction())
          {
          case MotionEvent.ACTION_DOWN:   
                 break;
          case MotionEvent.ACTION_MOVE:
                       int x_cord = (int)event.getRawX();
                       int y_cord = (int)event.getRawY();

                       if(x_cord>windowwidth){x_cord=windowwidth;}
                       if(y_cord>windowheight){y_cord=windowheight;}

                      layoutParams1.leftMargin = x_cord -25;
                       layoutParams1.topMargin = y_cord - 75;

                       ball1.setLayoutParams(layoutParams1);
                 break;          
       default : break;
        }
      }
     return true;}

      }

     <!-- main.xml -->

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
      android:layout_height="fill_parent"
     >
     <ImageView 
     android:layout_width="50sp" 
     android:layout_height="50sp" 
     android:id="@+id/ball"
    android:src="@drawable/ball">
   </ImageView>

   <ImageView 
    android:layout_y="30dip" 
    android:layout_x="118dip" 
    android:layout_width="50sp" 
    android:layout_height="50sp" 
    android:id="@+id/ball1"
    android:src="@drawable/ball1">
   </ImageView>

    </LinearLayout>

请大家帮忙解决这个问题,我已经尝试了所有可能的方法来解决它。


你好 Pinky,你的问题解决了吗? - Dipak Keshariya
http://tech-papers.org/android-drag-and-drop/ - Balwinder SIngh
请参见:https://developer.android.com/guide/topics/ui/drag-drop.html#DesignDragOperation - valerybodak
5个回答

1

你可以从这段代码中得到灵感,它将非常有帮助...而且我也在这个概念上工作。

package com.example.drag;

 import android.app.*;
 import android.graphics.*;
 import android.os.*;
 import android.util.Log;
 import android.view.*;
 import android.view.View.*;
 import android.widget.*;
 import android.widget.AbsoluteLayout.LayoutParams;

public class MainActivity extends Activity implements OnTouchListener {
    // The letter that the user drags.
   // The letter outline that the user is supposed to drag letterView to.
    private AbsoluteLayout mainLayout;
    TextView letterView;
    TextView emptyLetterView;
    TextView letter_E,letter_I,letter_O,letter_U;
    TextView letter_C,letter_T;
    int status=0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mainLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
            mainLayout.setOnTouchListener(this);
            letterView = (TextView) findViewById(R.id.imageView);
            letterView.setOnTouchListener(this);

            emptyLetterView = (TextView) findViewById(R.id.textView1);

            letter_E= (TextView) findViewById(R.id.text_E);
            letter_I= (TextView) findViewById(R.id.text_I);
            letter_O= (TextView) findViewById(R.id.text_O);
            letter_U= (TextView) findViewById(R.id.text_U);
            letter_C= (TextView) findViewById(R.id.text_C);
            letter_T= (TextView) findViewById(R.id.text_T);


            letter_I.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    status=2;
                    return false;
                }
            });
            letter_E.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    status=1;
                    Log.i("ImageStatus",""+status);
                    return false;
                }
            });
            AbsoluteLayout abl=(AbsoluteLayout)findViewById(R.id.absLayout);
            abl.setOnTouchListener(new OnTouchListener() {

                   @Override
                   public boolean onTouch(View v, MotionEvent event) {
                       // TODO Auto-generated method stub
                       Log.i("touch",""+event);

                       if(status==1) // any event from down and move
                       {
                           LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-letter_E.getWidth()/2,(int)event.getY()-letter_E.getHeight()/2);
                           letter_E.setLayoutParams(lp);                      
                       }
                       else if (status==2) {
                           LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-letter_E.getWidth()/2,(int)event.getY()-letter_E.getHeight()/2);
                           letter_I.setLayoutParams(lp);
                    }
                       if(event.getAction()==MotionEvent.ACTION_UP){
                           status=0;
                           letter_E.setBackgroundColor(Color.TRANSPARENT);
                           letter_I.setBackgroundColor(Color.TRANSPARENT);
                       }
                       return true;
                }
            });

            Typeface type= Typeface.createFromAsset(getAssets(), "AlphaBalloon.ttf");

            letterView.setTypeface(type);
            emptyLetterView.setTypeface(type);
            letter_E.setTypeface(type);
            letter_I.setTypeface(type);
            letter_O.setTypeface(type);
            letter_U.setTypeface(type);
            letter_C.setTypeface(type);
            letter_T.setTypeface(type);
    }

    private boolean dragging = false;
    private Rect hitRect = new Rect();

    @Override
    /**
     * NOTE:  Had significant problems when I tried to react to ACTION_MOVE on letterView.   Kept getting alternating (X,Y) 
     * locations of the motion events, which caused the letter to flicker and move back and forth.  The only solution I could 
     * find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE 
     * associated with the mainLayout.
     */
    public boolean onTouch(View v, MotionEvent event) {
            boolean eventConsumed = true;
            int x = (int)event.getX();
            int y = (int)event.getY();

            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                    if (v == letterView) {
                            dragging = true;
                            eventConsumed = false;
                    }
            } else if (action == MotionEvent.ACTION_UP) {

                    if (dragging) {
                            emptyLetterView.getHitRect(hitRect);
                            if (hitRect.contains(x, y))
                                    setSameAbsoluteLocation(letterView, emptyLetterView);
                    }
                    dragging = false;
                    eventConsumed = false;

            } else if (action == MotionEvent.ACTION_MOVE) {
                    if (v != letterView) {
                            if (dragging) {
                                    setAbsoluteLocationCentered(letterView, x, y);
                            }
                    }
            }

            return eventConsumed;
    }
    private void setSameAbsoluteLocation(View v1, View v2) {
            AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
            setAbsoluteLocation(v1, alp2.x, alp2.y);


            letterView.setVisibility(View.GONE);
            emptyLetterView.setText("A");
            emptyLetterView.setTextColor(Color.BLACK);
            Toast.makeText(getApplicationContext(), "C for Cat", Toast.LENGTH_SHORT).show();
    }
    private void setAbsoluteLocationCentered(View v, int x, int y) {
            setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);

    }
    private void setAbsoluteLocation(View v, int x, int y) {
            AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
            alp.x = x;
            alp.y = y;
            v.setLayoutParams(alp);
    }
  }

And XML is.....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LLayout">


<AbsoluteLayout
    android:id="@+id/absLayout"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true" >


    <TextView
      android:id="@+id/imageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="10dp"
      android:layout_y="-3dp"
      android:text="A"
      android:textSize="80dp" />

  <TextView
      android:id="@+id/text_"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="73dp"
      android:layout_y="-3dp"
      android:text="E"
      android:textSize="80dp"/>

  <TextView
      android:id="@+id/text_I"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="134dp"
      android:layout_y="-4dp"
      android:text="I"
      android:textSize="80dp" />

  <TextView
      android:id="@+id/text_O"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="196dp"
      android:layout_y="-4dp"
      android:text="O"
      android:textSize="80dp" />

  <TextView
      android:id="@+id/text_U"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="260dp"
      android:layout_y="-4dp"
      android:text="U"
      android:textSize="80dp" />



  <TextView
      android:id="@+id/text_T"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="196dp"
      android:layout_y="131dp"
      android:text="T"
      android:textSize="80dp" />

  <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="120dp"
      android:layout_y="128dp"
      android:text="A"
      android:textColor="#ffff9f"
      android:textSize="80dp" />

  <TextView
      android:id="@+id/text_C"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="45dp"
      android:layout_y="129dp"
      android:text="C"
      android:textSize="80dp" />

 </AbsoluteLayout>
 <AbsoluteLayout
    android:id="@+id/absLayoutNew"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
 </AbsoluteLayout>

 <TextView
     android:id="@+id/text_E"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="H"
     android:textSize="80dp" />


我遇到了一个问题。如果用户在布局的左侧、右侧、顶部或底部拖动或按压过多,应用程序会崩溃。这是链接:http://stackoverflow.com/questions/21347431/app-is-fc-if-the-user-presses-too-much-to-left-or-right - Si8

1

您需要确保事件发生在给定的球中。即使触摸到图像对象之外,也会声明onTouch事件。因此,您可以在ACTION_DOWN阶段检查是否已封锁以确保触摸在对象的边界内。然后,您可以设置一个标志来告诉系统正在移动哪个球。我会让您进行编码,但希望这能让您了解应该做什么。


谢谢...我尝试在触摸对象时设置标志,但那也没有帮助...:( - Veena
你需要确保手指在球的范围内。尝试在 ACTION_DOWN 被按下时仅打印 x/y 坐标,看看会发生什么。将其与图像的边界进行比较。 - PearsonArtPhoto
是的,我尝试打印坐标。如果触碰到第一个球,它会显示其坐标,并打印出当第二个球被触碰时应该显示的消息。这让我感到困惑。顺便问一下,我的方法正确吗?还有其他做法吗? - Veena
我遇到了一个问题。如果用户在布局的左侧、右侧、顶部或底部拖动或按压过多,应用程序会崩溃。这是链接:http://stackoverflow.com/questions/21347431/app-is-fc-if-the-user-presses-too-much-to-left-or-right - Si8

0
我真是太傻了...仔细看了一下,我意识到你的基本方法是错误的。你想在球的ImageView上设置onTouchListener。实际上,我会创建一个球对象,因为你以后肯定会想对它们做些什么。原始坐标在那里同样有效,所以...试试这个方法,然后告诉我它的效果如何。

嘿,创建一个球对象怎么做?在我的游戏中,我会动态地将图像添加到ImageView中。 - Veena

0
问题出在LinearLayout上,因为它的视图是水平或垂直相关的。只需将您的布局更改为FrameLayout或已弃用的AbsoluteLayout即可解决问题。这对您来说完全没问题。

我尝试了,但不起作用:(...现在正在以不同的方式尝试它..无论如何,谢谢..:) - Veena

0

我正在尝试编写一个类似的程序并遇到了同样的问题。 我觉得整个程序只能有一个“onTouchListener”。我使用FrameLayout,并且当我点击屏幕时,我想要出现一个新球。 当我定义“FrameLayout.setOnTouchListener”时它可以工作。

如果我触摸现有的球,我希望将它拖动到其他地方。 当“Ball.setOnTouchListener”被定义时它可以工作。

但是,当我同时使用“setOnTouchListeners”时,其中只有一个会起作用。

因此,我认为Android只能使用一个“setOnTouchListener”,并且您必须以某种方式(我不知道如何)检查哪个动态创建的球被触摸并处理它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接