3

我已經使用這個PhotoView庫自定義ImageView的。我想在特定點上縮放圖像。下面是我找到的方法是setScale(float scale, float focalX, float focalY, boolean animate)如何在特定的XY座標縮放(點)在ImageView的

我想知道我能傳遞的focalXfocalY的值,我有X和Y座標我傳遞目前,它擴展到非常不同的位置。

這裏是一個片段,

intResultX = intTotalX/intArraySize; 
intResultY = intTotalY/intArraySize; 
mMap.setScale(5, intResultX, intResultY, true); 
+0

'focalX'和'focalY'是從您的視圖的左上角 – pskink

+0

@pskink相對偏移我檢查了這一點,但我原來的XY位置是遠離視圖的左上角它縮小了一個角。 – Kuls

+0

如果你通過0,0的縮放是圍繞你的意見左上角,如果你通過getWidth()/ 2,getHeight()/ 2縮放是圍繞你的視圖的中心等完成 – pskink

回答

1

要以特定的XY座標放大在ImageView的可以帶有刻度沿傳遞focalX和focalY的值(必須是最大規模的PhotoView的分鐘刻度之間)和布爾值來設置動畫。

代碼來獲取最大最小刻度:

mPhotoView.getMinimumScale(); 

mPhotoView.getMaximumScale(); 

focalX和focalY它可以在屏幕上任意點,在這裏我已經採取了兩個例子一個是屏幕的中心,另一種是左上角。以下是這兩種情況的代碼。

代碼:

Random r = new Random(); 
float minScale = mPhotoView.getMinimumScale(); 
float maxScale = mPhotoView.getMaximumScale(); 
float randomScale = minScale + (r.nextFloat() * (maxScale - minScale)); 

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int height = displayMetrics.heightPixels; 
int width = displayMetrics.widthPixels; 
int centerX=width/2; 
int centerY =height/2; 

/*pass a value of focalX and focalY to scale image to center*/ 
//mPhotoView.setScale(randomScale, centerX, centerY, true); 

/*pass a value of focalX and focalY to scale image to top left corner*/ 
mPhotoView.setScale(randomScale, 0, 0, true); 
+0

但是如何縮放(縮放和移動)到位圖的x,y座標? https://stackoverflow.com/questions/48961771/how-to-scale-photoview-to-x-y-coordinates-of-bitmap – NickUnuchek

1

設置縮放到指定的規模。圖像將圍繞點 (focusX,focusY)爲中心。這些浮標範圍從0到1並且表示焦點 從視圖的左側和頂部的餾分。例如,左上 角的圖像將是(0,0)。而右下角將是(1,1)。

public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) { 

    /*setZoom can be called before the image is on the screen, but at this point, 
    image and view sizes have not yet been calculated in onMeasure. Thus, we should 
    delay calling setZoom until the view has been measured.*/ 

    if (!onDrawReady) { 
    delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType); 
    return; 
    } 

    if (scaleType != mScaleType) { 
    setScaleType(scaleType); 
    } 
    resetZoom(); 
    scaleImage(scale, viewWidth/2, viewHeight/2, true); 
    matrix.getValues(m); 
    m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f)); 
    m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f)); 
    matrix.setValues(m); 
    fixTrans(); 
    setImageMatrix(matrix); 
} 

希望這會有所幫助。快樂的編碼。

相關問題