下面列出了android.graphics.Matrix#preRotate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
int centerX = width / 2;
int centerY = height / 2;
innerPadding = (int) (paramInnerPadding * width / 100);
outerPadding = (int) (paramOuterPadding * width / 100);
arrowPointerSize = (int) (paramArrowPointerSize * width / 100);
valueSliderWidth = (int) (paramValueSliderWidth * width / 100);
outerWheelRadius = width / 2 - outerPadding - arrowPointerSize;
innerWheelRadius = outerWheelRadius - valueSliderWidth;
colorWheelRadius = innerWheelRadius - innerPadding;
outerWheelRect.set(centerX - outerWheelRadius, centerY - outerWheelRadius, centerX + outerWheelRadius, centerY + outerWheelRadius);
innerWheelRect.set(centerX - innerWheelRadius, centerY - innerWheelRadius, centerX + innerWheelRadius, centerY + innerWheelRadius);
colorWheelBitmap = createColorWheelBitmap(colorWheelRadius * 2, colorWheelRadius * 2);
gradientRotationMatrix = new Matrix();
gradientRotationMatrix.preRotate(270, width / 2, height / 2);
colorViewPath.arcTo(outerWheelRect, 270, -180);
colorViewPath.arcTo(innerWheelRect, 90, 180);
valueSliderPath.arcTo(outerWheelRect, 270, 180);
valueSliderPath.arcTo(innerWheelRect, 90, -180);
}
public void drawPointer(Canvas c) {
Matrix matrix = new Matrix();
// 设置缩放
matrix.postScale(scale, scale);
// 开始转
matrix.preRotate((float) -discStart);
// 转轴还原
matrix.preTranslate(-(float) brWidth / 2, -(float) brHeight / 2);
// 将位置送到view的中心
matrix.postTranslate(WidthCenter, HeightCenter);
// 绘制图片
c.drawBitmap(bitmapScale, matrix, bitmapRefreshPaint);
}
public static Bitmap rotate(Bitmap scaledBitmap, int i) {
if (i == 0) {
return scaledBitmap;
}
Matrix matrix = new Matrix();
matrix.preRotate(-i);
return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(),
scaledBitmap.getHeight(), matrix, false);
}
public Matrix matrixForDraw ()
{
Matrix cMatrix = new Matrix();
float ax = anchorPoint.x * bounds().width();
float ay = anchorPoint.y * bounds.height();
cMatrix.preTranslate(position.x, position.y);
if (rotation != 0)
cMatrix.preRotate((float) Math.toDegrees(rotation));
if (scaleX != 1 || scaleY != 1)
cMatrix.preScale(scaleX, scaleY);
cMatrix.preTranslate(-ax, -ay);
return cMatrix;
}
public Matrix matrixForBackwardConvert ()
{
Matrix cMatrix = new Matrix();
float ax = anchorPoint.x * bounds().width();
float ay = anchorPoint.y * bounds.height();
cMatrix.preTranslate(position.x, position.y);
if (rotation != 0)
cMatrix.preRotate((float) Math.toDegrees(rotation));
if (scaleX != 1 || scaleY != 1)
cMatrix.preScale(scaleX, scaleY);
cMatrix.preTranslate(-ax, -ay);
return cMatrix;
}
/**
* Draw sun
*
* @param canvas canvas
*/
private void drawSun(Canvas canvas) {
final Matrix matrix = mMatrix;
matrix.reset();
float dragPercent = Math.min(1f, Math.abs(mPercent));
final float offsetX = isRefreshing ? mScreenWidth / 2 * (2 - dragPercent) - mSunWidth / 2 : (mScreenWidth * dragPercent - mSunWidth) / 2;
final float offsetY = totalDistance * func(dragPercent);
matrix.preRotate(mSunRotateAngle == 0.0f ? (360 * dragPercent) : mSunRotateAngle, mSunWidth / 2, mSunHeight / 2);
matrix.postTranslate(offsetX, offsetY);
canvas.drawBitmap(mSun, matrix, null);
}
/**
* Rotate the bitmap a given amount of degrees. This is used to get the correct bitmap when
* the device is in landscape mode.
* @param bitmap
* @param degrees
* @return a rotated bitmap
*/
public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(degrees);
// Rotating Bitmap
Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
return Bitmap.createScaledBitmap(rotated, bitmap.getWidth(), bitmap.getHeight(), true);
}
public static boolean setImageToScreenMatrix(Matrix dst, RectF image,
RectF screen, int rotation) {
RectF rotatedImage = new RectF();
dst.setRotate(rotation, image.centerX(), image.centerY());
if (!dst.mapRect(rotatedImage, image)) {
return false; // fails for rotations that are not multiples of 90
// degrees
}
boolean rToR = dst.setRectToRect(rotatedImage, screen, Matrix.ScaleToFit.CENTER);
boolean rot = dst.preRotate(rotation, image.centerX(), image.centerY());
return rToR && rot;
}
public static Matrix transformMatrix(int transform, float px, float py) {
Matrix matrix = new Matrix();
switch (transform) {
case Sprite.TRANS_ROT90:
matrix.preRotate(90, px, py);
break;
case Sprite.TRANS_ROT180:
matrix.preRotate(180, px, py);
break;
case Sprite.TRANS_ROT270:
matrix.preRotate(270, px, py);
break;
case Sprite.TRANS_MIRROR:
matrix.preScale(-1, 1, px, py);
break;
case Sprite.TRANS_MIRROR_ROT90:
matrix.preRotate(90, px, py);
matrix.preScale(-1, 1, px, py);
break;
case Sprite.TRANS_MIRROR_ROT180:
matrix.preRotate(180, px, py);
matrix.preScale(-1, 1, px, py);
break;
case Sprite.TRANS_MIRROR_ROT270:
matrix.preRotate(270, px, py);
matrix.preScale(-1, 1, px, py);
break;
}
return matrix;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF oval = new RectF(
CIRCLE_STROKE_WIDTH,
CIRCLE_STROKE_WIDTH,
mSize - CIRCLE_STROKE_WIDTH,
mSize - CIRCLE_STROKE_WIDTH);
Paint circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setStrokeWidth(CIRCLE_STROKE_WIDTH);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeCap(Paint.Cap.ROUND);
Matrix matrix = new Matrix();
matrix.preRotate(CIRCLE_START_ANGLE, mSize / 2, mSize / 2);
SweepGradient gradient = new SweepGradient(mSize / 2, mSize / 2,
CIRCLE_STROKE_COLOR, CIRCLE_STROKE_COLOR_END);
gradient.setLocalMatrix(matrix);
circlePaint.setShader(gradient);
canvas.drawArc(oval, CIRCLE_START_ANGLE, mProgress, false, circlePaint);
Paint fullPaint = new Paint();
fullPaint.setAntiAlias(true);
circlePaint.setStyle(Paint.Style.STROKE);
fullPaint.setStrokeCap(Paint.Cap.ROUND);
fullPaint.setColor(Color.TRANSPARENT);
if (mProgress == 360) {
fullPaint.setColor(CIRCLE_STROKE_COLOR_END);
} else if (mProgress > 1) {
fullPaint.setColor(CIRCLE_STROKE_COLOR);
}
canvas.drawCircle(mSize / 2, CIRCLE_STROKE_WIDTH, CIRCLE_STROKE_WIDTH / 2, fullPaint);
}
@Override
public PLIImage rotate(float degrees, float px, float py) {
Matrix matrix = new Matrix();
matrix.preRotate(degrees, px, py);
Bitmap image = Bitmap.createBitmap(mBitmap, 0, 0, mWidth, mHeight, matrix, true);
this.deleteImage();
this.createWithBitmap(image, false);
return this;
}
/**rotate methods*/
@Override
public PLIImage rotate(int angle)
{
if((angle % 90) != 0)
return this;
Matrix matrix = new Matrix();
matrix.preRotate(angle);
Bitmap image = Bitmap.createBitmap(mBitmap, 0, 0, mWidth, mHeight, matrix, true);
this.deleteImage();
this.createWithBitmap(image, false);
return this;
}
@Override
public PLIImage rotate(float degrees, float px, float py)
{
Matrix matrix = new Matrix();
matrix.preRotate(degrees, px, py);
Bitmap image = Bitmap.createBitmap(mBitmap, 0, 0, mWidth, mHeight, matrix, true);
this.deleteImage();
this.createWithBitmap(image, false);
return this;
}
public static Bitmap getTiled(final Bitmap source, final int xSize, final int ySize, final boolean tile, final String background_file) {
Bitmap tiledBitmap = null;
if (background_file != null) {
try {
tiledBitmap = BitmapLoader.bitmapFromBundleCache(background_file);
if (tiledBitmap == null) {
android.util.Log.d("NumberWall", "Regenerating image");
final Uri background_uri = Uri.parse(background_file);
FileDescriptor fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor();
final Bitmap image_bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
Bitmap rotated_bitmap = image_bitmap;
final Matrix image_matrix = new Matrix();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor(); // reset
final ExifInterface exif = new ExifInterface(fileDescriptor);
int rotation = exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
android.util.Log.d("NumberWall", "Rotation: " + rotation);
if (rotation != 0) {
image_matrix.preRotate(rotation);
rotated_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), image_matrix, true);
image_bitmap.recycle();
}
}
tiledBitmap = getBestCroppedScaled(rotated_bitmap, xSize, ySize);
BitmapLoader.saveBitmapAsBundle(background_file, Bitmap.createBitmap(tiledBitmap));
} else {
tiledBitmap = Bitmap.createBitmap(tiledBitmap); // make a copy
android.util.Log.d("NumberWall", "cache hit");
}
} catch (Exception e) {
// cannot load bitmap
android.util.Log.e("NumberWall", "Cannot load bitmap: " + e);
}
}
if (tiledBitmap == null) {
tiledBitmap = Bitmap.createBitmap(xSize, ySize, Bitmap.Config.ARGB_8888);
}
if (source != null) {
final Canvas canvas = new Canvas(tiledBitmap);
final BitmapDrawable drawable = new BitmapDrawable(xdrip.getAppContext().getResources(), source);
drawable.setBounds(0, 0, xSize, ySize);
if (tile) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.draw(canvas);
} else {
double y_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_y_param", ""), 50d) / 100d; // TODO move to method signature
int yoffset = Math.max(0, (int) ((getScreenDpi() * y_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
final double spacer_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_s_param", ""), 10d) / 100d;
int xoffset = Math.max(0, (int) ((getScreenDpi() * spacer_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
canvas.drawBitmap(source, xoffset, yoffset, new Paint());
}
source.recycle();
} // returns blank bitmap if source is null
return tiledBitmap;
}
public static void setRectToRect(Matrix matrix, RectF src, RectF dst, int rotation, Matrix.ScaleToFit align)
{
float tx, sx;
float ty, sy;
if (rotation == 90 || rotation == 270)
{
sx = dst.height() / src.width();
sy = dst.width() / src.height();
}
else
{
sx = dst.width() / src.width();
sy = dst.height() / src.height();
}
if (align != Matrix.ScaleToFit.FILL)
{
if (sx > sy)
{
sx = sy;
}
else
{
sy = sx;
}
}
tx = -src.left * sx;
ty = -src.top * sy;
matrix.setTranslate(dst.left, dst.top);
if (rotation == 90)
{
matrix.preRotate(90);
matrix.preTranslate(0, -dst.width());
}
else if (rotation == 180)
{
matrix.preRotate(180);
matrix.preTranslate(-dst.width(), -dst.height());
}
else if (rotation == 270)
{
matrix.preRotate(270);
matrix.preTranslate(-dst.height(), 0);
}
matrix.preScale(sx, sy);
matrix.preTranslate(tx, ty);
}
/**
* Takes a screenshot of the current display and shows an animation.
*/
@SuppressLint("NewApi")
public void takeScreenshot(Context context, String fileFullPath)
{
if(fileFullPath == ""){
format = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = format.format(new Date(System.currentTimeMillis())) + ".png";
fileFullPath = "/data/local/tmp/" + fileName;
}
if(ShellUtils.checkRootPermission()){
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
ShellUtils.execCommand("/system/bin/screencap -p "+ fileFullPath,true);
}
}
else {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = wm.getDefaultDisplay();
mDisplayMatrix = new Matrix();
mDisplayMetrics = new DisplayMetrics();
// We need to orient the screenshot correctly (and the Surface api seems to take screenshots
// only in the natural orientation of the device :!)
mDisplay.getRealMetrics(mDisplayMetrics);
float[] dims =
{
mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels
};
float degrees = getDegreesForRotation(mDisplay.getRotation());
boolean requiresRotation = (degrees > 0);
if (requiresRotation)
{
// Get the dimensions of the device in its native orientation
mDisplayMatrix.reset();
mDisplayMatrix.preRotate(-degrees);
mDisplayMatrix.mapPoints(dims);
dims[0] = Math.abs(dims[0]);
dims[1] = Math.abs(dims[1]);
}
Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]);
if (requiresRotation)
{
// Rotate the screenshot to the current orientation
Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(ss);
c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
c.rotate(degrees);
c.translate(-dims[0] / 2, -dims[1] / 2);
c.drawBitmap(mScreenBitmap, 0, 0, null);
c.setBitmap(null);
mScreenBitmap = ss;
if (ss != null && !ss.isRecycled())
{
ss.recycle();
}
}
// If we couldn't take the screenshot, notify the user
if (mScreenBitmap == null)
{
Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show();
}
// Optimizations
mScreenBitmap.setHasAlpha(false);
mScreenBitmap.prepareToDraw();
saveBitmap2file(context, mScreenBitmap, fileFullPath);
}
}
}
@Override
@NonNull
protected BitmapWorkerResult doInBackground(Void... params) {
if (mInputUri == null) {
return new BitmapWorkerResult(new NullPointerException("Input Uri cannot be null"));
}
final ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = mContext.getContentResolver().openFileDescriptor(mInputUri, "r");
} catch (FileNotFoundException e) {
return new BitmapWorkerResult(e);
}
final FileDescriptor fileDescriptor;
if (parcelFileDescriptor != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
} else {
return new BitmapWorkerResult(new NullPointerException("ParcelFileDescriptor was null for given Uri: [" + mInputUri + "]"));
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
if (options.outWidth == -1 || options.outHeight == -1) {
return new BitmapWorkerResult(new IllegalArgumentException("Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]"));
}
options.inSampleSize = BitmapLoadUtils.calculateInSampleSize(options, mRequiredWidth, mRequiredHeight);
options.inJustDecodeBounds = false;
Bitmap decodeSampledBitmap = null;
boolean decodeAttemptSuccess = false;
while (!decodeAttemptSuccess) {
try {
decodeSampledBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
decodeAttemptSuccess = true;
} catch (OutOfMemoryError error) {
Log.e(TAG, "doInBackground: BitmapFactory.decodeFileDescriptor: ", error);
options.inSampleSize *= 2;
}
}
if (decodeSampledBitmap == null) {
return new BitmapWorkerResult(new IllegalArgumentException("Bitmap could not be decoded from the Uri: [" + mInputUri + "]"));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
BitmapLoadUtils.close(parcelFileDescriptor);
}
int exifOrientation = BitmapLoadUtils.getExifOrientation(mContext, mInputUri);
int exifDegrees = BitmapLoadUtils.exifToDegrees(exifOrientation);
int exifTranslation = BitmapLoadUtils.exifToTranslation(exifOrientation);
ExifInfo exifInfo = new ExifInfo(exifOrientation, exifDegrees, exifTranslation);
Matrix matrix = new Matrix();
if (exifDegrees != 0) {
matrix.preRotate(exifDegrees);
}
if (exifTranslation != 1) {
matrix.postScale(exifTranslation, 1);
}
if (!matrix.isIdentity()) {
return new BitmapWorkerResult(BitmapLoadUtils.transformBitmap(decodeSampledBitmap, matrix), exifInfo);
}
return new BitmapWorkerResult(decodeSampledBitmap, exifInfo);
}
public static void setRectToRect(Matrix matrix, RectF src, RectF dst, int rotation, Matrix.ScaleToFit align)
{
float tx, sx;
float ty, sy;
if (rotation == 90 || rotation == 270)
{
sx = dst.height() / src.width();
sy = dst.width() / src.height();
}
else
{
sx = dst.width() / src.width();
sy = dst.height() / src.height();
}
if (align != Matrix.ScaleToFit.FILL)
{
if (sx > sy)
{
sx = sy;
}
else
{
sy = sx;
}
}
tx = -src.left * sx;
ty = -src.top * sy;
matrix.setTranslate(dst.left, dst.top);
if (rotation == 90)
{
matrix.preRotate(90);
matrix.preTranslate(0, -dst.width());
}
else if (rotation == 180)
{
matrix.preRotate(180);
matrix.preTranslate(-dst.width(), -dst.height());
}
else if (rotation == 270)
{
matrix.preRotate(270);
matrix.preTranslate(-dst.height(), 0);
}
matrix.preScale(sx, sy);
matrix.preTranslate(tx, ty);
}
public static Bitmap preRotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.preRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
}
@Override
public void onPrepareBufferEx(final Viewport viewport, final Canvas c)
{
if (!route.show)
return;
final int[] cxy = viewport.mapCenterXY;
final int half = Math.round(pointWidth / 2);
List<Waypoint> waypoints = route.getWaypoints();
synchronized (waypoints)
{
for (Waypoint wpt : waypoints)
{
Bitmap bitmap = bitmaps.get(wpt);
if (bitmap == null)
{
int width = pointWidth;
int height = pointWidth + 2;
if (showNames)
{
Rect bounds = new Rect();
textPaint.getTextBounds(wpt.name, 0, wpt.name.length(), bounds);
bounds.inset(-2, -4);
width += 5 + bounds.width();
if (height < bounds.height())
height = bounds.height();
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bc = new Canvas(bitmap);
bc.translate(half, half);
if (showNames)
bc.translate(0, 2);
bc.drawCircle(0, 0, half, fillPaint);
bc.drawCircle(0, 0, half, borderPaint);
if (showNames)
{
Rect rect = new Rect();
textPaint.getTextBounds(wpt.name, 0, wpt.name.length(), rect);
rect.inset(-2, -4);
rect.offset(+half + 5, +half - 3);
bc.drawRect(rect, textFillPaint);
bc.drawText(wpt.name, +half + 6, +half, textPaint);
}
bitmaps.put(wpt, bitmap);
}
int[] xy = application.getXYbyLatLon(wpt.latitude, wpt.longitude);
Matrix matrix = new Matrix();
if (viewport.mapHeading != 0f)
matrix.preRotate(viewport.mapHeading, half, half);
matrix.postTranslate(xy[0] - half - cxy[0], xy[1] - half - cxy[1]);
c.drawBitmap(bitmap, matrix, null);
}
}
}