下面列出了android.graphics.Matrix#MSCALE_Y 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Rect getDrawableBounds(Drawable d) {
if (d == null) return null;
Rect result = new Rect();
Rect tDrawableRect = d.getBounds();
Matrix drawableMatrix = getImageMatrix();
float[] values = new float[9];
drawableMatrix.getValues(values);
result.left = (int) values[Matrix.MTRANS_X];
result.top = (int) values[Matrix.MTRANS_Y];
result.right = (int) (result.left + tDrawableRect.width() * values[Matrix.MSCALE_X]);
result.bottom = (int) (result.top + tDrawableRect.height() * values[Matrix.MSCALE_Y]);
return result;
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
*/
public Matrix fitScreen() {
mMinScaleX = 1f;
mMinScaleY = 1f;
Matrix save = new Matrix();
save.set(mMatrixTouch);
float[] vals = new float[9];
save.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
save.setValues(vals);
return save;
}
/**
*
* @param imageView
* @param width
* @param height
*/
public static final void updateImageViewMatrix(ImageView imageView, float width, float height) {
Drawable drawable = imageView.getDrawable();
if(drawable == null) {
throw new NullPointerException("ImageView drawable is null");
}
Matrix matrix = imageView.getImageMatrix();
if(!matrix.isIdentity()) {
float[] values = new float[9];
matrix.getValues(values);
RectF src = new RectF();
src.left = 0;
src.top = 0;
src.right = width;
src.bottom = height;
RectF dst = new RectF();
dst.left = values[Matrix.MTRANS_X];
dst.top = values[Matrix.MTRANS_Y];
dst.right = dst.left + (drawable.getIntrinsicWidth() * values[Matrix.MSCALE_X]);
dst.bottom = dst.top + (drawable.getIntrinsicHeight() * values[Matrix.MSCALE_Y]);
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
}
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
*/
public Matrix fitScreen() {
mMinScaleX = 1f;
mMinScaleY = 1f;
Matrix save = new Matrix();
save.set(mMatrixTouch);
float[] vals = new float[9];
save.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
save.setValues(vals);
return save;
}
/**
* 重置Matrix
*/
private void reSetMatrix() {
if(checkRest()){
mCurrentMatrix.set(mMatrix);
setImageMatrix(mCurrentMatrix);
}else {
//判断Y轴是否需要更正
float[] values=new float[9];
getImageMatrix().getValues(values);
float height=mImageHeight*values[Matrix.MSCALE_Y];
if(height<getHeight()){
//在图片真实高度小于容器高度时,Y轴居中,Y轴理想偏移量为两者高度差/2,
float topMargin=(getHeight()-height)/2;
if(topMargin!=values[Matrix.MTRANS_Y]){
mCurrentMatrix.set(getImageMatrix());
mCurrentMatrix.postTranslate(0, topMargin-values[Matrix.MTRANS_Y]);
setImageMatrix(mCurrentMatrix);
}
}
}
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
*/
public Matrix fitScreen() {
mMinScaleX = 1f;
mMinScaleY = 1f;
Matrix save = new Matrix();
save.set(mMatrixTouch);
float[] vals = new float[9];
save.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
save.setValues(vals);
return save;
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds. Output Matrix is available for those who wish to cache the object.
*/
public void fitScreen(Matrix outputMatrix) {
mMinScaleX = 1f;
mMinScaleY = 1f;
outputMatrix.set(mMatrixTouch);
float[] vals = valsBufferForFitScreen;
for (int i = 0; i < 9; i++) {
vals[i] = 0;
}
outputMatrix.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
outputMatrix.setValues(vals);
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds. Output Matrix is available for those who wish to cache the object.
*/
public void fitScreen(Matrix outputMatrix) {
mMinScaleX = 1f;
mMinScaleY = 1f;
outputMatrix.set(mMatrixTouch);
float[] vals = valsBufferForFitScreen;
for (int i = 0; i < 9; i++) {
vals[i] = 0;
}
outputMatrix.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
outputMatrix.setValues(vals);
}
public static float scaleValue(Matrix matrix) {
float[] f = new float[9];
matrix.getValues(f);
float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];
return scaleX;
}
public static float[] LoadGraphicsMatrix(Matrix matrix) {
float m[] = new float[16];
float v[] = new float[9];
matrix.getValues(v);
m[0] = v[Matrix.MSCALE_X]; //m.a;
m[1] = v[Matrix.MSKEW_X]; //m.b;
m[2] = 0.0f;
m[3] = 0.0f;
m[4] = v[Matrix.MSKEW_Y]; //m.c;
m[5] = v[Matrix.MSCALE_Y]; //m.d;
m[6] = 0.0f;
m[7] = 0.0f;
m[8] = 0.0f;
m[9] = 0.0f;
m[10] = 1.0f;
m[11] = 0.0f;
m[12] = v[Matrix.MTRANS_X]; //m.tx;
m[13] = v[Matrix.MTRANS_Y]; //m.ty;
m[14] = 0.0f;
m[15] = 1.0f;
return m;
}
/**
* 初始化模板Matrix和图片的其他数据
*/
private void initData() {
//设置完图片后,获取该图片的坐标变换矩阵
mMatrix.set(getImageMatrix());
float[] values=new float[9];
mMatrix.getValues(values);
//图片宽度为屏幕宽度除缩放倍数
mImageWidth=getWidth()/values[Matrix.MSCALE_X];
mImageHeight=(getHeight()-values[Matrix.MTRANS_Y]*2)/values[Matrix.MSCALE_Y];
mScale=values[Matrix.MSCALE_X];
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
loadMatrixValues();
if(interpolatedTime >= 1) {
mMatrix.getValues(mMatrixValues);
mMatrixValues[Matrix.MSCALE_X] = this.targetScale;
mMatrixValues[Matrix.MSCALE_Y] = this.targetScale;
mMatrixValues[Matrix.MTRANS_X] = this.targetTranslationX;
mMatrixValues[Matrix.MTRANS_Y] = this.targetTranslationY;
mMatrix.setValues(mMatrixValues);
} else {
final float scaleFactor = (this.initialScale + interpolatedTime * (this.targetScale - this.initialScale)) / mScale;
mMatrix.postScale(scaleFactor, scaleFactor);
mMatrix.getValues(mMatrixValues);
final float currentTranslationX = mMatrixValues[Matrix.MTRANS_X];
final float currentTranslationY = mMatrixValues[Matrix.MTRANS_Y];
final float dx = this.initialTranslationX + interpolatedTime * (this.targetTranslationX - this.initialTranslationX) - currentTranslationX;
final float dy = this.initialTranslationY + interpolatedTime * (this.targetTranslationY - this.initialTranslationY) - currentTranslationY;
mMatrix.postTranslate(dx, dy);
}
ViewCompat.postInvalidateOnAnimation(TouchImageView.this);
}
public void syncCharts() {
if (dstCharts == null) {
return;
}
Matrix srcMatrix;
float[] srcVals = new float[9];
Matrix dstMatrix;
float[] dstVals = new float[9];
// get src chart translation matrix:
srcMatrix = srcChart.getViewPortHandler().getMatrixTouch();
srcMatrix.getValues(srcVals);
// apply X axis scaling and position to dst charts:
for (Chart dstChart : dstCharts) {
if (dstChart.getVisibility() == View.VISIBLE) {
dstMatrix = dstChart.getViewPortHandler().getMatrixTouch();
dstMatrix.getValues(dstVals);
dstVals[Matrix.MSCALE_X] = srcVals[Matrix.MSCALE_X];
dstVals[Matrix.MSKEW_X] = srcVals[Matrix.MSKEW_X];
dstVals[Matrix.MTRANS_X] = srcVals[Matrix.MTRANS_X];
dstVals[Matrix.MSKEW_Y] = srcVals[Matrix.MSKEW_Y];
dstVals[Matrix.MSCALE_Y] = srcVals[Matrix.MSCALE_Y];
dstVals[Matrix.MTRANS_Y] = srcVals[Matrix.MTRANS_Y];
dstVals[Matrix.MPERSP_0] = srcVals[Matrix.MPERSP_0];
dstVals[Matrix.MPERSP_1] = srcVals[Matrix.MPERSP_1];
dstVals[Matrix.MPERSP_2] = srcVals[Matrix.MPERSP_2];
dstMatrix.setValues(dstVals);
dstChart.getViewPortHandler().refresh(dstMatrix, dstChart, true);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
getImageMatrix().getValues(f);
scaleX = f[Matrix.MSCALE_X];
scaleY = f[Matrix.MSCALE_Y];
}
/**
* limits the maximum scale and X translation of the given matrix
*
* @param matrix
*/
public void limitTransAndScale(Matrix matrix, RectF content) {
matrix.getValues(matrixBuffer);
float curTransX = matrixBuffer[Matrix.MTRANS_X];
float curScaleX = matrixBuffer[Matrix.MSCALE_X];
float curTransY = matrixBuffer[Matrix.MTRANS_Y];
float curScaleY = matrixBuffer[Matrix.MSCALE_Y];
// min scale-x is 1f
mScaleX = Math.min(Math.max(mMinScaleX, curScaleX), mMaxScaleX);
// min scale-y is 1f
mScaleY = Math.min(Math.max(mMinScaleY, curScaleY), mMaxScaleY);
float width = 0f;
float height = 0f;
if (content != null) {
width = content.width();
height = content.height();
}
float maxTransX = -width * (mScaleX - 1f);
mTransX = Math.min(Math.max(curTransX, maxTransX - mTransOffsetX), mTransOffsetX);
float maxTransY = height * (mScaleY - 1f);
mTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);
matrixBuffer[Matrix.MTRANS_X] = mTransX;
matrixBuffer[Matrix.MSCALE_X] = mScaleX;
matrixBuffer[Matrix.MTRANS_Y] = mTransY;
matrixBuffer[Matrix.MSCALE_Y] = mScaleY;
matrix.setValues(matrixBuffer);
}
@Override
public void setImageBitmap(Bitmap bm) {
// TODO Auto-generated method stub
super.setImageBitmap(bm);
//设置完图片后,获取该图片的坐标变换矩阵
mMatrix.set(getImageMatrix());
float[] values = new float[9];
mMatrix.getValues(values);
//图片宽度为屏幕宽度除缩放倍数
mImageWidth = getWidth() / values[Matrix.MSCALE_X];
mImageHeight = (getHeight() - values[Matrix.MTRANS_Y] * 2) / values[Matrix.MSCALE_Y];
}
@Override
protected void onDraw(Canvas canvas) {
if (bubbleDrawable == null) {
return; // couldn't resolve the URI
}
if (bubbleDrawable.getIntrinsicHeight() == 0 || bubbleDrawable.getIntrinsicWidth() == 0) {
return; // nothing to draw (empty bounds)
}
final Matrix mDrawMatrix = getImageMatrix();
final int mPaddingLeft = getPaddingLeft();
final int mPaddingTop = getPaddingTop();
if (mDrawMatrix == null && getPaddingTop() == 0 && getPaddingLeft() == 0) {
bubbleDrawable.draw(canvas);
} else {
final int saveCount = canvas.getSaveCount();
canvas.save();
//crop to padding api above 16
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (getCropToPadding()) {
final int scrollX = getScrollX();
final int scrollY = getScrollY();
canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop,
scrollX + getRight() - getLeft() - getRight(),
scrollY + getBottom() - getTop() - getBottom());
}
}
canvas.translate(mPaddingLeft, mPaddingTop);
if (mDrawMatrix != null) {
canvas.concat(mDrawMatrix);
mDrawMatrix.getValues(matrixValues);
final float scaleX = matrixValues[Matrix.MSCALE_X];
final float scaleY = matrixValues[Matrix.MSCALE_Y];
final float translateX = matrixValues[Matrix.MTRANS_X];
final float translateY = matrixValues[Matrix.MTRANS_Y];
final ScaleType scaleType = getScaleType();
if (scaleType == ScaleType.CENTER) {
bubbleDrawable.setOffsetLeft(-translateX);
bubbleDrawable.setOffsetTop(-translateY);
bubbleDrawable.setOffsetBottom(-translateY);
bubbleDrawable.setOffsetRight(-translateX);
} else if (scaleType == ScaleType.CENTER_CROP) {
float scale = scaleX > scaleY ? 1 / scaleY : 1 / scaleX;
bubbleDrawable.setOffsetLeft(-translateX * scale);
bubbleDrawable.setOffsetTop(-translateY * scale);
bubbleDrawable.setOffsetBottom(-translateY * scale);
bubbleDrawable.setOffsetRight(-translateX * scale);
bubbleDrawable.setScale(scale);
} else {
bubbleDrawable.setScale(scaleX > scaleY ? 1 / scaleY : 1 / scaleX);
}
}
bubbleDrawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
}
/**
* From http://stackoverflow.com/a/26930938/5334314
* Returns the actual bitmap position in 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;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
return;
}
int drawableWidth = drawable.getIntrinsicWidth();
int drawableHeight = drawable.getIntrinsicHeight();
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
viewHeight = setViewSize(heightMode, heightSize, drawableHeight);
//
// Set view dimensions
//
setMeasuredDimension(viewWidth, viewHeight);
//
// Scale image for view
//
float scaleX = (float) viewWidth / drawableWidth;
float scaleY = (float) viewHeight / drawableHeight;
float scale = Math.min(scaleX, scaleY);
//
// Center the image
//
float redundantYSpace = viewHeight - (scale * drawableHeight);
float redundantXSpace = viewWidth - (scale * drawableWidth);
matchViewWidth = viewWidth - redundantXSpace;
matchViewHeight = viewHeight - redundantYSpace;
if (normalizedScale == 1) {
//
// Stretch and center image to fit view
//
matrix.setScale(scale, scale);
matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
} else {
prevMatrix.getValues(m);
//
// Rescale Matrix after rotation
//
m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale;
m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale;
//
// TransX and TransY from previous matrix
//
float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y];
//
// Width
//
float prevActualWidth = prevMatchViewWidth * normalizedScale;
float actualWidth = getImageWidth();
translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth);
//
// Height
//
float prevActualHeight = prevMatchViewHeight * normalizedScale;
float actualHeight = getImageHeight();
translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight);
//
// Set the matrix to the adjusted scale and translate values.
//
matrix.setValues(m);
}
fixTrans();
setImageMatrix(matrix);
}
/**
* Returns the bitmap position inside an imageView.
*
* @param imageView source ImageView
*
* @return 0: left, 1: top, 2: width, 3: height
*/
public static int[] getDisplayedImageLocation(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[] imgViewScreenLoc = new int[2];
imageView.getLocationOnScreen(imgViewScreenLoc);
// get the actual image location inside its image view
int left = imgViewScreenLoc[0] + (imgViewW - actW) / 2;
int top = imgViewScreenLoc[1] + (imgViewH - actH) / 2;
ret[0] = left;
ret[1] = top;
return ret;
}