2013-04-24 69 views
4

我有一个不同大小的图像库。每个图像依次显示在ImageView之内(通过OnTouchListener)。我需要知道图片框架的位置,我正在向ImageView亲属展示,但通过我已经完成的测试,我只获得了ImageView的坐标。任何想法?如何在ImageView中获取图片的位置?

enter image description here

我需要的(X1,Y1)和(X2,Y2)的值。

在此先感谢。

这是我的课:

public class PuzzleView extends ImageView { 

protected Paint currentPaint;  

protected boolean drawRect = false;  
protected float left; 
protected float top; 
protected float right; 
protected float bottom; 

protected float pixelX; 
protected float pixelY; 

protected int nChunksX = 5; 
protected int nChunksY = 5; 

protected int currentWidth = 0; 
protected int currentHeight = 0; 

public PuzzleView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    currentPaint = new Paint(); 
    currentPaint.setDither(true); 
    currentPaint.setColor(0xFF00CC00); 
    currentPaint.setStyle(Paint.Style.STROKE); 
    currentPaint.setStrokeJoin(Paint.Join.ROUND); 
    currentPaint.setStrokeCap(Paint.Cap.ROUND); 
    currentPaint.setStrokeWidth(2);       
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    float chunkWidth = currentWidth/nChunksX; 
    float chunkHeight = currentHeight/nChunksY; 

    float posX = ((int)(pixelX/chunkWidth)) * chunkWidth; 
    float posY = ((int)(pixelY/chunkHeight)) * chunkHeight; 

    canvas.drawRect(posX, posY, posX + chunkWidth, posY + chunkHeight, currentPaint); 

    Rect rect = this.getDrawable().getBounds(); 
    canvas.drawRect(rect, currentPaint); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    // Get image matrix values and place them in an array 
    float[] f = new float[9]; 
    getImageMatrix().getValues(f); 

    // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY) 
    final float scaleX = f[Matrix.MSCALE_X]; 
    final float scaleY = f[Matrix.MSCALE_Y]; 

    // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight) 
    final Drawable d = getDrawable(); 
    final int origW = d.getIntrinsicWidth(); 
    final int origH = d.getIntrinsicHeight(); 

    // Calculate the actual dimensions 
    final int actW = Math.round(origW * scaleX); 
    final int actH = Math.round(origH * scaleY); 

    currentWidth = actW; 
    currentHeight = actH; 
} 

public boolean isDrawRect() { 
    return drawRect; 
} 

public void setDrawRect(boolean drawRect) { 
    this.drawRect = drawRect; 
} 

public float getLeftRect() { 
    return left; 
} 

public void setLeftRect(float left) { 
    this.left = left; 
} 

public float getTopRect() { 
    return top; 
} 

public void setTopRect(float top) { 
    this.top = top; 
} 

public float getRightRect() { 
    return right; 
} 

public void setRightRect(float right) { 
    this.right = right; 
} 

public float getBottomRect() { 
    return bottom; 
} 

public void setBottomRect(float bottom) { 
    this.bottom = bottom; 
} 

public float getPixelX() { 
    return pixelX; 
} 

public void setPixelX(float pixelX) { 
    this.pixelX = pixelX; 
} 

public float getPixelY() { 
    return pixelY; 
} 

public void setPixelY(float pixelY) { 
    this.pixelY = pixelY; 
} 

public int getChunksX() { 
    return nChunksX; 
} 

public void setChunksX(int nChunksX) { 
    this.nChunksX = nChunksX; 
} 

public int getChunksY() { 
    return nChunksY; 
} 

public void setChunksY(int nChunksY) {   
    this.nChunksY = nChunksY; 
} 
} 

目前,源图像在XML文件中定义:

<com.jocajica.shakepic.PuzzleView 
    android:id="@+id/imageViewSelected" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:contentDescription="@string/image_selected" 
    android:src="@android:drawable/progress_indeterminate_horizontal" /> 

我需要对图像绘制的网格。

+0

出了什么问题共达/ getLeft/getBottom/GetRight时? – Blackbelt 2013-04-24 13:29:21

+0

@blackbelt我相信这些将返回其容器中ImageView的坐标。 OP想要知道ImageView – 2013-04-24 14:13:00

+0

内实际图像的偏移量。我需要知道当前图像的偏移量,getTop/getLeft/getBottom/getRight仅返回其容器中ImageView的坐标。 – 2013-04-24 14:22:38

回答

8

据雅各Nordfalk的链接,我是能够产生一个静态方法让你获得图像的位置和尺寸来自ImageView。

/** 
* Returns the bitmap position inside an imageView. 
* @param imageView source ImageView 
* @return 0: left, 1: top, 2: width, 3: height 
*/ 
public static int[] getBitmapPositionInsideImageView(ImageView imageView) { 
    int[] ret = new int[4]; 

    if (imageView == null || imageView.getDrawable() == null) 
     return ret; 

    // Get image dimensions 
    // Get image matrix values and place them in an array 
    float[] f = new float[9]; 
    imageView.getImageMatrix().getValues(f); 

    // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY) 
    final float scaleX = f[Matrix.MSCALE_X]; 
    final float scaleY = f[Matrix.MSCALE_Y]; 

    // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight) 
    final Drawable d = imageView.getDrawable(); 
    final int origW = d.getIntrinsicWidth(); 
    final int origH = d.getIntrinsicHeight(); 

    // Calculate the actual dimensions 
    final int actW = Math.round(origW * scaleX); 
    final int actH = Math.round(origH * scaleY); 

    ret[2] = actW; 
    ret[3] = actH; 

    // Get image position 
    // We assume that the image is centered into ImageView 
    int imgViewW = imageView.getWidth(); 
    int imgViewH = imageView.getHeight(); 

    int top = (int) (imgViewH - actH)/2; 
    int left = (int) (imgViewW - actW)/2; 

    ret[0] = left; 
    ret[1] = top; 

    return ret; 
} 
+0

这将返回负值的偏移量。 – 2017-02-08 09:19:47

0

覆盖的onLayout方法

//inside puzzleView 
    float[] matrix = new float[9]; 
    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 

     super.onLayout(changed, left, top, right, bottom); 
     matrix = getMatrix(); 

    }  
    public void getMatrix(){ 
     return matrix; 
    } 


    private float[] getMatrix() { 

     final float[] matrix = new float[9]; 
     getImageMatrix().getValues(matrix); 

     return matrix; 
    } 

,并用它来获取值

 // Extract the scale and translation values from the matrix. 
     float scaleX = matrix[Matrix.MSCALE_X]; 
     float scaleY = matrix[Matrix.MSCALE_Y]; 
     float transX = matrix[Matrix.MTRANS_X]; 
     float transY = matrix[Matrix.MTRANS_Y]; 

我希望这帮助ü

相关问题