下面列出了android.graphics.Matrix#reset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void getProperBaseMatrix(RotateBitmap bitmap, Matrix matrix) {
float viewWidth = getWidth();
float viewHeight = getHeight();
float w = bitmap.getWidth();
float h = bitmap.getHeight();
int rotation = bitmap.getRotation();
matrix.reset();
// We limit up-scaling to 2x otherwise the result may look bad if it's
// a small icon.
float widthScale = Math.min(viewWidth / w, 2.0f);
float heightScale = Math.min(viewHeight / h, 2.0f);
float scale = Math.min(widthScale, heightScale);
matrix.postConcat(bitmap.getRotateMatrix());
matrix.postScale(scale, scale);
matrix.postTranslate(
(viewWidth - w * scale) / 2F,
(viewHeight - h * scale) / 2F);
}
/**
* Function to draw image in the center of arc
*
* @param canvas Canvas to draw
* @param tempAngle Temporary angle
* @param bitmap Bitmap to draw
*/
private void drawImage(Canvas canvas, float tempAngle, Bitmap bitmap) {
//get every arc img width and angle
int imgWidth = (radius / mWheelItems.size()) - mImagePadding;
float angle = (float) ((tempAngle + 360 / mWheelItems.size() / 2) * Math.PI / 180);
//calculate x and y
int x = (int) (center + radius / 2 / 2 * Math.cos(angle));
int y = (int) (center + radius / 2 / 2 * Math.sin(angle));
//create arc to draw
Rect rect = new Rect(x - imgWidth / 2, y - imgWidth / 2, x + imgWidth / 2, y + imgWidth / 2);
//rotate main bitmap
float px = rect.exactCenterX();
float py = rect.exactCenterY();
Matrix matrix = new Matrix();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2);
matrix.postRotate(tempAngle + 120);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG ));
Log.d("sadsdsddssd" , bitmap.getWidth() + " : "+bitmap.getHeight());
matrix.reset();
}
private void computePath(Rect bounds) {
final float currentScale = mCurrentScale;
final Path path = mPath;
final RectF rect = mRect;
final Matrix matrix = mMatrix;
path.reset();
int totalSize = Math.min(bounds.width(), bounds.height());
float initial = mClosedStateSize;
float destination = totalSize;
float currentSize = initial + (destination - initial) * currentScale;
float halfSize = currentSize / 2f;
float inverseScale = 1f - currentScale;
float cornerSize = halfSize * inverseScale;
float[] corners = new float[]{halfSize, halfSize, halfSize, halfSize, halfSize, halfSize, cornerSize, cornerSize};
rect.set(bounds.left, bounds.top, bounds.left + currentSize, bounds.top + currentSize);
path.addRoundRect(rect, corners, Path.Direction.CCW);
matrix.reset();
matrix.postRotate(-45, bounds.left + halfSize, bounds.top + halfSize);
matrix.postTranslate((bounds.width() - currentSize) / 2, 0);
float hDiff = (bounds.bottom - currentSize - mExternalOffset) * inverseScale;
matrix.postTranslate(0, hDiff);
path.transform(matrix);
}
private void drawSky(Canvas canvas) {
Matrix matrix = mMatrix;
matrix.reset();
float dragPercent = Math.min(1f, Math.abs(mPercent));
float skyScale;
float scalePercentDelta = dragPercent - SCALE_START_PERCENT;
if (scalePercentDelta > 0) {
/** Change skyScale between {@link #SKY_INITIAL_SCALE} and 1.0f depending on {@link #mPercent} */
float scalePercent = scalePercentDelta / (1.0f - SCALE_START_PERCENT);
skyScale = SKY_INITIAL_SCALE - (SKY_INITIAL_SCALE - 1.0f) * scalePercent;
} else {
skyScale = SKY_INITIAL_SCALE;
}
float offsetX = -(mScreenWidth * skyScale - mScreenWidth) / 2.0f;
float offsetY = (1.0f - dragPercent) * mParent.getTotalDragDistance() - mSkyTopOffset
// Offset canvas moving
- mSkyHeight * (skyScale - 1.0f) / 2 // Offset sky scaling
+ mSkyMoveOffset * dragPercent; // Give it a little move top -> bottom
matrix.postScale(skyScale, skyScale);
matrix.postTranslate(offsetX, offsetY);
canvas.drawBitmap(mSky, matrix, null);
}
/**
* 计算两个矩形之间的变换矩阵
*
* unknownMatrix.mapRect(to, from)
* 已知from矩形和to矩形,求unknownMatrix
*
* @param from
* @param to
* @param result unknownMatrix
*/
public static void calculateRectTranslateMatrix(RectF from, RectF to, Matrix result) {
if (from == null || to == null || result == null) {
return;
}
if (from.width() == 0 || from.height() == 0) {
return;
}
result.reset();
result.postTranslate(-from.left, -from.top);
result.postScale(to.width() / from.width(), to.height() / from.height());
result.postTranslate(to.left, to.top);
}
private void drawJet(Canvas canvas) {
Matrix matrix = mMatrix;
matrix.reset();
float dragPercent = mPercent;
float rotateAngle = 0;
// Check overdrag
if (dragPercent > 1.0f && !mEndOfRefreshing) {
rotateAngle = (dragPercent % 1) * 10;
dragPercent = 1.0f;
}
float offsetX = ((mScreenWidth * dragPercent) / 2) - mJetWidthCenter;
float offsetY = mJetTopOffset
+ (mParent.getTotalDragDistance() / 2)
* (1.0f - dragPercent)
- mJetHeightCenter;
if (isRefreshing) {
if (checkCurrentAnimationPart(AnimationPart.FIRST)) {
offsetY -= getAnimationPartValue(AnimationPart.FIRST);
} else if (checkCurrentAnimationPart(AnimationPart.SECOND)) {
offsetY -= getAnimationPartValue(AnimationPart.SECOND);
} else if (checkCurrentAnimationPart(AnimationPart.THIRD)) {
offsetY += getAnimationPartValue(AnimationPart.THIRD);
} else if (checkCurrentAnimationPart(AnimationPart.FOURTH)) {
offsetY += getAnimationPartValue(AnimationPart.FOURTH);
}
}
matrix.setTranslate(offsetX, offsetY);
if (dragPercent == 1.0f) {
matrix.preRotate(rotateAngle, mJetWidthCenter, mJetHeightCenter);
}
canvas.drawBitmap(mJet, matrix, null);
}
public Bitmap getBitMap(byte[] data, Camera camera, boolean mIsFrontalCamera) {
int width = camera.getParameters().getPreviewSize().width;
int height = camera.getParameters().getPreviewSize().height;
YuvImage yuvImage = new YuvImage(data, camera.getParameters()
.getPreviewFormat(), width, height, null);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 80,
byteArrayOutputStream);
byte[] jpegData = byteArrayOutputStream.toByteArray();
// 获取照相后的bitmap
Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegData, 0,
jpegData.length);
Matrix matrix = new Matrix();
matrix.reset();
if (mIsFrontalCamera) {
matrix.setRotate(-90);
} else {
matrix.setRotate(90);
}
tmpBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(),
tmpBitmap.getHeight(), matrix, true);
tmpBitmap = tmpBitmap.copy(Bitmap.Config.ARGB_8888, true);
int hight = tmpBitmap.getHeight() > tmpBitmap.getWidth() ? tmpBitmap
.getHeight() : tmpBitmap.getWidth();
float scale = hight / 800.0f;
if (scale > 1) {
tmpBitmap = Bitmap.createScaledBitmap(tmpBitmap,
(int) (tmpBitmap.getWidth() / scale),
(int) (tmpBitmap.getHeight() / scale), false);
}
return tmpBitmap;
}
protected void getParentTransform(Matrix transform) {
if (mTransformCallback != null) {
mTransformCallback.getTransform(transform);
} else {
transform.reset();
}
}
protected RectF calculateEnclosingRect() {
if (0 == texts.size()) {
return null;
}
TextProperties tp = texts.get(texts.size()-1);
Matrix mat = new Matrix();
RectF globalRect = new RectF();
float top = tp.textPosition.y;
for(int i=0; i<tp.textLines.length; i++) {
int h = tp.textRects.get(i).height();
RectF rect = new RectF(0, 0, tp.textRects.get(i).width(), h);
rect.offset(imageRect.left, imageRect.top);
mat.reset();
mat.preRotate(-tp.rotation, tp.rotationCenter.x, tp.rotationCenter.y);
mat.preTranslate(tp.textPosition.x, top);
mat.mapRect(rect);
if (0 == i) {
globalRect.set(rect);
} else {
globalRect.top = Math.min(globalRect.top, rect.top);
globalRect.left = Math.min(globalRect.left, rect.left);
globalRect.bottom = Math.max(globalRect.bottom, rect.bottom);
globalRect.right = Math.max(globalRect.right, rect.right);
}
top += h + interline*tp.scale;
}
return globalRect;
}
/**
* 计算两个矩形之间的变换矩阵
* <p>
* unknownMatrix.mapRect(to, from)
* 已知from矩形和to矩形,求unknownMatrix
*
* @param from
* @param to
* @param result unknownMatrix
*/
public static void calculateRectTranslateMatrix(RectF from, RectF to, Matrix result) {
if (from == null || to == null || result == null) {
return;
}
if (from.width() == 0 || from.height() == 0) {
return;
}
result.reset();
result.postTranslate(-from.left, -from.top);
result.postScale(to.width() / from.width(), to.height() / from.height());
result.postTranslate(to.left, to.top);
}
private void init() {
//should use matrix scale type.
setScaleType(ScaleType.MATRIX);
Matrix mat = getImageMatrix();
mat.reset();
setImageMatrix(mat);
}
public void zoomIn(float x, float y, Matrix outputMatrix) {
outputMatrix.reset();
outputMatrix.set(mMatrixTouch);
outputMatrix.postScale(1.4f, 1.4f, x, y);
}
public void zoom(float scaleX, float scaleY, float x, float y, Matrix outputMatrix) {
outputMatrix.reset();
outputMatrix.set(mMatrixTouch);
outputMatrix.postScale(scaleX, scaleY, x, y);
}
public void zoom(float scaleX, float scaleY, float x, float y, Matrix outputMatrix) {
outputMatrix.reset();
outputMatrix.set(mMatrixTouch);
outputMatrix.postScale(scaleX, scaleY, x, y);
}
public Bitmap getBitMapWithRect(byte[] data, Camera camera, boolean mIsFrontalCamera,Rect rect) {
int width = camera.getParameters().getPreviewSize().width;
int height = camera.getParameters().getPreviewSize().height;
YuvImage yuvImage = new YuvImage(data, camera.getParameters()
.getPreviewFormat(), width, height, null);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 80,
byteArrayOutputStream);
byte[] jpegData = byteArrayOutputStream.toByteArray();
// 获取照相后的bitmap
Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegData, 0,
jpegData.length);
// Log.e("xie", "getbitmap width"+tmpBitmap.getWidth()+"rect="+rect );
if (rect.top<0){
rect.top=0;
}
if (rect.top>tmpBitmap.getHeight()){
rect.top=tmpBitmap.getHeight();
}
if (rect.left<0){
rect.left=0;
}
if (rect.left>tmpBitmap.getWidth()){
rect.left=tmpBitmap.getWidth();
}
int widthRect=rect.right-rect.left;
if(rect.right>tmpBitmap.getWidth()){
widthRect=tmpBitmap.getWidth()-rect.left;
}
int heightRect=rect.bottom-rect.top;
if(rect.bottom>tmpBitmap.getHeight()){
heightRect=tmpBitmap.getHeight()-rect.top;
}
// Log.i("xie","xie rect"+rect+"wid"+widthRect+"height"+heightRect);
tmpBitmap = Bitmap.createBitmap(tmpBitmap, rect.left, rect.top, widthRect,
heightRect);
Matrix matrix = new Matrix();
matrix.reset();
if (mIsFrontalCamera) {
matrix.setRotate(-90);
} else {
matrix.setRotate(90);
}
tmpBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(),
tmpBitmap.getHeight(), matrix, true);
// Log.e("xie", "getbitmap temp"+tmpBitmap.getWidth()+"asdhe "+tmpBitmap.getHeight() );
tmpBitmap = tmpBitmap.copy(Bitmap.Config.ARGB_8888, true);
int hight = tmpBitmap.getHeight() > tmpBitmap.getWidth() ? tmpBitmap
.getHeight() : tmpBitmap.getWidth();
float scale = hight / 800.0f;
if (scale > 1) {
tmpBitmap = Bitmap.createScaledBitmap(tmpBitmap,
(int) (tmpBitmap.getWidth() / scale),
(int) (tmpBitmap.getHeight() / scale), false);
}
return tmpBitmap;
}
public void zoomOut(float x, float y, Matrix outputMatrix) {
outputMatrix.reset();
outputMatrix.set(mMatrixTouch);
outputMatrix.postScale(0.7f, 0.7f, x, y);
}
public static boolean setBitmapToDisplayMatrix(Matrix m, RectF imageBounds,
RectF displayBounds) {
m.reset();
return m.setRectToRect(imageBounds, displayBounds, Matrix.ScaleToFit.CENTER);
}
@Override
protected Matrix resetInstance(Matrix obj) {
obj.reset();
return obj;
}
protected void drawText(float radius, float size, GeoPoint pt, GISDisplay display) {
if (TextUtils.isEmpty(mText))
return;
Paint textPaint = new Paint();
textPaint.setColor(mTextColor);
textPaint.setAntiAlias(true);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setStrokeCap(Paint.Cap.ROUND);
float gap = (float) (1 / display.getScale());
float innerRadius = radius - gap;
float textSize = size * innerRadius; // initial text size
Rect textRect = new Rect();
textPaint.setTextSize(size);
textPaint.getTextBounds(mText, 0, mText.length(), textRect);
textPaint.setTextSize(textSize);
float halfW = textRect.width() * innerRadius / 2;
float halfH = textRect.height() * innerRadius / 2;
// float outerTextRadius = (float) Math.sqrt(halfH * halfH + halfW * halfW);
// float textScale = innerRadius / outerTextRadius;
float textX = (float) (pt.getX() - halfW - radius / 2);
float textY = (float) (pt.getY() + halfH - radius / 2);
switch (mTextAlignment) {
case ALIGN_TOP:
textY = textY - halfH * 2;
break;
case ALIGN_TOP_RIGHT:
textX = textX + halfW * 2 - halfW * .5f;
textY = textY - halfH * 2;
break;
case ALIGN_RIGHT:
textX = textX + halfW * 2 - halfW * 0.25f;
break;
case ALIGN_BOTTOM_RIGHT:
textX = textX + halfW * 2 - halfW * .5f;
textY = textY + halfH * 2;
break;
case ALIGN_BOTTOM:
textY = textY + halfH * 2;
break;
case ALIGN_BOTTOM_LEFT:
textX = textX - halfW * 2 + halfW * .5f;
textY = textY + halfH * 2;
break;
case ALIGN_LEFT:
textX = textX - halfW * 2 + halfW * 0.25f;
break;
case ALIGN_TOP_LEFT:
textX = textX - halfW * 2 + halfW * .5f;
textY = textY - halfH * 2;
break;
}
Path textPath = new Path();
textPaint.getTextPath(mText, 0, mText.length(), textX, textY, textPath);
textPath.close();
Matrix matrix = new Matrix();
matrix.reset();
matrix.setScale(1, -1, (float) pt.getX(), (float) pt.getY());
textPath.transform(matrix);
display.drawPath(textPath, textPaint);
}
public void drawTextOnPath(
String text,
Path path,
float hOffset,
float vOffset,
Paint paint)
{
if (null == mMainCanvas) {
return;
}
PathMeasure pm = new PathMeasure(path, false);
Matrix matrix = new Matrix();
Path charPath = new Path();
Path textPath = new Path();
float pathLength = pm.getLength();
float coordinates[] = new float[2];
float tangent[] = new float[2];
int i = 0;
float position = hOffset;
while (i < text.length()) {
String ch = text.substring(i, i + 1);
float charWidth = paint.measureText(ch);
float nextPosition = position + charWidth;
if (nextPosition > pathLength) {
break;
}
pm.getPosTan(position + charWidth / 2, coordinates, tangent);
float rotateAngle = (float) Math.toDegrees(
Math.atan2((double) tangent[1], (double) tangent[0]));
charPath.reset();
paint.getTextPath(ch, 0, ch.length(), -charWidth / 2, vOffset, charPath);
charPath.close(); // workaround
matrix.reset();
matrix.postScale(1, -1, 0, 0);
matrix.postRotate(rotateAngle, 0, 0);
matrix.postTranslate(coordinates[0], coordinates[1]);
textPath.addPath(charPath, matrix);
++i;
position = nextPosition;
}
mMainCanvas.drawPath(textPath, paint);
// for debug
//mMainCanvas.drawTextOnPath(text, path, hOffset, vOffset, paint);
}