2017-02-23 100 views
1

我正在開發一個android應用程序,在我的應用程序中動態創建一些ImageView並將該ImageView設置爲RelativeLayout。所以我想獨立地拖動佈局中的每個圖像。現在它的工作但不正確,當我拖動圖像,然後圖像開始振動和緩慢移動。而我想實現捏捏變焦在這image.Is它可能嗎?以及如何在移動圖像時去除振動?如果有任何人知道它,請幫助我..onTouchListener在ImageView中無法正常工作

感謝

這是我onTouchListener()

canvasImage[i].setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 


       x1 = motionEvent.getX(); 
       y1 = motionEvent.getY(); 

       //Log.v("vvvvvv", "" + x1 + " " + y1); 
       final int action = motionEvent.getAction(); 

       switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 

        case MotionEvent.ACTION_DOWN: 

         String w = imglayout.getTop() + " " + imglayout.getHeight(); 
         Log.v("wwww", "" + w); 

         x2 = motionEvent.getX(); 
         y2 = motionEvent.getY(); 

         dx = x2 - canvasImage[finalI].getX(); 
         dy = y2 - canvasImage[finalI].getY(); 

         //Log.v("cccccc", "" + x2 + " " + y2); 
         moving = true; 
         break; 
        case MotionEvent.ACTION_UP: 
         moving = false; 
         break; 
        case MotionEvent.ACTION_MOVE: 


         if (moving) { 

          canvasImage[finalI].setX(motionEvent.getX() - dx); 
          canvasImage[finalI].setY(motionEvent.getY() - dy); 

          //Log.v("qqqqqq", "" + canvasImage[finalI].getX()); 
         } 
         break; 
       } 
       //mainLayout.invalidate(); 
       return true; 
      } 

     }); 

這是我setCanvasImage()用於設置圖像轉換成RelativeLayout的

方法
public void setCanvasImage() { 


    int screenSize = getResources().getConfiguration().screenLayout & 
      Configuration.SCREENLAYOUT_SIZE_MASK; 


    final int imgCount = dbobj.getFromTemp(); 
    canvasImage = new ImageView[imgCount]; 
    imglayout = (RelativeLayout) findViewById(R.id.canvas); 

    final String[] strImage = dbobj.getdImage(); 

    imglayout.removeAllViews(); 


    for (int i = 0; i < imgCount; i++) { 

     canvasImage[i] = new ImageView(this); 
     canvasImage[i].setTag("img_" + i); 
     boolean tabletSize = getResources().getBoolean(R.bool.isTablet); 

     if (tabletSize) { 

      int imgWidth = 130; 
      int imgHeight = 130; 

      RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight); 
      paramss.setMargins(posLeft, posTop, 0, 0); 
      canvasImage[i].setLayoutParams(paramss); 

      canvasImage[i].setScaleType(ScaleType.FIT_XY); 
      canvasImage[i].setTag(strImage[i]); 
      setImage(strImage[i], canvasImage[i]); 
      imglayout.addView(canvasImage[i]); 


     } 
     if(screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL){ 


      int imgWidth = 100; 
      int imgHeight = 100; 
      RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight); 
      paramss.setMargins(posLeft, posTop, 0, 0); 
      canvasImage[i].setLayoutParams(paramss); 
      canvasImage[i].setTag(strImage[i]); 
      setImage(strImage[i], canvasImage[i]); 
      imglayout.addView(canvasImage[i]); 
     } 

     posLeft = posLeft + 15; 
    } 
} 

RelativeLayout xml文件

<RelativeLayout 
    android:layout_below="@+id/btnhorscrolvw" 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:id="@+id/canvas" 
    android:background="#ffff" 
    android:orientation="horizontal" 
    > 
</RelativeLayout> 
+0

嘗試添加andoid:clickable =「true」 – Ak9637

+0

但它不工作。 – Astro

回答

1

我認爲這將工作。

float dX, dY; 

@Override 
public boolean onTouch(View view, MotionEvent event) { 

switch (event.getAction()) { 

    case MotionEvent.ACTION_DOWN: 

     dX = view.getX() - event.getRawX(); 
     dY = view.getY() - event.getRawY(); 
     break; 

    case MotionEvent.ACTION_MOVE: 

     view.animate() 
       .x(event.getRawX() + dX) 
       .y(event.getRawY() + dY) 
       .setDuration(0) 
       .start(); 
     break; 
    default: 
     return false; 
} 
return true; 
} 
+0

哦....它真的工作。感謝您的回答。 – Astro

0

您需要使用的ImageView的scalx和scaleY因素實現捏縮放

imageview.setScaleX(scalefactor); 
imageview.setScaleY(scalefacto); 

的Android可以調整保證金左和利潤率最高爲draging的ImageView的

你可以看到這個函數的完整實現在Move zoom and rotate imageview in android

相關問題