2014-05-12 55 views
0

我想在縮放的自定義圖像視圖中移動和縮放android上的位圖。 這正常工作與getImageMatrix()反轉(invertMatrix)重寫的onTouchEvent:通過倒置矩陣移動位圖,移動後位圖消失

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (!isStringEmpty(getTextForCanvas())) { 
     dumpEvent(event); 
     eventX = event.getX(); 
     eventY = event.getY(); 
     eventXY = new float[] {eventX, eventY}; 
     getImageMatrix().invert(invertMatrix); 
     invertMatrix.mapPoints(eventXY); 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       savedMatrix.set(invertMatrix); 
       //start.set(event.getRawX(), event.getRawY()); 
       start.set(eventX, eventY); 
       mode = Constants.DRAG; 
       break; 
      case MotionEvent.ACTION_POINTER_DOWN: 
       oldDist = spacing(event); 
       if (oldDist > 10f) { 
        savedMatrix.set(invertMatrix); 
        midPoint(mid, event); 
        mode = Constants.ZOOM; 
       } 
       break; 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_POINTER_UP: 
       mode = Constants.NONE; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       if (mode == Constants.DRAG) { 
        invertMatrix.set(savedMatrix); 
        invertMatrix.postTranslate(event.getX() - start.x, event.getY() - start.y); 
       } else if (mode == Constants.ZOOM) { 
        float newDist = spacing(event); 
        if (newDist > 10f) { 
         invertMatrix.set(savedMatrix); 
         scale = newDist/oldDist; 
         invertMatrix.postScale(scale, scale, mid.x, mid.y); 
        } 
       } 
       break; 
     } 
    } 

    setImageMatrix(invertMatrix); 
    return true; 
} 

的的onDraw方法:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawBitmap(mImageBitmap, 0, 0, mImagePaint); 
    canvas.drawBitmap(canvasBitmap, invertMatrix, null); 
} 

但最後invertMatrix失去的數據,並因此至少位圖繪製在開始時(Android 4.4)或消失(Android 4.2)。 這裏的inverseMatrix的日誌:

I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 650.47687][0.0, 0.0, 1.0]} 
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 652.96216][0.0, 0.0, 1.0]} 
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 654.5][0.0, 0.0, 1.0]} 
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 656.0872][0.0, 0.0, 1.0]} 
I/System.out﹕ Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]} 

任何人都可以幫助我嗎?也許我必須在無效的情況下存儲矩陣?怎麼樣?

更新:登錄Android 4.2版本:(矩陣COORDS反轉):

I/System.out﹕ Matrix{[1.0, -0.0, 101.778534][-0.0, 1.0, 225.0][0.0, 0.0, 1.0]} 
I/System.out﹕ Matrix{[1.0, 0.0, -101.778534][0.0, 1.0, -225.0][0.0, 0.0, 1.0]} 
+0

要echieve什麼? – pskink

+0

你大概可以閱讀我的答案在這裏http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink

+0

謝謝你的答案。我不想實現任何想法。我只想保持矩陣。正如您在日誌中看到的那樣,逆矩陣最後會丟失其數據。同時我不知道爲什麼。也許這個問題類似於http://stackoverflow.com/questions/20431374/bug-with-android-4-3-imageview-method-getimagematrix/20476958#20476958,但我的應用程序既不在4.2也不在4.4。 – user3626019

回答

0

解決方案:

 if (firstTime) { 
      eventXY = new float[] {eventX, eventY}; 
      getImageMatrix().invert(invertMatrix); 
      invertMatrix.mapPoints(eventXY); 
      firstTime = false; 
     }