下面列出了android.graphics.Matrix#postScale ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Tile getTile(int x, int y, int zoom) {
Matrix matrix = new Matrix();
float scale = (float) Math.pow(2, zoom) * mScale;
matrix.postScale(scale, scale);
matrix.postTranslate(-x * mDimension, -y * mDimension);
Bitmap bitmap = Bitmap.createBitmap(mDimension, mDimension, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
c.setMatrix(matrix);
for (Point p : mPoints) {
c.drawCircle((float) p.x, (float) p.y, 1, new Paint());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return new Tile(mDimension, mDimension, baos.toByteArray());
}
public static Bitmap compressBmpWithScale(Bitmap bm) {
if (bm == null) return null;
int bitmapWidth = bm.getWidth();
int bitmapHeight = bm.getHeight();
boolean smallerThenScreen = true;
float ratio = bitmapWidth / bitmapHeight;
int width = Math.min(getScreenWidthPix(), s_defaultPicWidth);
int height = Math.min(getScreenHeightPix(), s_defaultPicHeight);
float scaleWidth;
float scaleHeight;
if (bitmapWidth > width) {
scaleWidth = (float) width / bitmapWidth;
scaleHeight = width * bitmapHeight / bitmapWidth;
} else {
scaleHeight = (float) height / bitmapHeight;
scaleWidth = bitmapWidth * height / bitmapHeight;
}
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 产生缩放后的Bitmap对象
return Bitmap.createBitmap(bm, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
}
/**
* 根据压缩图片并且适应view
*
* @param bitmap
* 压缩图片
* @param contentView
* 适应的view
* @return 压缩后的图片
*/
public Bitmap compressionFiller(Bitmap bitmap, View contentView)
{
int layoutHeight = contentView.getHeight();
float scale = 0f;
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();
scale = bitmapHeight > bitmapWidth
? layoutHeight / (bitmapHeight * 1f)
: screenWidth / (bitmapWidth * 1f);
Bitmap resizeBmp;
if (scale != 0)
{
int bitmapheight = bitmap.getHeight();
int bitmapwidth = bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 长和宽放大缩小的比例
resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapwidth,
bitmapheight, matrix, true);
} else
{
resizeBmp = bitmap;
}
return resizeBmp;
}
public void setBackgroundBitmap(Bitmap bitmap) {
mPoints = new ArrayList<Point>();
if (bitmap != null) {
Matrix matrix = new Matrix();
float scaleW = getWidth() / bitmap.getWidth();
float scaleH = getHeight() / bitmap.getHeight();
float scale = scaleW > scaleH ? scaleH : scaleW;
if (scale <= 0) {
return;
}
matrix.postScale(scale, scale); //长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
mBitmap = resizeBmp;
invalidate();
} else {
mBitmap = null;
}
}
protected void zoomOut(float rate) {
if (mBitmap == null) {
return;
}
float width = getWidth();
float height = getHeight();
Matrix tmp = new Matrix(mSuppMatrix);
tmp.postScale(1F / sScaleRate, 1F / sScaleRate, width / 2F, height / 2F);
if (getScale(tmp) < 1F) {
mSuppMatrix.setScale(1F, 1F, width / 2F, height / 2F);
} else {
mSuppMatrix.postScale(1F / rate, 1F / rate, width / 2F, height / 2F);
}
setImageMatrix(getImageViewMatrix());
center(true, true, false);
}
public static Bitmap resizeBitmap(Bitmap bm, int maxWidth) {
Bitmap returnBm;
int w = bm.getWidth();
int h = bm.getHeight();
float scaleWidth;
float scaleHeight;
if (w > h) {
scaleWidth = ((float) maxWidth) / w;
scaleHeight = scaleWidth;
} else {
scaleHeight = ((float) maxWidth) / h;
scaleWidth = scaleHeight;
}
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
returnBm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
return returnBm;
}
@Override
public void drawIcon(Canvas canvas, float fraction, Drawable drawable, int colorOfIcon,
WidthAndHeightOfView widthAndHeightOfView) {
int centerX = widthAndHeightOfView.getWidth() / 2;
int centerY = widthAndHeightOfView.getHeight() / 2 - 150;
canvas.save();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float newFraction = (fraction - 0.65f) / 0.35f;
paint.setColor(Color.parseColor("#e53935"));
canvas.drawCircle(centerX, centerY - 50, 100 * (1 - newFraction), paint);
paint.setColor(Color.parseColor("#fdd835"));
canvas.drawCircle(centerX -35, centerY + 35,100 * (1 - newFraction), paint);
paint.setColor(Color.parseColor("#1e88e5"));
canvas.drawCircle(centerX + 35, centerY + 35, 100 * (1 - newFraction), paint);
canvas.restore();
canvas.save();
Path path = new Path();
Bitmap bitmap = drawableToBitmap(drawable);
Matrix matrix = new Matrix();
matrix.postScale(1.7f, 1.7f, centerX, centerY);
canvas.concat(matrix);
path.addCircle(centerX, centerY, bitmap.getHeight() * 1.5f * newFraction, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(bitmap, centerX - bitmap.getWidth() / 2, centerY - bitmap.getHeight() / 2, paint);
canvas.restore();
}
protected void zoomOut(float rate) {
if (mBitmapDisplayed.getBitmap() == null) {
return;
}
float cx = getWidth() / 2F;
float cy = getHeight() / 2F;
// Zoom out to at most 1x.
Matrix tmp = new Matrix(mSuppMatrix);
tmp.postScale(1F / rate, 1F / rate, cx, cy);
if (getScale(tmp) < 1F) {
mSuppMatrix.setScale(1F, 1F, cx, cy);
} else {
mSuppMatrix.postScale(1F / rate, 1F / rate, cx, cy);
}
setImageMatrix(getImageViewMatrix());
center(true, true);
}
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `mTextureView` is fixed.
*
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
@DebugLog
private void configureTransform(final int viewWidth, final int viewHeight) {
final Activity activity = getActivity();
if (textureView == null || previewSize == null || activity == null) {
return;
}
final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
final Matrix matrix = new Matrix();
final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
final float centerX = viewRect.centerX();
final float centerY = viewRect.centerY();
if(rotation == Surface.ROTATION_90) {
//Log.d(TAG, "Rotation is Surface.ROTATION_90");
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
final float scale =
Math.max(
(float) viewHeight / previewSize.getHeight(),
(float) viewWidth / previewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(-90, centerX, centerY);
} /*else if(rotation == Surface.ROTATION_180) {
Log.d(TAG, "Rotation is Surface.ROTATION_180");
matrix.postRotate(180, centerX, centerY);
} else if(rotation == Surface.ROTATION_270) {
Log.d(TAG, "Rotation is Surface.ROTATION_270");
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
final float scale =
Math.max(
(float) viewHeight / previewSize.getHeight(),
(float) viewWidth / previewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
}*/
textureView.setTransform(matrix);
}
/**
* 改变bitmap的大小
* @param bitmap 目标bitmap
* @param newW 目标宽度
* @param newH 目标高度
* @return
*/
public static Bitmap changeBitmapSize(Bitmap bitmap, int newW, int newH) {
int oldW = bitmap.getWidth();
int oldH = bitmap.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newW) / oldW;
float scaleHeight = ((float) newH) / oldH;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
bitmap = Bitmap.createBitmap(bitmap, 0, 0, oldW, oldH, matrix, true);
return bitmap;
}
private static Bitmap a(Bitmap bitmap, int i) {
Matrix matrix = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width <= height) {
width = height;
}
float f = ((float) i) / ((float) width);
matrix.postScale(f, f);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
* 缩放drawable
*/
public static BitmapDrawable scaleDrawable(BitmapDrawable drawable, int w, int h) {
Bitmap oldBitmap = drawable.getBitmap();
int width = oldBitmap.getWidth();
int height = oldBitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, width, height, matrix, true);
return new BitmapDrawable(null, newBitmap);
}
private Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
if (bm==null) return null;
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
mIsInteracting = true;
Matrix transformationMatrix = new Matrix();
float focusX = scaleGestureDetector.getFocusX();
float focusY = scaleGestureDetector.getFocusY();
float scaleX = ScaleGestureDetectorCompat.getCurrentSpanX(scaleGestureDetector) / ScaleGestureDetectorCompat.getPreviousSpanX(scaleGestureDetector);
float scaleY = ScaleGestureDetectorCompat.getCurrentSpanY(scaleGestureDetector) / ScaleGestureDetectorCompat.getPreviousSpanY(scaleGestureDetector);
//Zoom focus is where the fingers are centered,
transformationMatrix.postTranslate(-focusX, -focusY);
transformationMatrix.postScale(scaleX, scaleY);
float focusShiftX = focusX - mLastFocusX;
float focusShiftY = focusY - mLastFocusY;
transformationMatrix.postTranslate(focusX + focusShiftX, focusY + focusShiftY);
mDrawMatrix.postConcat(transformationMatrix);
constrainView();
recalculateXCoordinates(mGraphWidth * mDrawMatrixValues[0]);
if(calculateLegendBounds())
Utils.calculateLegendInformation(mSeries.get(0).getSeries(), 0, mGraphWidth * mDrawMatrixValues[0], mLegendPaint);
mLastFocusX = focusX;
mLastFocusY = focusY;
if(mFocusedPoint != null) {
calculateValueTextHeight();
}
invalidateGlobal();
return true;
}
private static Bitmap formatBlurBitmap(Activity activity, boolean isMatrix) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
int statusBarHeight = 0;
// If need cut statusBar on sdk < KITKAT
if (NEED_CUT) {
Rect frame = new Rect();
view.getWindowVisibleDisplayFrame(frame);
statusBarHeight = frame.top;
}
if (isMatrix) {
// Compress
Matrix matrix = new Matrix();
matrix.postScale(1.0f / SCALE_FACTOR, 1.0f / SCALE_FACTOR);
// New Compress bitmap
bitmap = Bitmap.createBitmap(bitmap, 0, statusBarHeight,
bitmap.getWidth(), bitmap.getHeight() - statusBarHeight, matrix, true);
} else {
bitmap = Bitmap.createBitmap(bitmap, 0, statusBarHeight, bitmap.getWidth(),
bitmap.getHeight() - statusBarHeight);
}
view.destroyDrawingCache();
return bitmap;
}
/**
* 缩放Bitmap
*/
public static Bitmap scaleBitmap(Bitmap target, int w, int h) {
int width = target.getWidth();
int height = target.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(((float) w / width), ((float) h / height));
return Bitmap.createBitmap(target, 0, 0, width, height, matrix, true);
}
private Bitmap createClippedBitmap() {
//if (mSampleSize <= 1) {
// TODO has problem, this method is not useful on some picture
// return mClipImageView.clip();
//}
final float[] matrixValues = mClipImageView.getClipMatrixValues();
final float scale = matrixValues[Matrix.MSCALE_X];
final float transX = matrixValues[Matrix.MTRANS_X];
final float transY = matrixValues[Matrix.MTRANS_Y];
final Rect border = mClipImageView.getClipBorder();
final float cropX = ((-transX + border.left) / scale) * mSampleSize;
final float cropY = ((-transY + border.top) / scale) * mSampleSize;
final float cropWidth = (border.width() / scale) * mSampleSize;
final float cropHeight = (border.height() / scale) * mSampleSize;
final RectF srcRect = new RectF(cropX, cropY, cropX + cropWidth, cropY + cropHeight);
final Rect clipRect = getRealRect(srcRect);
final BitmapFactory.Options ops = new BitmapFactory.Options();
final Matrix outputMatrix = new Matrix();
outputMatrix.setRotate(mDegree);
if (mMaxWidth > 0 && cropWidth > mMaxWidth) {
ops.inSampleSize = findBestSample((int) cropWidth, mMaxWidth);
final float outputScale = mMaxWidth / (cropWidth / ops.inSampleSize);
outputMatrix.postScale(outputScale, outputScale);
}
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(mInput, false);
final Bitmap source = decoder.decodeRegion(clipRect, ops);
recycleImageViewBitmap();
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
outputMatrix, false);
} catch (Exception e) {
return mClipImageView.clip();
} finally {
if (decoder != null && !decoder.isRecycled()) {
decoder.recycle();
}
}
}
/**
* 双击后放大或者缩小
* <p>
* 将图片缩放比例缩放到nextScale指定的值.
* 但nextScale值不能大于最大缩放值不能小于fit center情况下的缩放值.
* 将双击的点尽量移动到控件中心.
*
* @param x 双击的点
* @param y 双击的点
* @see #calculateNextScale(float, float)
* @see #getMaxScale()
*/
private void doubleTap(float x, float y) {
if (!isReady()) {
return;
}
//获取第一层变换矩阵
Matrix innerMatrix = MathUtils.matrixTake();
getInnerMatrix(innerMatrix);
//当前总的缩放比例
float innerScale = MathUtils.getMatrixScale(innerMatrix)[0];
float outerScale = MathUtils.getMatrixScale(mOuterMatrix)[0];
float currentScale = innerScale * outerScale;
//控件大小
float displayWidth = getWidth();
float displayHeight = getHeight();
//最大放大大小
float maxScale = getMaxScale();
//接下来要放大的大小
float nextScale = calculateNextScale(innerScale, outerScale);
//如果接下来放大大于最大值或者小于fit center值,则取边界
if (nextScale > maxScale) {
nextScale = maxScale;
}
if (nextScale < innerScale) {
nextScale = innerScale;
}
//开始计算缩放动画的结果矩阵
Matrix animEnd = MathUtils.matrixTake(mOuterMatrix);
//计算还需缩放的倍数
animEnd.postScale(nextScale / currentScale, nextScale / currentScale, x, y);
//将放大点移动到控件中心
animEnd.postTranslate(displayWidth / 2f - x, displayHeight / 2f - y);
//得到放大之后的图片方框
Matrix testMatrix = MathUtils.matrixTake(innerMatrix);
testMatrix.postConcat(animEnd);
RectF testBound = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
testMatrix.mapRect(testBound);
//修正位置
float postX = 0;
float postY = 0;
if (testBound.right - testBound.left < displayWidth) {
postX = displayWidth / 2f - (testBound.right + testBound.left) / 2f;
} else if (testBound.left > 0) {
postX = -testBound.left;
} else if (testBound.right < displayWidth) {
postX = displayWidth - testBound.right;
}
if (testBound.bottom - testBound.top < displayHeight) {
postY = displayHeight / 2f - (testBound.bottom + testBound.top) / 2f;
} else if (testBound.top > 0) {
postY = -testBound.top;
} else if (testBound.bottom < displayHeight) {
postY = displayHeight - testBound.bottom;
}
//应用修正位置
animEnd.postTranslate(postX, postY);
//清理当前可能正在执行的动画
cancelAllAnimator();
//启动矩阵动画
mScaleAnimator = new ScaleAnimator(mOuterMatrix, animEnd);
mScaleAnimator.start();
//清理临时变量
MathUtils.rectFGiven(testBound);
MathUtils.matrixGiven(testMatrix);
MathUtils.matrixGiven(animEnd);
MathUtils.matrixGiven(innerMatrix);
}
/**
* 双击后放大或者缩小
*
* 将图片缩放比例缩放到nextScale指定的值.
* 但nextScale值不能大于最大缩放值不能小于fit center情况下的缩放值.
* 将双击的点尽量移动到控件中心.
*
* @param x 双击的点
* @param y 双击的点
*
* @see #calculateNextScale(float, float)
* @see #getMaxScale()
*/
private void doubleTap(float x, float y) {
if (!isReady()) {
return;
}
//获取第一层变换矩阵
Matrix innerMatrix = MathUtils.matrixTake();
getInnerMatrix(innerMatrix);
//当前总的缩放比例
float innerScale = MathUtils.getMatrixScale(innerMatrix)[0];
float outerScale = MathUtils.getMatrixScale(mOuterMatrix)[0];
float currentScale = innerScale * outerScale;
//控件大小
float displayWidth = getWidth();
float displayHeight = getHeight();
//最大放大大小
float maxScale = getMaxScale();
//接下来要放大的大小
float nextScale = calculateNextScale(innerScale, outerScale);
//如果接下来放大大于最大值或者小于fit center值,则取边界
if (nextScale > maxScale) {
nextScale = maxScale;
}
if (nextScale < innerScale) {
nextScale = innerScale;
}
//开始计算缩放动画的结果矩阵
Matrix animEnd = MathUtils.matrixTake(mOuterMatrix);
//计算还需缩放的倍数
animEnd.postScale(nextScale / currentScale, nextScale / currentScale, x, y);
//将放大点移动到控件中心
animEnd.postTranslate(displayWidth / 2f - x, displayHeight / 2f - y);
//得到放大之后的图片方框
Matrix testMatrix = MathUtils.matrixTake(innerMatrix);
testMatrix.postConcat(animEnd);
RectF testBound = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
testMatrix.mapRect(testBound);
//修正位置
float postX = 0;
float postY = 0;
if (testBound.right - testBound.left < displayWidth) {
postX = displayWidth / 2f - (testBound.right + testBound.left) / 2f;
} else if (testBound.left > 0) {
postX = -testBound.left;
} else if (testBound.right < displayWidth) {
postX = displayWidth - testBound.right;
}
if (testBound.bottom - testBound.top < displayHeight) {
postY = displayHeight / 2f - (testBound.bottom + testBound.top) / 2f;
} else if (testBound.top > 0) {
postY = -testBound.top;
} else if (testBound.bottom < displayHeight) {
postY = displayHeight - testBound.bottom;
}
//应用修正位置
animEnd.postTranslate(postX, postY);
//清理当前可能正在执行的动画
cancelAllAnimator();
//启动矩阵动画
mScaleAnimator = new ScaleAnimator(mOuterMatrix, animEnd);
mScaleAnimator.start();
//清理临时变量
MathUtils.rectFGiven(testBound);
MathUtils.matrixGiven(testMatrix);
MathUtils.matrixGiven(animEnd);
MathUtils.matrixGiven(innerMatrix);
}
/**
* Post-scales by the specified scale factors.
*
* @param scaleX
* @param scaleY
* @return
*/
public Matrix zoom(float scaleX, float scaleY) {
Matrix save = new Matrix();
save.set(mMatrixTouch);
save.postScale(scaleX, scaleY);
return save;
}