Skip to content

Commit

Permalink
Add matrixbuffer to avoid constant allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Feb 28, 2016
1 parent 03d91e4 commit 5f51623
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

/**
Expand Down Expand Up @@ -350,6 +351,11 @@ public void centerViewPort(final float[] transformedPts, final View view) {
refresh(save, view, true);
}

/**
* buffer for storing matrix values
*/
protected final float[] matrixBuffer = new float[9];

/**
* call this method to refresh the graph with a given matrix
*
Expand Down Expand Up @@ -377,14 +383,13 @@ public Matrix refresh(Matrix newMatrix, View chart, boolean invalidate) {
*/
public void limitTransAndScale(Matrix matrix, RectF content) {

float[] vals = new float[9];
matrix.getValues(vals);
matrix.getValues(matrixBuffer);

float curTransX = vals[Matrix.MTRANS_X];
float curScaleX = vals[Matrix.MSCALE_X];
float curTransX = matrixBuffer[Matrix.MTRANS_X];
float curScaleX = matrixBuffer[Matrix.MSCALE_X];

float curTransY = vals[Matrix.MTRANS_Y];
float curScaleY = vals[Matrix.MSCALE_Y];
float curTransY = matrixBuffer[Matrix.MTRANS_Y];
float curScaleY = matrixBuffer[Matrix.MSCALE_Y];

// min scale-x is 1f, max is the max float
mScaleX = Math.min(Math.max(mMinScaleX, curScaleX), mMaxScaleX);
Expand All @@ -408,13 +413,13 @@ public void limitTransAndScale(Matrix matrix, RectF content) {
float newTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);
mTransY = newTransY;

vals[Matrix.MTRANS_X] = mTransX;
vals[Matrix.MSCALE_X] = mScaleX;
matrixBuffer[Matrix.MTRANS_X] = mTransX;
matrixBuffer[Matrix.MSCALE_X] = mScaleX;

vals[Matrix.MTRANS_Y] = mTransY;
vals[Matrix.MSCALE_Y] = mScaleY;
matrixBuffer[Matrix.MTRANS_Y] = mTransY;
matrixBuffer[Matrix.MSCALE_Y] = mScaleY;

matrix.setValues(vals);
matrix.setValues(matrixBuffer);
}

/**
Expand Down

0 comments on commit 5f51623

Please sign in to comment.