myq_26 2013-04-23 03:06 采纳率: 16.7%
浏览 3202

在屏幕上移动图片的问题

用户想在屏幕上把图像从一个位置移动到另外一个位置,我设置了代码来移动图像,但是问题是如果我移动一个图像,邻近的图像也跟着一起移动。如何只移动一个图像?
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_width="fill_parent"
        android:layout_height="fill_parent">
  <ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp"
    android:id="@+id/imageview1" 
    android:src="@drawable/image1"  />    
<ImageView
    android:layout_width="100sp" 
    android:layout_height="100sp" 
    android:id="@+id/imageview2"
    android:src="@drawable/image2"   />             
 </RelativeLayout>
  • 写回答

1条回答

  • luhuajcdd 2013-04-23 07:51
    关注

    不能用相对布局,用绝对布局应该是可以的

    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?