Android : Android path transform

on Monday, November 10, 2014


I have two views, ImageView and "AnimatedPathView". ImageView is scaled i scrolled. I try get scale the Path. But when i scale the AnimatedPathView canvas then this Path is blur. I try scale path with transform(); But this not working well.



public class AnimatedPathView extends View { public static boolean mAnimatoin = false;




public PathEffect mPathEffect;
public PathEffect mOldPathEffect;

private static float ratio = 1;
public int mStrokeColor;
public float mStrokeWidth;

float mProgress = 0f;
float mPathLength = 0f;

private static float m[];
private Matrix matrix = null;

public AnimatedPathView(Context context)
{
this(context, null);
init();
}

public AnimatedPathView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
init();
}

public AnimatedPathView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnimatedPathView);
a.recycle();

init();
}

public void init()
{
ratio = getResources().getDisplayMetrics().density;

m = new float[9];
matrix = new Matrix();
setPath(new PathEffect(ratio));
}

public void setPath(PathEffect p)
{
mPathEffect = p;
mPathLength = p.length;
}

/**
* Set the drawn path using an array of array of floats. First is x parameter, second is y.
* @param points The points to set on
*/
public void setPath(float[]... points)
{
if(points.length == 0)
throw new IllegalArgumentException("Cannot have zero points in the line");

Path p = new Path();
p.moveTo(points[0][0], points[0][1]);

for(int i=1; i < points.length; i++)
{
p.lineTo(points[i][0], points[i][1]);
}

setPath(new PathEffect(p,ratio));
}

public void setPercentage(float percentage)
{
if(percentage < 0.0f || percentage > 1.0f)
throw new IllegalArgumentException("setPercentage not between 0.0f and 1.0f");

mProgress = percentage;
invalidate();
}

public void setMatrix(Matrix mat)
{
mat.getValues(m);
matrix.set(mat);
}

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

if(mAnimatoin)
{
mPathEffect.createCirclPath(mPathLength,mProgress);
}
else
{
mPathEffect.createCirclPath();
}

canvas.drawPath(mPathEffect.mPath, mPathEffect.mPaint);
}

public void clearView()
{
mOldPathEffect = mPathEffect;
setPath(new PathEffect(ratio));

invalidate();
}

public void clearPath()
{
mPathEffect.mPath.rewind();
invalidate();
}

public void refreshPath()
{
if(mOldPathEffect != null)
{
setPath(mOldPathEffect);

invalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(widthMeasureSpec);

int measuredWidth, measuredHeight;

if(widthMode == MeasureSpec.AT_MOST)
throw new IllegalStateException("AnimatedPathView cannot have a WRAP_CONTENT property");
else
measuredWidth = widthSize;

if(heightMode == MeasureSpec.AT_MOST)
throw new IllegalStateException("AnimatedPathView cannot have a WRAP_CONTENT property");
else
measuredHeight = heightSize;

setMeasuredDimension(measuredWidth, measuredHeight);
}


}


0 comments:

Post a Comment