android 旋转箭头,Android PathMeasure小案例-箭头沿环形路径旋转

概述

下述小案例仅为PathMeasure的api的使用总结,非项目使用demo!!!

先看下效果~

fc517bc1a9aa

PathMeasure小案例

注:注释部分代码与下面使用Matrix代码效果相同

public class PathMeasureView extends View {

private Paint mPaint = new Paint();

private Paint mLinePaint = new Paint(); //坐标系

private Bitmap mBitmap;

private Matrix mMatrix = new Matrix();

private float[] pos = new float[2];

private float[] tan = new float[2];

private Path mPath = new Path();

private float mFloat;

public PathMeasureView(Context context) {

super(context);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setColor(Color.BLACK);

mPaint.setStrokeWidth(4);

mLinePaint.setStyle(Paint.Style.STROKE);

mLinePaint.setColor(Color.RED);

mLinePaint.setStrokeWidth(6);

//缩小图片

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 4;

mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.arrow,options);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, mLinePaint);

canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), mLinePaint);

canvas.translate(getWidth() / 2, getHeight() / 2);

mPath.reset();

mPath.addCircle(0,0,200, Path.Direction.CW);

canvas.drawPath(mPath, mPaint);

mFloat += 0.01;

if (mFloat >= 1){

mFloat = 0;

}

// PathMeasure pathMeasure = new PathMeasure(mPath, false);

// pathMeasure.getPosTan(pathMeasure.getLength() * mFloat,pos,tan);

Log.e("TAG", "onDraw: pos[0]="+pos[0]+";pos[1]="+pos[1]);

Log.e("TAG", "onDraw: tan[0]="+tan[0]+";tan[1]="+tan[1]);

// //计算出当前的切线与x轴夹角的度数

// double degrees = Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI;

Log.e("TAG", "onDraw: degrees="+degrees);

// mMatrix.reset();

// //进行角度旋转

// mMatrix.postRotate((float) degrees, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);

// //将图片的绘制点中心与当前点重合

// mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2, pos[1]-mBitmap.getHeight() / 2);

// canvas.drawBitmap(mBitmap,mMatrix, mPaint);

PathMeasure pathMeasure = new PathMeasure(mPath, false);

//将pos信息和tan信息保存在m中

pathMeasure.getMatrix(pathMeasure.getLength() * mFloat, mMatrix, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);

//将图片的旋转坐标调整到图片中心位置

mMatrix.preTranslate(-mBitmap.getWidth() / 2, -mBitmap.getHeight() / 2);

canvas.drawBitmap(mBitmap,mMatrix, mPaint);

invalidate();

}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部