我正在尝试为Android平台制作媒体播放器,而我正在尝试添加的功能之一是能够拖动和捏缩放图片。
我遇到的问题是我从“Hello,Android”编辑中复制了此代码。 3:
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
这段代码完全符合我的要求,但我需要纯X,Y坐标和图片的宽度/高度而不是矩阵。
有谁知道如何将矩阵更改为X,Y坐标以及宽度和高度?
答案 0 :(得分:42)
原来解决方案很简单:
float[] values = new float[9];
matrix.getValues(values);
globalX = values[Matrix.MTRANS_X];
globalY = values[Matrix.MTRANS_Y];
width = values[Matrix.MSCALE_X]*imageWidth;
height = values[Matrix.MSCALE_Y]*imageHeight;
答案 1 :(得分:5)
float height = matrixValues[4]*((ImageView)currentView).getDrawable().getIntrinsicHeight();
获得正确的身高。
答案 2 :(得分:3)
我写了一些方法可以帮助一些用户(非常感谢上面的代码!!!):
private void logImageViewMatrixInfos(Matrix matrix, ImageView imageView) {
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
float globalY = values[5];
float width = values[0]* imageView.getWidth();
float height = values[4] * imageView.getHeight();
Log.i("Log value", "Image Details: xPos: " + globalX + " yPos: " + globalY + "\nwidth: " + width + " height: " + height);
}
private float getXValueFromMatrix(Matrix matrix) {
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
return globalX;
}
private float getYValueFromMatrix(Matrix matrix) {
float[] values = new float[9];
matrix.getValues(values);
float globalY = values[5];
return globalY;
}
private float getWidthFromMatrix(Matrix matrix, ImageView imageview) {
float[] values = new float[9];
matrix.getValues(values);
float width = values[0]* imageview.getWidth();
return width;
}
private float getHeightFromMatrix(Matrix matrix, ImageView imageview) {
float[] values = new float[9];
matrix.getValues(values);
float height = values[4] * imageview.getHeight();
return height;
}
<强>:=)强>