Skip to content

Instantly share code, notes, and snippets.

View AndriyBas's full-sized avatar

Andrii Bas AndriyBas

View GitHub Profile
@AndriyBas
AndriyBas / HolyScaleMath.java
Created November 9, 2016 07:05
how to calculate the holy scale
float widthAspect = 1.0F * canvasWidth / width;
float heightAspect = 1.0F * canvasHeight / height;
// fit the smallest size
holyScale = Math.min(widthAspect, heightAspect);
// map rect vertices, where destPoint and srcPoints are float[]
matrix.mapPoints(destPoints, srcPoints);
boolean pointInTriangle(@NonNull PointF pt, @NonNull PointF v1,
@NonNull PointF v2, @NonNull PointF v3) {
boolean b1 = crossProduct(pt, v1, v2) < 0.0f;
boolean b2 = crossProduct(pt, v2, v3) < 0.0f;
boolean b3 = crossProduct(pt, v3, v1) < 0.0f;
return (b1 == b2) && (b2 == b3);
}
boolean onRotate(RotateGestureDetector detector) {
// ...
rotationInDegrees -= detector.getRotationDegreesDelta();
}
boolean onScale(ScaleGestureDetector detector) {
// ...
scale += (detector.getScaleFactor() - 1.0F);
}
boolean onMove(MoveGestureDetector detector) {
// ...
@AndriyBas
AndriyBas / MatrixMath.java
Last active November 9, 2016 07:27
all the math how to create Matrix from the model
// calculate params
float topLeftX = layer.getX() * canvasWidth;
float topLeftY = layer.getY() * canvasHeight;
// center - for rotation
float centerX = topLeftX + getWidth() * holyScale * 0.5F;
float centerY = topLeftY + getHeight() * holyScale * 0.5F;
float rotationInDegree = layer.getRotationInDegrees();
float scaleX = layer.getScale();
float scaleY = layer.getScale();
@NonNull
private Bitmap createBitmap(@NonNull TextLayer textLayer, @Nullable Bitmap reuseBmp) {
int boundsWidth = canvasWidth;
// init params - size, color, typeface
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(textLayer.getFont().getSize() * canvasWidth);
textPaint.setColor(textLayer.getFont().getColor());
textPaint.setTypeface(fontProvider.getTypeface(textLayer.getFont().getFace()));
Window window = dialog.getWindow();
if (window != null) {
// remove background
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
// remove dim
WindowManager.LayoutParams windowParams = window.getAttributes();
window.setDimAmount(0.0F);
window.setAttributes(windowParams);
}
class Font {
// color value (ex: 0xFF00FF)
private int color;
// name of the font
private String typeface;
// size of the font, relative to parent
private float size;
}
class Layer {
float x; // top left X
float y; // top left Y
float scale;
@FloatRange(from = 0.0F, to = 360.0F)
float rotationInDegrees;
}

Keybase proof

I hereby claim:

  • I am andriybas on github.
  • I am andriybas (https://keybase.io/andriybas) on keybase.
  • I have a public key ASABDupy0ZcakpjaI57_qPDSYqSOqNeK5PvuBebiwr2ECgo

To claim this, I am signing this object: