對不起,這裏有新東西。爲什麼我的動畫沒有開始?
的問題是:
我有當應用程序試圖從一個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;
}
使用主線程處理程序?可能你試圖從不同的線程更新UI? – Debanjan
它的主線程... –