Skip to content

Commit

Permalink
Implemented translations. Using Matrix for transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelproulx committed Aug 31, 2017
1 parent aeb95e0 commit 20aa2ad
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 119 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/agsw/FabricView/DrawableObjects/CBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

/**
* Created by antwan on 10/3/2015.
Expand Down Expand Up @@ -42,7 +45,20 @@ public Bitmap getBitmap() {

@Override
public void draw(Canvas canvas) {
Matrix matrix = new Matrix();
for (CTransform t:
getTransforms()) {
t.applyTransform(matrix);
}
Bitmap canvasBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(canvasBitmap);
canvas.save();
canvas.concat(matrix);
canvas.drawBitmap(mBitmap, getXcoords(), getYcoords(), getPaint());
canvas.restore();
// Bitmap transformedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

canvas.drawBitmap(canvasBitmap, 0, 0, getPaint());
}

@Override
Expand Down
31 changes: 21 additions & 10 deletions src/main/java/com/agsw/FabricView/DrawableObjects/CDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import java.util.ArrayList;
Expand Down Expand Up @@ -92,14 +94,28 @@ public void setYcoords(int y) {

public abstract void draw(Canvas canvas);

public void applyTransforms(Canvas base) {
Bitmap bitmap = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
// public void applyTransforms(Canvas base) {
// Bitmap bitmap = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Bitmap.Config.ARGB_8888);
// Canvas temp = new Canvas(bitmap);
// for (CTransform t :
// mTransforms) {
// temp = t.applyTransform(temp);
// }
// base.drawBitmap(bitmap, getXcoords(), getYcoords(), getPaint());
// }

public Rect computeBounds() {
RectF bounds = new RectF(x, y, x+width, y+height);
Matrix m = new Matrix();
for (CTransform t :
mTransforms) {
temp = t.applyTransform(temp);
t.applyTransform(m);
}
base.drawBitmap(bitmap, getXcoords(), getYcoords(), getPaint());
m.mapRect(bounds);
Rect result = new Rect();
bounds.round(result);
return result;

}

public boolean hasTransforms() {
Expand Down Expand Up @@ -139,9 +155,4 @@ public boolean equals(Object obj) {
public List<CTransform> getTransforms() {
return mTransforms;
}

public RectF getBounds() {
return new RectF((float)Math.floor(getXcoords()), (float)Math.floor(getYcoords()),
(float)Math.ceil(getXcoords()+getWidth()), (float)Math.ceil(getYcoords()+getHeight()));
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/agsw/FabricView/DrawableObjects/CPath.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.agsw.FabricView.DrawableObjects;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
Expand All @@ -17,7 +18,14 @@ public CPath() {

@Override
public void draw(Canvas canvas) {
canvas.drawPath(mPath, getPaint());
Matrix matrix = new Matrix();
for (CTransform t:
getTransforms()) {
t.applyTransform(matrix);
}
Path copy = new Path(mPath);
copy.transform(matrix);
canvas.drawPath(copy, getPaint());
}

public void lineTo(float eventX, float eventY) {
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/agsw/FabricView/DrawableObjects/CRotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;

/**
* Created by emmanuel.proulx on 2017-08-27.
*/

public class CRotation extends CTransform {
private final CDrawable mDrawable;
private int mRotDegree;

/**
* You must call setRotation after calling this contructor.
*/
public CRotation(CDrawable drawable) {
mDrawable = drawable;
setDrawable(drawable);
}

public CRotation(CDrawable drawable, int rotation) {
mDrawable = drawable;
setDrawable(drawable);
mRotDegree = rotation;
}

Expand All @@ -36,13 +37,18 @@ public void draw(Canvas canvas) {
throw new UnsupportedOperationException("Don't call draw() directly on this class.");
}

// @Override
// public Canvas applyTransform(Canvas canvas) {
// Bitmap bitmap = Bitmap.createBitmap(getDrawable().getWidth(), getDrawable().getHeight(), Bitmap.Config.ARGB_8888);
// Canvas temp = new Canvas(bitmap);
// getDrawable().draw(temp);
// temp.rotate(-getRotation());
// return temp;
// }

@Override
public Canvas applyTransform(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(mDrawable.getWidth(), mDrawable.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
mDrawable.draw(temp);
temp.rotate(-getRotation());
return temp;
public void applyTransform(Matrix m) {
m.setRotate(mRotDegree);
}

@Override
Expand All @@ -52,10 +58,10 @@ public boolean equals(Object obj) {
return false;
}
CRotation other = (CRotation) obj;
if(other.mDrawable == null && this.mDrawable == null) {
if(other.getDrawable() == null && this.getDrawable() == null) {
return true;
}
if(!mDrawable.equals(other.mDrawable)) {
if(!getDrawable().equals(other.getDrawable())) {
return false;
}
return other.mRotDegree == this.mRotDegree;
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/agsw/FabricView/DrawableObjects/CScale.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;

/**
* Created by emmanuel.proulx on 2017-08-27.
*/

public class CScale extends CTransform {
private final CDrawable mDrawable;
private float mFactor = 0.0f;

/**
* You must call setDirection after calling this constructor.
*/
public CScale(CDrawable drawable) {
mDrawable = drawable;
setDrawable(drawable);
}

public CScale(CDrawable drawable, float direction) {
mDrawable = drawable;
setDrawable(drawable);
mFactor = direction;
}

Expand All @@ -37,13 +38,18 @@ public void draw(Canvas canvas) {
throw new UnsupportedOperationException("Don't call draw() directly on this class.");
}

// @Override
// public Canvas applyTransform(Canvas canvas) {
// Bitmap bitmap = Bitmap.createBitmap(getDrawable().getWidth(), getDrawable().getHeight(), Bitmap.Config.ARGB_8888);
// Canvas temp = new Canvas(bitmap);
// getDrawable().draw(temp);
// temp.scale(mFactor, mFactor);
// return temp;
// }

@Override
public Canvas applyTransform(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(mDrawable.getWidth(), mDrawable.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
mDrawable.draw(temp);
temp.scale(mFactor, mFactor);
return temp;
public void applyTransform(Matrix m) {
m.setScale(mFactor, mFactor);
}

@Override
Expand All @@ -53,10 +59,10 @@ public boolean equals(Object obj) {
return false;
}
CScale other = (CScale) obj;
if(other.mDrawable == null && this.mDrawable == null) {
if(other.getDrawable() == null && this.getDrawable() == null) {
return true;
}
if(!mDrawable.equals(other.mDrawable)) {
if(!getDrawable().equals(other.getDrawable())) {
return false;
}
return other.mFactor == this.mFactor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package com.agsw.FabricView.DrawableObjects;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;

/**
* Created by emmanuel.proulx on 2017-08-27.
*/

public abstract class CTransform extends CDrawable {
abstract Canvas applyTransform(Canvas canvas);

private CDrawable mDrawable;

public CDrawable getDrawable() {
return mDrawable;
}

public void setDrawable(CDrawable drawable) {
this.mDrawable = drawable;
}

public abstract void applyTransform(Matrix matrix);
}
28 changes: 17 additions & 11 deletions src/main/java/com/agsw/FabricView/DrawableObjects/CTranslation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;

import java.util.Vector;

Expand All @@ -10,18 +12,17 @@
*/

public class CTranslation extends CTransform {
private final CDrawable mDrawable;
private Vector<Integer> mDirection = new Vector<Integer>(2);

/**
* You must call setDirection after calling this constructor.
*/
public CTranslation(CDrawable drawable) {
mDrawable = drawable;
setDrawable(drawable);
}

public CTranslation(CDrawable drawable, Vector<Integer> direction) {
mDrawable = drawable;
setDrawable(drawable);
mDirection = direction;
}

Expand All @@ -39,13 +40,18 @@ public void draw(Canvas canvas) {
throw new UnsupportedOperationException("Don't call draw() directly on this class.");
}

// @Override
// public Canvas applyTransform(Canvas canvas) {
// Bitmap bitmap = Bitmap.createBitmap(getDrawable().getWidth(), getDrawable().getHeight(), Bitmap.Config.ARGB_8888);
// Canvas temp = new Canvas(bitmap);
// getDrawable().draw(temp);
// temp.translate(mDirection.get(0), mDirection.get(1));
// return temp;
// }

@Override
public Canvas applyTransform(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(mDrawable.getWidth(), mDrawable.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
mDrawable.draw(temp);
temp.translate(mDirection.get(0), mDirection.get(1));
return temp;
public void applyTransform(Matrix m) {
m.setTranslate(mDirection.get(0), mDirection.get(1));
}

@Override
Expand All @@ -55,10 +61,10 @@ public boolean equals(Object obj) {
return false;
}
CTranslation other = (CTranslation) obj;
if(other.mDrawable == null && this.mDrawable == null) {
if(other.getDrawable() == null && this.getDrawable() == null) {
return true;
}
if(!mDrawable.equals(other.mDrawable)) {
if(!getDrawable().equals(other.getDrawable())) {
return false;
}
return other.mDirection == this.mDirection;
Expand Down
Loading

0 comments on commit 20aa2ad

Please sign in to comment.