4

プロジェクトユーザーが画面上のある位置の画像を別の位置に移動するように取り組んでいます。画像を移動するためのサンプルコードを作成しましたが、ここでの問題は、1つの画像を移動すると、隣接する画像も移動を開始することです。これがサンプルコードです。

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>
4

5 に答える 5

2

これは、すべてをに入れているためです。つまり、必要LinearLayoutな場所にアイテムを配置することはできません。アイテムは常に次々に配置されます。RelativeLayout代わりに使用してみてください。それが十分に柔軟でない場合は、を参照してくださいCanvas

于 2012-06-14T06:59:29.517 に答える
0

その理由は次のとおりです。画面のアップロード action_move が遅すぎます。

通常、画面上で指を動かしていなくても、アクション ムーブは非常に頻繁にアップロードされます。ただし、一部の電話画面はそれほど敏感ではありません。

電話のしきい値を変更できます。カーネルのサポートが必要です。

于 2013-03-19T11:23:28.127 に答える
0

実際には、プログラムでイメージを宣言することで、この問題を回避できます。

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); 

追加されたイメージビューにタッチイベントを追加します

于 2014-08-04T06:37:32.530 に答える
0

RelativeLayout とランダムな場所で複数のイメージビューを管理するために、コードを自由に変更しました。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);
于 2013-04-08T23:08:18.353 に答える