2014-11-01 72 views
2

我正在使用eclipse將圖像置於縮放圖像後的ImageView中

我在用矩陣 縮放類型獲取圖像的中心時出現問題。

我的意思是,我想有矩陣規模型,也有對圖像中的中心...... 簡單地說,當我運行應用程序在第一次它的形象是規模,是在屏幕左側

我希望當我第一次運行應用程序時。將圖像放在視圖的中心,沒有比例。現在

我的第一個畫面是這樣的..

enter image description here

我希望它看起來像這樣..

enter image description here

  • 也追平這些話題,但couldn」找到任何解決方案。

Fit to Android Imageview Matrix Center of Screen

Scale and center matrix on arbitrary point

How to use scaletype of a ImageView as center and matrix at the sametime?

這是我的代碼...

public class MainActivity extends Activity { 

    private ImageView img; 
    private Matrix matrix = new Matrix(); 
    private float scale = 1f; 
    private ScaleGestureDetector SGD; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     img = (ImageView)findViewById(R.id.imageView1); 
     SGD = new ScaleGestureDetector(this,new ScaleListener()); 

    } 


    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     SGD.onTouchEvent(ev); 
     return true; 
    } 

    private class ScaleListener extends ScaleGestureDetector. 
    SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     scale *= detector.getScaleFactor(); 
     scale = Math.max(0.1f, Math.min(scale, 5.0f)); 
     matrix.setScale(scale, scale); 
     img.setImageMatrix(matrix); 
     return true; 
    } 
} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

佈局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:scaleType="matrix" 
    android:src="@drawable/gh" /> 

</RelativeLayout> 
+0

你的代碼看起來如何? – 2014-11-01 08:50:17

+1

縮小圖像大小n嘗試... – 2014-11-01 08:56:37

+0

顯示xml代碼@PouyaParakOctopus – 2014-11-01 09:09:05

回答

9

更好的解決方案是進行自定義的ImageView

public class TouchImageView extends ImageView { 

    Matrix matrix; 

    // We can be in one of these 3 states 
    static final int NONE = 0; 
    static final int DRAG = 1; 
    static final int ZOOM = 2; 
    int mode = NONE; 

    // Remember some things for zooming 
    PointF last = new PointF(); 
    PointF start = new PointF(); 
    float minScale = -3f; 
    float maxScale = 3f; 
    float[] m; 

    int viewWidth, viewHeight; 
    static final int CLICK = 3; 
    float saveScale = 1f; 
    protected float origWidth, origHeight; 
    int oldMeasuredWidth, oldMeasuredHeight; 

    ScaleGestureDetector mScaleDetector; 

    Context context; 

    public TouchImageView(Context context) { 
     super(context); 
     sharedConstructing(context); 
    } 

    public TouchImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     sharedConstructing(context); 
    } 

    private void sharedConstructing(Context context) { 
     super.setClickable(true); 
     this.context = context; 
     mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
     matrix = new Matrix(); 
     m = new float[9]; 
     setImageMatrix(matrix); 
     setScaleType(ScaleType.MATRIX); 

     setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       mScaleDetector.onTouchEvent(event); 
       PointF curr = new PointF(event.getX(), event.getY()); 

       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         last.set(curr); 
         start.set(last); 
         mode = DRAG; 
         break; 

        case MotionEvent.ACTION_MOVE: 
         if (mode == DRAG) { 
          float deltaX = curr.x - last.x; 
          float deltaY = curr.y - last.y; 
          float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale); 
          float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale); 
          matrix.postTranslate(fixTransX, fixTransY); 
          fixTrans(); 
          last.set(curr.x, curr.y); 
         } 
         break; 

        case MotionEvent.ACTION_UP: 
         mode = NONE; 
         int xDiff = (int) Math.abs(curr.x - start.x); 
         int yDiff = (int) Math.abs(curr.y - start.y); 
         if (xDiff < CLICK && yDiff < CLICK) 
          performClick(); 
         break; 

        case MotionEvent.ACTION_POINTER_UP: 
         mode = NONE; 
         break; 
       } 

       setImageMatrix(matrix); 
       invalidate(); 
       return true; // indicate event was handled 
      } 

     }); 
    } 

    public void setMaxZoom(float x) { 
     maxScale = x; 
    } 

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 
     @Override 
     public boolean onScaleBegin(ScaleGestureDetector detector) { 
      mode = ZOOM; 
      return true; 
     } 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      float mScaleFactor = detector.getScaleFactor(); 
      float origScale = saveScale; 
      saveScale *= mScaleFactor; 
      if (saveScale > maxScale) { 
       saveScale = maxScale; 
       mScaleFactor = maxScale/origScale; 
      } else if (saveScale < minScale) { 
       saveScale = minScale; 
       mScaleFactor = minScale/origScale; 
      } 

      if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight) 
       matrix.postScale(mScaleFactor, mScaleFactor, viewWidth/2, viewHeight/2); 
      else 
       matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY()); 

      fixTrans(); 
      return true; 
     } 
    } 

    void fixTrans() { 
     matrix.getValues(m); 
     float transX = m[Matrix.MTRANS_X]; 
     float transY = m[Matrix.MTRANS_Y]; 

     float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale); 
     float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale); 

     if (fixTransX != 0 || fixTransY != 0) 
      matrix.postTranslate(fixTransX, fixTransY); 
    } 

    float getFixTrans(float trans, float viewSize, float contentSize) { 
     float minTrans, maxTrans; 

     if (contentSize <= viewSize) { 
      minTrans = 0; 
      maxTrans = viewSize - contentSize; 
     } else { 
      minTrans = viewSize - contentSize; 
      maxTrans = 0; 
     } 

     if (trans < minTrans) 
      return -trans + minTrans; 
     if (trans > maxTrans) 
      return -trans + maxTrans; 
     return 0; 
    } 

    float getFixDragTrans(float delta, float viewSize, float contentSize) { 
     if (contentSize <= viewSize) { 
      return 0; 
     } 
     return delta; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     viewWidth = MeasureSpec.getSize(widthMeasureSpec); 
     viewHeight = MeasureSpec.getSize(heightMeasureSpec); 

     // 
     // Rescales image on rotation 
     // 
     if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight 
       || viewWidth == 0 || viewHeight == 0) 
      return; 
     oldMeasuredHeight = viewHeight; 
     oldMeasuredWidth = viewWidth; 

     if (saveScale == 1) { 
      //Fit to screen. 
      float scale; 

      Drawable drawable = getDrawable(); 
      if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) 
       return; 
      int bmWidth = drawable.getIntrinsicWidth(); 
      int bmHeight = drawable.getIntrinsicHeight(); 

      Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight); 

      float scaleX = (float) viewWidth/(float) bmWidth; 
      float scaleY = (float) viewHeight/(float) bmHeight; 
      scale = Math.min(scaleX, scaleY); 
      matrix.setScale(scale, scale); 

      // Center the image 
      float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight); 
      float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth); 
      redundantYSpace /= (float) 2; 
      redundantXSpace /= (float) 2; 

      matrix.postTranslate(redundantXSpace, redundantYSpace); 

      origWidth = viewWidth - 2 * redundantXSpace; 
      origHeight = viewHeight - 2 * redundantYSpace; 
      setImageMatrix(matrix); 
     } 
     fixTrans(); 
    } 
} 

,只需使用該TouchImageView

public class MultiTouchActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TouchImageView img = new TouchImageView(this); 
     img.setImageResource(R.drawable.img); 
     img.setMaxZoom(4f); 
     setContentView(img); 
    } 
} 

希望它會幫助你:)
Refer this to get more info

更新:

您可以使用XML也

,無論你想添加這個控制在xml文件

<com.example.test.TouchImageView 
    android:id="@+id/imageViewMy" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/img" 
    /> 

所以你的xml將如下所示:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <com.example.test.TouchImageView 
     android:id="@+id/imageViewMy" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/img" /> 

</RelativeLayout> 

活動:

public class MultiTouchActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity1); 
     TouchImageView img = (TouchImageView) findViewById(R.id.imageViewMy); 
     img.setMaxZoom(4f); 
    } 
} 
+0

我綁它,但它不在中間,它變得更糟...... – 2014-11-01 09:43:48

+0

使用'android:layout_centerInParent =「true」' – 2014-11-01 09:46:27

+0

看圖像...... https://drive.google.com/file/d/0Bz-YU0FsDfp0NGNjMWNKejdILUE/view?usp =分享 – 2014-11-01 09:46:45

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:scaleType="matrix"    
    android:src="@drawable/gh" /> 

</RelativeLayout> 

android:layout_centerHorizontal="true" <!-- To take it to center --> android:scaleType="fitXY" <!-- Fit the Image -->

變化layot寬度和高度wrap_content

而作爲每SCR裁剪圖像大小。並將它們存儲在不同的可繪製文件夾中

+0

感謝您的回答......但我必須將刻度類型設置爲矩陣才能獲得縮放....我的意思是我無法將其更改爲「fitXY」,在這種情況下,我不會擁有雙指縮放功能 – 2014-11-01 10:26:20

+0

然後u需要裁剪圖像按屏幕大小,並把他們在不同的文件夾繪製@PouyaParakOctopus – 2014-11-01 10:30:23

+0

如何??????????????? – 2014-11-01 10:33:32

0

設置寬度= 512和高度= 300.和圖像作爲背景 嘗試我的代碼。其工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:layout_gravity="center"> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="500dp" 
    android:layout_height="300dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:scaleType="matrix" 
    android:background="@drawable/imgg" /> 

</RelativeLayout> 
+0

謝謝......這是更好的....但看,我已經記錄的視頻/我怎樣才能讓它不知何故,當我縮小它,我到達屏幕的中間不是西北:)它... https://drive.google.com/file/d/0Bz-YU0FsDfp0UWZhM21SaEloVzQ/view?usp =分享 – 2014-11-01 11:20:05

+0

這意味着你的形象問題是解決儀式? – 2014-11-01 11:24:50

+0

是的......一些如何! ......在之前的評論中我只提到過一個小問題 – 2014-11-01 12:00:37