下面列出了android.graphics.Matrix#preScale ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Calculates the exact output size based upon the crops/rotates and zooms in the hierarchy.
*
* @param inputSize Main image size
* @return Size after applying all zooms/rotates and crops
*/
PointF getOutputSize(@NonNull Point inputSize) {
Matrix matrix = new Matrix();
matrix.preConcat(flipRotate.getLocalMatrix());
matrix.preConcat(cropEditorElement.getLocalMatrix());
matrix.preConcat(cropEditorElement.getEditorMatrix());
EditorElement mainImage = getMainImage();
if (mainImage != null) {
float xScale = 1f / (xScale(mainImage.getLocalMatrix()) * xScale(mainImage.getEditorMatrix()));
matrix.preScale(xScale, xScale);
}
float[] dst = new float[4];
matrix.mapPoints(dst, new float[]{ 0, 0, inputSize.x, inputSize.y });
float widthF = Math.abs(dst[0] - dst[2]);
float heightF = Math.abs(dst[1] - dst[3]);
return new PointF(widthF, heightF);
}
/**
* Set the bitmap reflection
*
* @param bitmap
* @return
*/
public static Bitmap createReflectedImage(Bitmap bitmap, int reflectHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (height <= reflectHeight) {
return null;
}
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height
- reflectHeight, width, reflectHeight, matrix, true);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, reflectHeight,
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(reflectionImage, 0, 0, null);
LinearGradient shader = new LinearGradient(0, 0, 0,
bitmapWithReflection.getHeight(), 0x80ffffff, 0x00ffffff,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, 0, width, bitmapWithReflection.getHeight(), paint);
return bitmapWithReflection;
}
private void drawRocket(Canvas canvas) {
final Matrix matrix = mMatrix;
matrix.reset();
float dragPercent = Math.min(1f, Math.abs(mPercent));
float rocketScale;
float rocketMoveOffset;
float scalePercentDelta = dragPercent - SCALE_START_PERCENT;
float scalePercent = scalePercentDelta / (1.0f - SCALE_START_PERCENT);
rocketScale = ROCKET_INITIAL_SCALE + (ROCKET_FINAL_SCALE - ROCKET_INITIAL_SCALE) * scalePercent;
rocketMoveOffset = mRocketMoveOffset * scalePercent;
matrix.preScale(rocketScale, rocketScale);
final float offsetX = mScreenWidth / 2
- mRocket.getWidth() / 2
+ (1f - rocketScale) * mRocket.getWidth() / 2;
final float offsetY = mRocketTopOffset
+ (1.0f - dragPercent) * (mParent.getTotalDragDistance())
+ rocketMoveOffset
- mTop;
matrix.postTranslate(offsetX, offsetY);
canvas.drawBitmap(mRocket, matrix, null);
}
/**
* Return the bitmap with reflection.
*
* @param src The source of bitmap.
* @param reflectionHeight The height of reflection.
* @param recycle True to recycle the source of bitmap, false otherwise.
* @return the bitmap with reflection
*/
public static Bitmap addReflection(final Bitmap src,
final int reflectionHeight,
final boolean recycle) {
if (isEmptyBitmap(src)) return null;
final int REFLECTION_GAP = 0;
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
srcWidth, reflectionHeight, matrix, false);
Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
Canvas canvas = new Canvas(ret);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
LinearGradient shader = new LinearGradient(
0, srcHeight,
0, ret.getHeight() + REFLECTION_GAP,
0x70FFFFFF,
0x00FFFFFF,
Shader.TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(0, srcHeight + REFLECTION_GAP, srcWidth, ret.getHeight(), paint);
if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
if (recycle && !src.isRecycled() && ret != src) src.recycle();
return ret;
}
/**
* 获得带倒影的图片方法
*
* @param bitmap
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
/**
* 水平翻转图片
*
* @param bitmap 原图
* @return 水平翻转后的图片
*/
public static Bitmap reverseByHorizontal(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, false);
}
/**
* 获得带倒影的图片方法
*
* @param bitmap
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
private void init() {
DEFAULT_HEIGHT = mRadius * 3;
mBezPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBezPaint.setColor(color_bez);//默认QQ糖的颜色为粉红色
mBezPaint.setStyle(Paint.Style.FILL);
mRoundStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRoundStrokePaint.setColor(color_stroke);
mRoundStrokePaint.setStyle(Paint.Style.STROKE);
mRoundStrokePaint.setStrokeWidth(2);
mTouchPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTouchPaint.setColor(color_touch);//默认触摸反馈颜色也为粉红色
mTouchPaint.setStyle(Paint.Style.FILL);
mTouchPaint.setXfermode(clearXfermode);
mBezPath = new Path();
//y轴一样
p5 = new PointF(mRadius * bezFactor, mRadius);
p6 = new PointF(0, mRadius);
p7 = new PointF(-mRadius * bezFactor, mRadius);
//y轴一样
p0 = new PointF(0, -mRadius);
p1 = new PointF(mRadius * bezFactor, -mRadius);
p11 = new PointF(-mRadius * bezFactor, -mRadius);
//x轴一样
p2 = new PointF(mRadius, -mRadius * bezFactor);
p3 = new PointF(mRadius, 0);
p4 = new PointF(mRadius, mRadius * bezFactor);
//x轴一样
p8 = new PointF(-mRadius, mRadius * bezFactor);
p9 = new PointF(-mRadius, 0);
p10 = new PointF(-mRadius, -mRadius * bezFactor);
matrix_bounceL = new Matrix();
matrix_bounceL.preScale(-1, 1);
}
private static Matrix cropMatrix(Bitmap bitmap) {
Matrix matrix = new Matrix();
if (bitmap.getWidth() > bitmap.getHeight()) {
matrix.preScale(1, ((float) bitmap.getHeight()) / bitmap.getWidth());
} else {
matrix.preScale(((float) bitmap.getWidth()) / bitmap.getHeight(), 1);
}
return matrix;
}
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap)
{
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
0, height/2, width, height/2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height,width,height + reflectionGap,
deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
/**
* 获得带倒影的图片方法
* @param bitmap
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
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;
}
public static Bitmap createCutReflectedImage(Bitmap bitmap, int cutHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int totalHeight = mReflectImageHeight + cutHeight;
if (height <= totalHeight) {
return null;
}
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height
- mReflectImageHeight - cutHeight, width, mReflectImageHeight,
matrix, true);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
mReflectImageHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(reflectionImage, 0, 0, null);
LinearGradient shader = new LinearGradient(0, 0, 0,
bitmapWithReflection.getHeight(), 0x80ffffff, 0x00ffffff,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, 0, width, bitmapWithReflection.getHeight(), paint);
if (!reflectionImage.isRecycled()) {
reflectionImage.recycle();
}
System.gc();
return bitmapWithReflection;
}
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;
}
/**
* 倒影图片
* @param originalBitmap
* @return
*/
public static Bitmap createReflectedImage(Bitmap originalBitmap) {
final int reflectionGap = 4;
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
height / 2, width, height / 2, matrix, false);
Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
+ height / 2 + reflectionGap), Config.ARGB_8888);
Canvas canvas = new Canvas(withReflectionBitmap);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalBitmap.getHeight(), 0,
withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(),
paint);
return withReflectionBitmap;
}
public static Bitmap flip(Bitmap src) {
Matrix m = new Matrix();
m.preScale(-1, 1);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
}
private Matrix calculateViewBoxTransform(Box viewPort, Box viewBox, PreserveAspectRatio positioning)
{
Matrix m = new Matrix();
if (positioning == null || positioning.getAlignment() == null)
return m;
float xScale = viewPort.width / viewBox.width;
float yScale = viewPort.height / viewBox.height;
float xOffset = -viewBox.minX;
float yOffset = -viewBox.minY;
// 'none' means scale both dimensions to fit the viewport
if (positioning.equals(PreserveAspectRatio.STRETCH))
{
m.preTranslate(viewPort.minX, viewPort.minY);
m.preScale(xScale, yScale);
m.preTranslate(xOffset, yOffset);
return m;
}
// Otherwise, the aspect ratio of the image is kept.
// What scale are we going to use?
float scale = (positioning.getScale() == PreserveAspectRatio.Scale.Slice) ? Math.max(xScale, yScale) : Math.min(xScale, yScale);
// What size will the image end up being?
float imageW = viewPort.width / scale;
float imageH = viewPort.height / scale;
// Determine final X position
switch (positioning.getAlignment())
{
case XMidYMin:
case XMidYMid:
case XMidYMax:
xOffset -= (viewBox.width - imageW) / 2;
break;
case XMaxYMin:
case XMaxYMid:
case XMaxYMax:
xOffset -= (viewBox.width - imageW);
break;
default:
// nothing to do
break;
}
// Determine final Y position
switch (positioning.getAlignment())
{
case XMinYMid:
case XMidYMid:
case XMaxYMid:
yOffset -= (viewBox.height - imageH) / 2;
break;
case XMinYMax:
case XMidYMax:
case XMaxYMax:
yOffset -= (viewBox.height - imageH);
break;
default:
// nothing to do
break;
}
m.preTranslate(viewPort.minX, viewPort.minY);
m.preScale(scale, scale);
m.preTranslate(xOffset, yOffset);
return m;
}
public Object load(AssetInfo info) throws IOException {
InputStream in = null;
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
in = info.openStream();
BitmapFactory.decodeStream(in,null, options);
float scaleW=(float)options.outWidth /256f;
float scaleH=(float)options.outHeight/256f;
float scale = 1; //Math.max(scaleW,scaleH);
in.close();
in = null;
options = new BitmapFactory.Options();
options.inJustDecodeBounds=false;
options.inPurgeable = false;
options.inSampleSize = (int)FastMath.ceil(scale);
in = info.openStream();
bitmap = BitmapFactory.decodeStream(in, null, options);
if (bitmap == null) {
throw new IOException("Failed to load image: " + info.getKey().getName());
}
} finally {
if (in != null) {
in.close();
}
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Format fmt;
switch (bitmap.getConfig()) {
case ALPHA_8:
fmt = Format.Alpha8;
break;
case ARGB_4444:
fmt = Format.ARGB4444;
break;
case ARGB_8888:
fmt = Format.RGBA8;
break;
case RGB_565:
fmt = Format.RGB565;
break;
default:
// return null;
throw new IOException("Failed to load image: " + info.getKey().getName());
}
if (((TextureKey) info.getKey()).isFlipY()) {
Bitmap newBitmap = null;
Matrix flipMat = new Matrix();
flipMat.preScale(1.0f, -1.0f);
newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false);
bitmap.recycle();
bitmap = newBitmap;
if (bitmap == null) {
throw new IOException("Failed to flip image: " + info.getKey().getName());
}
}
Image image = new Image(fmt, width, height, null);
image.setEfficentData(bitmap);
return image;
}
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);
}
/**
* Flips image horizontally.
*
* @param src input image.
* @return flipped image.
*/
static Bitmap flipHorizontally(Bitmap src) {
Matrix m = new Matrix();
m.preScale(-1, 1);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
}