在android中拖放图像 [英] Drag and drop images in android

查看:28
本文介绍了在android中拖放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个单词争夺游戏,它允许用户将图像移动到图像..如果图像不匹配,那么它应该从它被拖动的位置返回到它的原始位置.我已经编写了一个示例代码移动图像,但这里的问题是,如果我移动一个图像,相邻图像也会开始移动.这是示例代码.

I m working on a word scramble game which allows user to move the image to image..if the image doesn't match then it should come back to it's original position from where it was dragged.I have written a sample code to move the image but the problem here is if I move one image the neighbouring image also starts moving.. Here is the sample code.

 /** 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>

请任何人帮助我..尽一切可能解决它..

Anyone plz help me out..Trying all possible thing to solve it..

推荐答案

您需要确保事件发生在给定的球中.即使您在图像对象之外进行触摸,也会声明 onTouch 事件.所以你可以做的是检查你已经阻止的 ACTION_DOWN 阶段,以确保触摸在对象的边界内.然后,您可以设置一个标志来告诉系统正在移动哪个球.我会让你编写代码,但希望这会让你知道该怎么做.

You need to make sure the event is happening in the given ball. The onTouch event will be declared even if you are touching outside of the image object. So what you could do is to check in the ACTION_DOWN phases you've blocked off to make sure the touch is in the bounds of the object. You could then set a flag to tell the system which ball is being moved. I'll let you do the coding up, but hopefully this will let you get an idea as to what to do.

这篇关于在android中拖放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆