视频直播源码,android动画小飞机旋转效果
//小飞机旋转动效果
public class PlaneView extends View {
private Paint paint;
private int width;
private int height;
private float curLength;
private float allLength;
private float mAnimatorValue;
private PathMeasure pathMeasure;
private Path path;
private float[] pos;
private float[] tan;
private Bitmap bitmap;
private Matrix mMatrix;
private boolean first;
public PlaneView(Context context) {
super(context);
init();
}
public PlaneView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PlaneView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
path = new Path();
pathMeasure = new PathMeasure();
first = true;
pos = new float[2];
tan = new float[2];
mMatrix = new Matrix();
drawbitmap();
//属性动画
final ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
//设置动画过程的监听
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mAnimatorValue = (float) animation.getAnimatedValue();
postInvalidate();
}
});
valueAnimator.setDuration(2000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
valueAnimator.start();
}
//单独创建一个100,100的bitmap画小飞机
private void drawbitmap() {
Canvas canvas = new Canvas();
bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
Path path = new Path();
path.moveTo(70, 70);
path.lineTo(50, 90);
path.lineTo(50, 50);
path.close();
canvas.drawPath(path, paint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (first) {
width = getWidth();
height = getHeight();
path.addCircle(width / 2, height / 2, 250, Path.Direction.CW);
pathMeasure.setPath(path, false);
allLength = pathMeasure.getLength();
first = false;
}
//画坐标轴和圆路径
canvas.drawLine(width / 2, 0, width / 2, height, paint);
canvas.drawLine(0, height / 2, width, height / 2, paint);
canvas.drawPath(path, paint);
//以curlength获取每一次圆上的点的坐标位置以及正切点位置。
pathMeasure.getPosTan(curLength, pos, tan);
// 以当前点的位置计算bitmap要旋转的角度,Math.atan2(tan[1], tan[0])获取弧度值,根据弧度与角度换算公式,Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI为每一次的角度
float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);
// 旋转,以70,70这个飞机头点为旋转基点
mMatrix.postRotate(degrees, 70, 70);
//平移,以70,70这个飞机头点为旋平移基点
mMatrix.postTranslate(pos[0] - 70, pos[1] - 70);
canvas.drawBitmap(bitmap, mMatrix, paint);
mMatrix.reset();
curLength = mAnimatorValue * allLength;
}
}
以上就是 视频直播源码,android动画小飞机旋转效果,更多内容欢迎关注之后的文章
标签:动画,canvas,float,private,paint,源码,70,path,android From: https://www.cnblogs.com/yunbaomengnan/p/17349324.html