为什么我的动画没有开始?

时间:2017-07-19 13:25:55

标签: android multithreading animation view handler

对不起伙计们,新来的。

问题是:

我有一个drawable,当应用程序尝试从RESTFul服务器获取一些数据时,它会旋转。

在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:

<?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"
/>

处理程序从AssyncTask接收消息,尝试连接服务器,它在控制应用程序的单例中。如果无法获取数据,请将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;
}

1 个答案:

答案 0 :(得分:0)

将以下代码设置为 onResume()

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