2017-07-19 35 views
0

对不起,这里有新东西。为什么我的动画没有开始?

的问题是:

我有当应用程序试图从一个RESTful servive取一些数据旋转的可绘制的。

在MainActvity的onCreate()方法中工作正常,但是,当AssycTask类无法获取数据时,会向等待同步的处理程序发送消息,然后调用Handler()。postDelayed()用重置动画和更新gui的方法。 UI已更新,但动画不会再次启动。

一些代码:

OnCreate中()运行OK:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_view); 
    animation.setRepeatCount(1000); 
    animation.setRepeatMode(Animation.RESTART); 
    navHeaderLilCategoria.setAnimation(animation); 
    animation.start(); 

动画/ rotateview:从AssyncTask尝试连接的服务器

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="2000" 
    android:interpolator="@android:anim/linear_interpolator" 
/> 

处理程序接收到消息,其在一个单身人士控制应用程序。如果斜面获取数据时,UI设置为不连接状态,则调度新尝试postDelayed():

public void handleMessage(Message msg) { 
     if(msg.what == ResourceManagerStatusTypes.OPERATION_OK || msg.what == ResourceManagerStatusTypes.GETTING_DONE) 
     { 
      triesToGetVinculoAtivo=0; 
      rootActivity.updateVinculoChanged((Vinculo) msg.obj, false); 
     }else{ 
      triesToGetVinculoAtivo++; 
      new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        rootActivity.updateVinculoChanged(null,true); 
        vinculosManager.getVinculoAtivoPopulated(new HandlerForVinculoAtivoPopulated(false)); 
       } 
      },MILLIS_FOR_EACH_TRY_TO_GET_VINCULO_ATIVO*triesToGetVinculoAtivo); 
      rootActivity.updateVinculoChanged(vinculosManager.getVinculoAtivo(),false); 
     } 
    } 

,更新其为NavigationView的报头内的UI的方法:

public void updateVinculoChanged(Vinculo vinculoAtivo, boolean syncAgain) { 
    navHeaderLilCategoria.clearAnimation(); 
    if(!syncAgain) { 
     if (vinculoAtivo != null) { 
      Imovel currentImovel = vinculoAtivo.getImovel(); 
      if (currentImovel != null) { 
       String categoria = currentImovel.getCategoria(); 
       if (categoria == null || categoria.trim().equals("")) { 
        navHeaderImvCategoria.setImageResource(R.mipmap.ic_error_icon); 
       } else if (categoria.equals(getString(R.string.imovel_categoria_residencial))) { 
        navHeaderImvCategoria.setImageResource(R.mipmap.ic_home); 
       } else if (categoria.equals(getString(R.string.imovel_categoria_comercial))) { 
        navHeaderImvCategoria.setImageResource(R.mipmap.ic_commerce); 
       } else { 
        navHeaderImvCategoria.setImageResource(R.mipmap.ic_industry); 
       } 
       if (vinculoAtivo.getDocType() == Vinculo.CPF) 
        navHeaderTxvDocumento.setText(FrazoUtils.maskToCPF(vinculoAtivo.getDocumento(), this)); 
       else if (vinculoAtivo.getDocType() == Vinculo.CNPJ) { 
        navHeaderTxvDocumento.setText(FrazoUtils.maskToCNPJ(vinculoAtivo.getDocumento(), this)); 
       } else { 
        navHeaderTxvDocumento.setText(vinculoAtivo.getDocumento()); 
       } 
       navHeaderTxvMatricula.setText(String.valueOf(vinculoAtivo.getMatricula())); 
       navHeaderTxvNome.setTextColor(ContextCompat.getColor(this, R.color.white)); 
       navHeaderTxvNome.setText(currentImovel.getNome()); 
       navHeaderTxvNome.setTextColor(ContextCompat.getColor(this, R.color.white)); 
       navHeaderTxvEndereco.setText(FrazoUtils.extractEnderecoFromImovel(currentImovel)); 
       navigationView.getMenu().setGroupEnabled(R.id.nav_menu_grp_com_matricula,true); 
      } else { 
       navHeaderImvCategoria.setImageResource(R.mipmap.ic_no_conection); 
       if (vinculoAtivo.getDocType() == Vinculo.CPF) 
        navHeaderTxvDocumento.setText(FrazoUtils.maskToCPF(vinculoAtivo.getDocumento(), this)); 
       else if (vinculoAtivo.getDocType() == Vinculo.CNPJ) { 
        navHeaderTxvDocumento.setText(FrazoUtils.maskToCNPJ(vinculoAtivo.getDocumento(), this)); 
       } else { 
        navHeaderTxvDocumento.setText(vinculoAtivo.getDocumento()); 
       } 
       navHeaderTxvMatricula.setText(String.valueOf(vinculoAtivo.getMatricula())); 
       navHeaderTxvNome.setTextColor(ContextCompat.getColor(this, R.color.red)); 
       navHeaderTxvNome.setText(getResources().getString(R.string.no_conection)); 
       navHeaderTxvEndereco.setTextColor(ContextCompat.getColor(this, R.color.red)); 
       navHeaderTxvEndereco.setText(getResources().getString(R.string.no_conection)); 
       navigationView.getMenu().setGroupEnabled(R.id.nav_menu_grp_com_matricula,false); 
      } 
     } else { 
      navHeaderImvCategoria.setImageResource(R.mipmap.ic_error_icon); 
      navHeaderTxvDocumento.setText(getResources().getString(R.string.ative_um_vinculo)); 
      navHeaderTxvMatricula.setText(getResources().getString(R.string.sem_dados)); 
      navHeaderTxvNome.setTextColor(ContextCompat.getColor(this, R.color.white)); 
      navHeaderTxvNome.setText(getResources().getString(R.string.sem_dados)); 
      navHeaderTxvEndereco.setTextColor(ContextCompat.getColor(this, R.color.white)); 
      navHeaderTxvEndereco.setText(getResources().getString(R.string.sem_dados)); 
      navigationView.getMenu().setGroupEnabled(R.id.nav_menu_grp_com_matricula,false); 
     } 
    }else 
    { 
     navHeaderImvCategoria.setImageResource(R.mipmap.ic_sync); 
     Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_view); 
     animation.setRepeatCount(1000); 
     animation.setRepeatMode(Animation.RESTART); 
     navHeaderLilCategoria.setAnimation(animation); 
     animation.startNow(); 
     navHeaderTxvDocumento.setText(getResources().getString(R.string.sincronizando)); 
     navHeaderTxvMatricula.setText(getResources().getString(R.string.sincronizando)); 
     navHeaderTxvNome.setTextColor(ContextCompat.getColor(this, R.color.white)); 
     navHeaderTxvNome.setText(getResources().getString(R.string.sincronizando)); 
     navHeaderTxvEndereco.setTextColor(ContextCompat.getColor(this, R.color.white)); 
     navHeaderTxvEndereco.setText(getResources().getString(R.string.sincronizando)); 
     navigationView.getMenu().setGroupEnabled(R.id.nav_menu_grp_com_matricula,false); 
    } 
} 

对不起,我的英语不好,和坏的代码,请帮我...

也许有用的信息: 其上的应用程序只运行一个活动,我只是改变一个布局内的活动,为了澄清,我请拨打以下方法:

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if(id == R.id.nav_menu_vinculos) 
    { 
     if(currentViewId!=R.layout.layout_vinculos) { 
      changeCurrentView(R.layout.layout_vinculos,new VinculosViewController(this)); 
     } 
    } 
    if(id == R.id.nav_2_via) 
    { 
     if(currentViewId!=R.layout.layout_list_vinculos_row) { 
      changeCurrentView(R.layout.layout_list_vinculos_row,null); 
     } 
    } 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

和:

public ViewGroup changeCurrentView(int newViewId, GUIController guiController) 
{ 
    removeGoneInvisible(currentView); 
    TransitionManager.beginDelayedTransition(rootChangeableLayout); 
    rootChangeableLayout.removeView(currentView); 
    currentViewId = newViewId; 
    currentView = (ViewGroup) getLayoutInflater().inflate(newViewId, null); 
    ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
    rootChangeableLayout.addView(currentView, params); 
    if(guiController!=null) { 
     guiController.control(currentView); 
    } 
    rootChangeableLayout.invalidate(); 
    return currentView; 
} 
+0

使用主线程处理程序?可能你试图从不同的线程更新UI? – Debanjan

+0

它的主线程... –

回答

0

设置你的下面的代码的onResume()

Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_view); 
animation.setRepeatCount(1000); 
animation.setRepeatMode(Animation.RESTART); 
navHeaderLilCategoria.setAnimation(animation); 
animation.start(); 
+0

顺便说一句,我从来没有暂停MainActivity或实例化一个新的活力,我只是取代布局中心的活动布局,我不认为这种方法将被称为,但我会尝试。 –

+0

没有工作... –

相关问题