Closed RealMoMo closed 6 years ago
add 建议:dispatchDraw方法临时变量较多,其实均可以去掉。测试在一些低端机,动画会流畅很多。
int height; //在onsiezechange初始化
@Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mltWave.size() > 0 && mPaint != null) { //View thisView = this; //int height = thisView.getHeight(); //long thisTime = System.currentTimeMillis(); for (Wave wave : mltWave) { mMatrix.reset(); canvas.save(); if (wave.velocity != 0) { //float offsetX = (wave.offsetX - (wave.velocity * mVelocity * (thisTime - mLastTime) / 1000f)); // realmo test : ((thisTime - mLastTime) / 1000f) = 0.085左右 // float offsetX = (wave.offsetX - (wave.velocity * mVelocity * 0.085f)); // if (-wave.velocity > 0) { // offsetX %= wave.width / 2; // } else { // while (offsetX < 0) { // offsetX += (wave.width / 2); // } // } // wave.offsetX = offsetX; // mMatrix.setTranslate(offsetX, (1 - mProgress) * height);//wave.offsetX = // canvas.translate(-offsetX, -wave.offsetY - (1 - mProgress) * height); //optimized wave.offsetX = (wave.offsetX - (wave.velocity * mVelocity * 0.085f)); if (-wave.velocity > 0) { wave.offsetX %= wave.width / 2; } else { while (wave.offsetX < 0) { wave.offsetX += (wave.width / 2); } } mMatrix.setTranslate(wave.offsetX, (1 - mProgress) * height);//wave.offsetX = canvas.translate(-wave.offsetX, -wave.offsetY - (1 - mProgress) * height); } else{ mMatrix.setTranslate(wave.offsetX, (1 - mProgress) * height); canvas.translate(-wave.offsetX, -wave.offsetY - (1 - mProgress) * height); } mPaint.getShader().setLocalMatrix(mMatrix); canvas.drawPath(wave.path, mPaint); canvas.restore(); } } if (mIsRunning) { invalidate(); } }
1.哪些属性无效? 2.局部变量对性能影响不大,真正消耗性能的大户在 setTranslate translate drawPath setLocalMatrix 中,动画时间的计算不能去掉,去掉之后,动画的速率就变成了帧数相关(不同性能的机子帧数不一样,会导致速度不一样),不是时间相关。
add 建议:dispatchDraw方法临时变量较多,其实均可以去掉。测试在一些低端机,动画会流畅很多。
int height; //在onsiezechange初始化