2012-12-07 55 views
0

我有一個佈局,其中包括兩個ImageViews。我想創建一個簡單的方法來在ImageViews中包含縮放。我看過一些代碼,但它們只能在單個ImageView上工作。 是否有可能使用ImageViews在佈局上進行這項工作?在Eclipse eclipse中捏縮放佈局

回答

0

寫下面的代碼拖動兩個圖像到您的活動文件,並實現捏縮放代碼到這個。

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_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:layout_width="50sp" android:layout_height="50sp" 
     android:id="@+id/image" android:src="@drawable/image"> 
    </ImageView> 
    <ImageView android:layout_y="30dip" android:layout_x="118dip" 
     android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1" 
     android:src="@drawable/image1"> 
    </ImageView> 
</RelativeLayout> 

,看看下面的鏈接瞭解更多信息。

Drag & Drop Two Images

+0

阻力是工作的罰款.....如何實現這個 – user960439

+0

捏放大@ user960439在我的代碼實現您捏縮放代碼。 –

+0

我已經實現了這個代碼http://stackoverflow.com/questions/10630373/android-image-view-pinch-zooming它不適合我....你有任何代碼請幫助 – user960439