旋转图像会使ui变慢

时间:2012-03-23 10:26:12

标签: android image animation rotation

我正在尝试实施一种速度计。我得到关于每分钟轮次,通过蓝牙增强和加载引擎的信息,我试图在屏幕上显示它们3个箭头女巫应该指向正确的方向。我试图使用旋转动画evry时间我得到数据(10-100ms)来设置箭头。但那使我的ui极度缓慢。按钮点击500ms即可做出反应。 Doese有人知道如何让它更好地工作吗?

源代码:

public void setTacho()
{


//rotate Tachonadel
Rpmcurrentdegree=Rpmcurrentdegree+Rpmdegree;
Rpmdegree=((rpms-lastrpm)*RPMtoDegree);

RpmAnim=new RotateAnimation((float)Rpmcurrentdegree, (float)Rpmdegree,       ivNadel.getWidth()/2, ivNadel.getHeight()/2);
RpmAnim.setFillEnabled(true);
RpmAnim.setFillAfter(true);
ivNadel.setAnimation(RpmAnim);
RpmAnim.start();


//rotate Boostbalken
currentBoostDegree=currentBoostDegree+BoostDegree;
BoostDegree=(boost-lastBoost)*BOOSTtoDegree;

//rotate Loadbalken
currentLoadDegree=currentLoadDegree+LoadDegree;
LoadDegree=(load-lastLoad)*LOADtoDegree;


BoostAnim=new RotateAnimation((float)-currentBoostDegree, (float)-BoostDegree,     ivBoost.getWidth()/2, ivBoost.getHeight()/2);
BoostAnim.setFillEnabled(true);
BoostAnim.setFillAfter(true);
ivBoost.setAnimation(BoostAnim);
BoostAnim.start();

LoadAnim=new RotateAnimation((float)currentLoadDegree, (float)LoadDegree,     ivLoad.getWidth()/2, ivLoad.getHeight()/2);
LoadAnim.setFillEnabled(true);
LoadAnim.setFillAfter(true);
ivLoad.setAnimation(LoadAnim);
LoadAnim.start();

}

当我尝试仅在值已经改变时进行旋转时,它仅在它们改变时起作用,但如果它们不是箭头则跳回到零位置。 isnt setfillafter告诉图像它应该保持新的位置?

代码:

public void setTacho()

{
//rotate Tachonadel
Rpmcurrentdegree=Rpmcurrentdegree+Rpmdegree;
Rpmdegree=((rpms-lastrpm)*RPMtoDegree);


if(Rpmdegree!=0)
{
RpmAnim=new RotateAnimation((float)Rpmcurrentdegree, (float)Rpmdegree,     ivNadel.getWidth()/2, ivNadel.getHeight()/2);
RpmAnim.setFillEnabled(true);
RpmAnim.setFillAfter(true);
ivNadel.setAnimation(RpmAnim);
RpmAnim.start();
}

//rotate Boostbalken
currentBoostDegree=currentBoostDegree+BoostDegree;
BoostDegree=(boost-lastBoost)*BOOSTtoDegree;
//rotate Loadbalken
currentLoadDegree=currentLoadDegree+LoadDegree;
LoadDegree=(load-lastLoad)*LOADtoDegree;

if(BoostDegree!=0)
{
BoostAnim=new RotateAnimation((float)-currentBoostDegree, (float)-BoostDegree,     ivBoost.getWidth()/2, ivBoost.getHeight()/2);
BoostAnim.setFillEnabled(true);
BoostAnim.setFillAfter(true);
ivBoost.setAnimation(BoostAnim);
BoostAnim.start();
}




if(LoadDegree!=0)
{
LoadAnim=new RotateAnimation((float)currentLoadDegree, (float)LoadDegree,     ivLoad.getWidth()/2, ivLoad.getHeight()/2);
LoadAnim.setFillEnabled(true);
LoadAnim.setFillAfter(true);
ivLoad.setAnimation(LoadAnim);
LoadAnim.start();
}
}

我不明白=(

thx 4 help

编辑:

调用回调的蓝牙线程的一部分

while (run) {
                try {

                    bytes = mmInStream.read(buffer);

                    if (connection.btCallback != null)
                    {
                            connection.btCallback.getData(buffer,bytes);
                    }

                } catch (IOException e) {

                    break;
                }

蓝牙线程的回调方法:

public void getData(byte[] bytes, int len)
        {
           setTacho(); 
        }

1 个答案:

答案 0 :(得分:0)

在单独的线程中运行您的旋转代码,因此您可以将动画设置为ImageViews,如下所示:

//for example
ivNadel.post(new Runnable() {   
    @Override
    public void run() {
        ivNadel.setAnimation(RpmAnim);
    }
});

看看它对你有帮助