Android 애니메이션 의 작은 공 을 애니메이션 인 스 턴 스 에 맞 춥 니 다.
구현 효과:
애니메이션 구성:
1.3 단계 베 세 르 곡선 을 통 해 원,의합 계수 의 유래,그리고 제어 점 을 어떻게 선택 하 는 지.
2.캔버스 canvas.translate,scale,rotate 의 방법 을 이용 하여 그 라 데 이 션 을 그 리 는 과정.
3.의합 과정 을 익히다.
4.익숙 하지 않 으 면 보조 점 의 이동 경 로 를 먼저 그 려 두 개의 원 의 분열 을 이해 하 는 의합 과정 에 도움 이 된다.
package com.example.administrator.animationworkdemo.views;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
import java.util.concurrent.CyclicBarrier;
/**
* , , ,
* , , , ,
*/
public class BallShapeChangeView extends View {
// magic number
//C .
private static final float C = 0.551915024494f;
private Paint mPaint;
private int mRadiusBig = 120, mRadiusSmall = (int) (mRadiusBig / 2f), mWidth, mHeight, mMimWidth = (int) (mRadiusSmall * 2 * 3)/*fill view mim width*/;
private float mFraction = 0, mFractionDegree = 0, /*degree*/
mLength, mDistanceBezier;
private Path mPathCircle, mPathBezier;
private ValueAnimator mValueAnimator;
private float[] mPointData = new float[8];// 4 ,
private float[] mPointCtrl = new float[16];// 8
private float[] mPos = new float[2];
private PathMeasure mPathMeasure;
private Path mPathBezier2;
public BallShapeChangeView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(0xFF7C191E);
mPaint.setAntiAlias(true);
mPathCircle = new Path();
mPathBezier = new Path();
mPathBezier2 = new Path();
mPathMeasure = new PathMeasure();
mValueAnimator = ValueAnimator.ofFloat(0, 1, 0);
mValueAnimator.setDuration(3000);
mValueAnimator.setRepeatCount(Integer.MAX_VALUE);
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mFraction = (float) animation.getAnimatedValue();
mFractionDegree = animation.getAnimatedFraction();
invalidate();
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// , ,
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode != MeasureSpec.AT_MOST && heightMode != MeasureSpec.AT_MOST) {
if (mWidth < mMimWidth)
mWidth = mMimWidth;
if (mHeight < mMimWidth)
mHeight = mMimWidth;
} else if (widthMeasureSpec != MeasureSpec.AT_MOST) {
if (mWidth < mMimWidth)
mWidth = mMimWidth;
} else if (heightMeasureSpec != MeasureSpec.AT_MOST) {
if (mHeight < mMimWidth)
mHeight = mMimWidth;
}
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// mFraction ,
canvas.translate(mWidth / 2, mHeight / 2);
canvas.scale(1, -1);
canvas.rotate(-360 * mFractionDegree);
setDoubleCirClePath();
canvas.drawPath(mPathCircle, mPaint);
if (mFraction < (1 / 3f)) {//
setCirclePath();
canvas.drawPath(mPathCircle, mPaint);
} else if (mFraction < 3 / 4f) {//
setBezierPath2();
canvas.drawPath(mPathBezier, mPaint);
canvas.drawPath(mPathBezier2, mPaint);
} else {//
//setLastBezierPath();
//canvas.drawPath(mPathBezier, mPaint);
}
}
private void setDoubleCirClePath() {
mPathCircle.reset();
if (mFraction < (1 / 3f)) {
mPathCircle.addCircle(-mRadiusSmall / 2f * mFraction * 3, 0, mRadiusSmall, Path.Direction.CW);
mPathCircle.addCircle(mRadiusSmall / 2f * mFraction * 3, 0, mRadiusSmall, Path.Direction.CW);
} else {
float distance = (mFraction - 1 / 3f) / (2 / 3f) * (mRadiusSmall * 2 + mRadiusSmall / 2f);
mPathCircle.addCircle(-mRadiusSmall / 2f - distance, 0, mRadiusSmall, Path.Direction.CW);
mPathCircle.addCircle(mRadiusSmall / 2f + distance, 0, mRadiusSmall, Path.Direction.CW);
}
}
// mFraction 0 ~ 1/3
private void setCirclePath() {
mPointData[0] = -mRadiusBig + mRadiusSmall / 2f * mFraction * 3f;
mPointData[1] = 0;
mPointData[2] = 0;
mPointData[3] = mRadiusBig - mRadiusBig / 2f * mFraction * 3f;//0 1 ;
mPointData[4] = mRadiusBig - mRadiusSmall / 2f * mFraction * 3f;
mPointData[5] = 0;
mPointData[6] = mPointData[2];
mPointData[7] = -mPointData[3];
mPointCtrl[0] = mPointData[0];// x
mPointCtrl[1] = mRadiusBig * C;// y
mPointCtrl[2] = mPointData[2] - mRadiusBig * C;
mPointCtrl[3] = mPointData[3];// y
mPointCtrl[4] = mPointData[2] + mRadiusBig * C;
mPointCtrl[5] = mPointData[3];
mPointCtrl[6] = mPointData[4];
mPointCtrl[7] = mPointCtrl[1];
mPointCtrl[8] = mPointData[4];
mPointCtrl[9] = -mPointCtrl[1];
mPointCtrl[10] = mPointCtrl[4];
mPointCtrl[11] = mPointData[7];
mPointCtrl[12] = mPointCtrl[2];
mPointCtrl[13] = mPointData[7];
mPointCtrl[14] = mPointData[0];
mPointCtrl[15] = -mPointCtrl[1];
mPathCircle.reset();
mPathCircle.moveTo(mPointData[0], mPointData[1]);
mPathCircle.cubicTo(mPointCtrl[0], mPointCtrl[1], mPointCtrl[2], mPointCtrl[3], mPointData[2], mPointData[3]);
mPathCircle.cubicTo(mPointCtrl[4], mPointCtrl[5], mPointCtrl[6], mPointCtrl[7], mPointData[4], mPointData[5]);
mPathCircle.cubicTo(mPointCtrl[8], mPointCtrl[9], mPointCtrl[10], mPointCtrl[11], mPointData[6], mPointData[7]);
mPathCircle.cubicTo(mPointCtrl[12], mPointCtrl[13], mPointCtrl[14], mPointCtrl[15], mPointData[0], mPointData[1]);
}
// mFraction 1/3 ~ 3/4
private void setBezierPath2() {
mPointData[0] = -mRadiusSmall / 2 - (mFraction - 1 / 3f) * mRadiusBig * 2f;
if (mFraction < 2 / 3f) {
mPointData[1] = -mRadiusSmall;
} else {
mPointData[1] = -mRadiusSmall + (mFraction - 2 / 3f) * 3 * mRadiusSmall;
}
if (mFraction < 3 / 4f) {
mPointData[2] = 0;
} else {
//
mPointData[2] = (mFraction - 3 / 4f) * 16 * mPointData[0];
}
// 2/3 , 0
mPointData[3] = -mRadiusBig + mFraction * mRadiusBig * 1.5f < -0.01f * mRadiusBig ? -mRadiusBig + mFraction * mRadiusBig * 1.5f : 0.01f * -mRadiusBig;
mPointData[4] = mPointData[2];
mPointData[5] = -mPointData[3];
mPointData[6] = mPointData[0];
mPointData[7] = -mPointData[1];
mPointCtrl[0] = mPointData[0] + mRadiusSmall;
mPointCtrl[1] = mPointData[3];
mPointCtrl[2] = mPointData[0] + mRadiusSmall;
mPointCtrl[3] = -mPointData[3];
mPathBezier.reset();
mPathBezier.moveTo(mPointData[0], mPointData[1]);
mPathBezier.quadTo(mPointCtrl[0], mPointCtrl[1], mPointData[2], mPointData[3]);
mPathBezier.lineTo(mPointData[4], mPointData[5]);
mPathBezier.quadTo(mPointCtrl[2], mPointCtrl[3], mPointData[6], mPointData[7]);
mPathBezier2.reset();
mPathBezier2.moveTo(-mPointData[0], mPointData[1]);
mPathBezier2.quadTo(-mPointCtrl[0], mPointCtrl[1], -mPointData[2], mPointData[3]);
mPathBezier2.lineTo(-mPointData[4], mPointData[5]);
mPathBezier2.quadTo(-mPointCtrl[2], mPointCtrl[3], -mPointData[6], mPointData[7]);
}
// mFraction 1/3 ~ 3/4
private void setBezierPath() {
mPathBezier.reset();
float distance = (2 * mRadiusSmall + mRadiusSmall / 2f) * mFraction;
//float topY = mRadiusSmall * (1 - 0.6f * mFraction);
float topY = mRadiusSmall - mRadiusSmall * (mFraction - 1 / 3f);
float distanceBezier = topY - distance * C * (0.5f + 0.5f * mFraction);
if (mDistanceBezier != 0 && distanceBezier < (mDistanceBezier)) {
distanceBezier = mDistanceBezier;
}
mPathBezier.moveTo(-distance, topY);
mPathBezier.cubicTo(-distance, distanceBezier, distance, distanceBezier, distance, topY);
if (mDistanceBezier == 0) {
mPathMeasure.setPath(mPathBezier, false);
mLength = mPathMeasure.getLength();
mPathMeasure.getPosTan(mLength / 2, mPos, null);
if (mPos[1] <= 8) {
mDistanceBezier = distanceBezier;
mPathBezier.reset();
mPathBezier.moveTo(-distance, topY);
mPathBezier.cubicTo(-distance, mDistanceBezier, distance, mDistanceBezier, distance, topY);
mPathBezier.lineTo(distance, -topY);
mPathBezier.cubicTo(distance, -mDistanceBezier, -distance, -mDistanceBezier, -distance, -topY);
mPathBezier.close();
return;
}
}
mPathBezier.lineTo(distance, -topY);
mPathBezier.cubicTo(distance, -distanceBezier, -distance, -distanceBezier, -distance, -topY);
mPathBezier.close();
}
// mFraction 3/4 ~ 1
private void setLastBezierPath() {
float x = -mRadiusSmall / 2f - (mFraction - 1 / 3f) / (2 / 3f) * (mRadiusSmall * 2 + mRadiusSmall / 2f);
mPathBezier.reset();
mPathBezier.moveTo(x, mRadiusSmall);
mPathBezier.quadTo(x, 0, x + mRadiusSmall + mRadiusSmall * (4 - mFraction * 4), 0);
mPathBezier.quadTo(x, 0, x, -mRadiusSmall);
mPathBezier.lineTo(x, mRadiusSmall);
mPathBezier.moveTo(-x, mRadiusSmall);
mPathBezier.quadTo(-x, 0, -x - mRadiusSmall - mRadiusSmall * (4 - mFraction * 4), 0);
mPathBezier.quadTo(-x, 0, -x, -mRadiusSmall);
mPathBezier.lineTo(-x, mRadiusSmall);
mPathBezier.close();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mValueAnimator.isRunning())
mValueAnimator.start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mValueAnimator.isRunning())
mValueAnimator.cancel();
}
}
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.