gpt4 book ai didi

android - 在 Android 的自定义 View 中模拟动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:28 24 4
gpt4 key购买 nike

我有一个自定义 View ,它看起来像一个带有 0 到 9 数字的纺车。基本上,当我从服务器下载内容时,它会从 0 向下滚动到 9,当返回值时,动画会在其上停止。动画是通过更新我正在绘制的文字的Y值制作的

一些相关代码:

public class TimeSpinner extends View
{

.....
private void initialize()
{
.....

handler = new Handler();
repetitiveRunnable = new Runnable() {
@Override
public void run() {
updateData();
}
};

}

public void updateData() {

float delta = 3;

mDigitY += delta;
mDigitAboveY += delta;
mDigitBelowY += delta;

//Test if animation needs to stop
if (mCurrentDigit == animateToDigit) {
handler.removeCallbacks(repetitiveRunnable);
} else {
handler.postDelayed(repetitiveRunnable, 5);
}

invalidate();
}


public void startLoadingDigit() {
handler.postDelayed(repetitiveRunnable, 5);
}

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

canvas.drawText(mDigitString, mDigitX, mDigitY, mDigitPaint);
canvas.drawText(mDigitAboveString, mDigitX, mDigitAboveY, mDigitPaint);
canvas.drawText(mDigitBelowString, mDigitX, mDigitBelowY, mDigitPaint);

}

}

问题是 UI 线程要在其他 View 上进行一些绘制,因此动画不流畅取决于手机的速度。就像帧率很差,或者有时它会停半秒。在功能强大的设备上相当不错。

现在的问题是,无论应用程序在做什么,我该怎么做才能使动画流畅? View 很简单,不是 SurfaceView。我应该以某种方式使用线程吗?代码示例会很棒。

稍后编辑。

我尝试使用 AsycTask 更新 Y 坐标

public class TimeSpinnerTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... arg0) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

while (animationRunning) {

updateData();
publishProgress();

try {
Thread.sleep(35);
} catch (InterruptedException e) {

}
}
return null;
}


@Override
protected void onProgressUpdate(Void... values) {
invalidate();
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {
invalidate();
super.onPostExecute(result);
}
}

public void updateData() {

mDigitY += delta;
mDigitAboveY += delta;
mDigitBelowY += delta;

if (mDigitAboveY > findCenterY(mCurrentDigit)) {
setCurrentDigit(mDigitAbove);
}

if (mCurrentDigit == animateToDigit) {

animationRunning = false;

setCurrentDigit(animateToDigit);
}
}

虽然这看起来确实有点流畅,但在旧设备上我仍然会遇到快门问题,有时动画甚至会暂停 1 秒然后继续。

异步任务通过 executeOnExecutor 启动, View 显示在 Android Maps V2 的 map 控件上。尤其是在 map 加载图 block 时会出现快门。

也许我没有采用最好的方法。这是我想要获得的最终结果: enter image description here有什么想法吗?

最佳答案

您是否尝试过使用 ValueAnimator ?这是为管理类似于您的动画而设计的。它将为您处理线程和帧率。

// Create a new value animator that will use the range 0 to 1
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);

// It will take 5000ms for the animator to go from 0 to 1
animator.setDuration(5000);

// Callback that executes on animation steps.
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = ((Float) (animation.getAnimatedValue())).floatValue();

Log.d("ValueAnimator", "value=" + value);

// Here you can now translate or redraw your view
// You need to map 'value' to your animation in regards to time
// eg) mDigitY = value; invalidate();
}
});

您也可以通过使用 OvershootInterpolator 在动画结束时实现弹性效果。 .

animator.setInterpolator(new OvershootInterpolator());

关于android - 在 Android 的自定义 View 中模拟动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com