Android 在屏幕上拖放图像?

Posted

技术标签:

【中文标题】Android 在屏幕上拖放图像?【英文标题】:Android Drag and drop images on the Screen? 【发布时间】:2012-06-17 04:09:07 【问题描述】:

我正在让项目用户将图像从一个位置移动到屏幕上的另一个位置。我已经编写了一个示例代码来移动图像,但这里的问题是,如果我移动一个图像,相邻图像也会开始移动。这是示例代码。任何一个想法。

Main.java

public class MainActivity extends Activity  
   int windowwidth;
   int windowheight;    
   ImageView ima1,ima2;

   private android.widget.RelativeLayout.LayoutParams layoutParams ;
   // private android.widget.RelativeLayout.LayoutParams layoutParams ;
   //private android.widget.RelativeLayout.LayoutParams layoutParams ;           

     @Override
     public void onCreate(Bundle savedInstanceState) 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

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

         System.out.println("width" +windowwidth);
         System.out.println("height" +windowheight);             

         ima1 = (ImageView)findViewById(R.id.imageview1);
         ima1.setOnTouchListener(new View.OnTouchListener()   

public boolean onTouch(View v, MotionEvent event) 
       layoutParams = (RelativeLayout.LayoutParams) ima1.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();

              System.out.println("value of x" +x_cord);
              System.out.println("value of y" +y_cord);           

                    if (x_cord > windowwidth) 
                        x_cord = windowwidth;
                       
                    if (y_cord > windowheight) 
                        y_cord = windowheight;
                       
             layoutParams.leftMargin = x_cord-25;
             layoutParams.topMargin = y_cord-25;
             //   layoutParams.rightMargin = x_cord-25;
             //   layoutParams.bottomMargin = y_cord-25;
             ima1.setLayoutParams(layoutParams);
                     break;
               default: break;
                
               return true;
            
         );

         ima2 = (ImageView)findViewById(R.id.imageview2);
         ima2.setOnTouchListener(new View.OnTouchListener()          

     public boolean onTouch(View v, MotionEvent event) 
         layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
              switch(event.getActionMasked())
                 
                   case MotionEvent.ACTION_DOWN:
                       break;
                   case MotionEvent.ACTION_MOVE:
                       int x_cord = (int) event.getRawX();
                       int y_cord = (int) event.getRawY();

                       System.out.println("value of x1" +x_cord);
                   System.out.println("value of y1" +y_cord);                            

                        if (x_cord > windowwidth) 
                            x_cord = windowwidth;
                        
                        if (y_cord > windowheight) 
                            y_cord = windowheight;
                        
                        layoutParams.leftMargin = x_cord - 25;
                        layoutParams.topMargin = y_cord - 75;
                        ima2.setLayoutParams(layoutParams);
                        break;
                    default: break;
                
                return true;
            
        );
       
   

main.xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_
        android:layout_>
  <ImageView 
    android:layout_ 
    android:layout_
    android:id="@+id/imageview1" 
    android:src="@drawable/image1"  />    
<ImageView
    android:layout_ 
    android:layout_ 
    android:id="@+id/imageview2"
    android:src="@drawable/image2"   />             
 </RelativeLayout>

【问题讨论】:

这里是示例polamreddyn.blogspot.in/2013/02/… 【参考方案1】:

将以下代码写入您的活动文件。

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


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener()          

    @Override
    public boolean onTouch(View v, MotionEvent event) 
        layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
        switch(event.getActionMasked())
        
            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;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
        
        return true;
    
);

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener()          

    @Override
    public boolean onTouch(View v, MotionEvent event) 
        layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
        switch(event.getActionMasked())
        
            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;
                
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
        
        return true;
    
);

XML 文件:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_>
    <ImageView android:layout_ android:layout_
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_ android:layout_ android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

【讨论】:

请查看我编辑的答案并使用 imageview 而不是 textview。 使用那一个只移动一个图像并移动到一些角落,图像的尺寸减小,第二个图像无法移动...... 以上代码是独立移动两个图像。 让我们continue this discussion in chat java.lang.RuntimeException:无法实例化活动 ComponentInfocom.touchin/com.touchin.Main:java.lang.InstantiationException:com.touchin.Main 和原因:java.lang。实例化异常:com.touchin.Main【参考方案2】:

那是因为您将所有内容都放在了LinearLayout 中,这意味着您无法将项目放在您想要的位置,它们总是一个接一个。您可以尝试改用RelativeLayout。如果这还不够灵活,你应该看看Canvas

【讨论】:

澄清一下,'RelativeLayout' 上有 3 个图像,你想在屏幕上分别移动它们,它们应该能够相互重叠,对吧? 是的,布局中的三个图像分别在屏幕上移动 您保留了您的 onTouch 代码,只是将 LinearLayout 替换为 RelativeLayout 并且还有不止一个图像在移动,或者目前有什么问题?【参考方案3】:

原因是:你的屏幕上传action_move太懒了。

在正常情况下,即使您的手指没有在屏幕上移动,动作动作也会非常频繁地上传。但有些手机屏幕没那么敏感。

您可以修改手机的阈值。它需要内核支持。

【讨论】:

亲爱的,这种发帖的回答是不允许的。我的意思是你发布相同的答案,内容相同,这是不允许的。【参考方案4】:

我冒昧地交替使用您的代码来管理相对布局和随机位置中的多个图像视图。我还添加了一种更好的获取窗口大小的方法,因为 Display.getHeight() 已被弃用。

    if(Build.VERSION.SDK_INT >= 13)
        android.graphics.Point p = new android.graphics.Point();
        this.getWindowManager().getDefaultDisplay().getSize(p); 
        width = p.x;
        height = p.y;
    
    else
        Display display = getWindowManager().getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();
    

    RelativeLayout rel = new RelativeLayout(this);
    rel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    rel.setBackgroundResource(R.drawable.bg);

    pic = new ImageView[10];
    layoutParams1 = new LayoutParams[10];

    for(int i = 0; i < 10; i++)
        pic[i] = new ImageView(this);
        pic[i].setImageResource(R.drawable.img);  
        pic[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
        pic[i].setAdjustViewBounds(true);  
        pic[i].setScaleType(ScaleType.FIT_XY);  
        pic[i].setMaxHeight(88);  
        pic[i].setMaxWidth(100);
        pic[i].setMinimumHeight(88);
        pic[i].setMinimumWidth(100);
        pic[i].setTag(i);
        int x = rand.nextInt(width);
        while(x > width - 88)
            x = rand.nextInt(width);
        
        int y = rand.nextInt(height);
        while(y > height - 100)
            y = rand.nextInt(height);
        
        layoutParams1[i] = (RelativeLayout.LayoutParams) pic[i].getLayoutParams();
        layoutParams1[i].leftMargin = x;
        layoutParams1[i].topMargin = y;
        pic[i].setLayoutParams(layoutParams1[i]);
        pic[i].setOnTouchListener(new View.OnTouchListener()          

            @Override
            public boolean onTouch(View v, MotionEvent event) 
                int index = Integer.valueOf(v.getTag().toString());
                layoutParams1[index] = (RelativeLayout.LayoutParams) v.getLayoutParams();
                switch(event.getActionMasked())
                
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();
                        if (x_cord > width) 
                            x_cord = width;
                        
                        if (y_cord > height) 
                            y_cord = height;
                        
                        layoutParams1[index].leftMargin = x_cord - 44;
                        layoutParams1[index].topMargin = y_cord - 50;
                        pic[index].setLayoutParams(layoutParams1[index]);
                        break;
                    default:
                        break;
                
                return true;
            
        );

        rel.addView(pic[i]);
    


    setContentView(rel);

【讨论】:

【参考方案5】:

实际上,您可以通过以编程方式声明图像来避免这个问题。

int id = getResources().getIdentifier("image1", "drawable", getPackageName());
ImageView imageView1 = new ImageView(this);
LinearLayout.LayoutParams vp = 
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT);
imageView1.setLayoutParams(vp);        
imageView1.setImageResource(id);        
someLinearLayout.addView(imageView1); 

 int id = getResources().getIdentifier("image2", "drawable", getPackageName());
ImageView imageView2 = new ImageView(this);
LinearLayout.LayoutParams vp1 = 
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT);
imageView2.setLayoutParams(vp1);        
imageView2.setImageResource(id);        
someLinearLayout.addView(imageView2); 

并将触摸事件添加到添加的图像视图中

【讨论】:

以上是关于Android 在屏幕上拖放图像?的主要内容,如果未能解决你的问题,请参考以下文章

解释如何在 GTK+3.0 上拖放

Android拖放图像:ACTION_DROP从未调用

android 2.2 中的拖放布局

在android中拖放textview

在画布上拖放形状

画布上的 Android 拖放 2D