获取菜单项动画菜单项、动画

2023-09-12 03:34:25 作者:因为太想念

我试图动画菜单项,该项活动时加载,在我的动作条(实际上ActionBarSherlock)。在code我有工作在创建活动是第一次,但是任何一次活动又打电话,我得到一个NullReference异常的loadingItem,因为 onCreateOptionsMenu 是在的onCreate 调用。我试图用在prepareOptionsMenu 但同样的事情。

 公共类MyActivity扩展SherlockActivity {
    私人菜单项loadingItem;

    @覆盖
    公共无效的onCreate(最终捆绑冰柱)
    {
        最后LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        最后ImageView的ivRefresh =(ImageView的)inflater.inflate(R.layout.refresh_view,NULL);

        最终的动画旋转= AnimationUtils.loadAnimation(这一点,R.anim.refresh);
        ivRefresh.startAnimation(旋转);
        loadingItem.setActionView(ivRefresh);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getSupportMenuInflater()膨胀(R.menu.my_menu,菜单);
        loadingItem = menu.findItem(R.id.loading);
        返回super.onCreateOptionsMenu(菜单);
    }
}
 

my_menu.xml

 < XML版本=1.0编码=UTF-8&GT?;

<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目机器人:ID =@ + ID /加载
          机器人:图标=@可绘制/加载
          机器人:showAsAction =总是
    />

< /菜单>
 

更新:

如何给Win10右键增加获得管理员权限菜单项功能

这最终什么,我试图完成。我有一个web视图和我想说明一个加载图标,直到web视图已完成加载:

  @覆盖
    公共无效的onCreate(最终捆绑冰柱)
    {
        的WebView web视图=(web视图)findViewById(R.id.webview);
        。webView.getSettings()setSupportZoom(真正的);
        。webView.getSettings()setBuiltInZoomControls(真正的);

        webView.loadUrl(http://www.google.com);

        webView.setWebViewClient(新WebBrowserClient());

        最后LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        最后ImageView的ivRefresh =(ImageView的)inflater.inflate(R.layout.refresh_view,NULL);

        最终的动画旋转= AnimationUtils.loadAnimation(这一点,R.anim.refresh);
        ivRefresh.startAnimation(旋转);
        loadingItem.setActionView(ivRefresh);

        webView.setWebChromeClient(新WebChromeClient(){
            公共无效onProgressChanged(web视图来看,INT进度){

                如果(isFinishing()及!&安培;进步== 100安培;&安培; loadingItem = NULL和放大器;!&安培;!loadingItem.getActionView()= NULL)
                {
                    。loadingItem.getActionView()clearAnimation();
                    loadingItem.setActionView(空);
                }
            }
        });
    }
 

解决方案

onCreateOptionsMenu 的onCreate 将永远被称为。事实上,的onCreate 将的总是是创建一个活动时调用的第一个方法。

您应该或者你使用它,或者把它声明为静态,这样的引用不会被删除时,该活动被销毁(前指定 loadingItem 变量,它仍可能发生,在这种情况下,我建议第一个选项)。

为什么不设置 onCreateOptionsMenu

动画

I'm trying to animate a menu item, while the activity is loading, in my ActionBar (actually ActionBarSherlock). The code I have works the first time the activity is created, but any time the activity is called again, I get a NullReference exception on "loadingItem" because onCreateOptionsMenu is called after onCreate. I tried using onPrepareOptionsMenu but the same thing.

public class MyActivity extends SherlockActivity  {
    private MenuItem loadingItem;

    @Override
    public void onCreate(final Bundle icicle) 
    {
        final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);

        final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh);
        ivRefresh.startAnimation(rotation);
        loadingItem.setActionView(ivRefresh);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.my_menu, menu);
        loadingItem = menu.findItem(R.id.loading);
        return super.onCreateOptionsMenu(menu);
    }
}

my_menu.xml

<?xml version="1.0" encoding="UTF-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/loading" 
          android:icon="@drawable/loading"
          android:showAsAction="always"          
    />

</menu>

Update:

This ultimately what I'm trying to accomplish. I have a WebView and I want to show a loading icon until the WebView has finished loading:

    @Override
    public void onCreate(final Bundle icicle) 
    {
        WebView webView = (WebView)findViewById(R.id.webview);
        webView.getSettings().setSupportZoom(true);  
        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("http://www.google.com");

        webView.setWebViewClient(new WebBrowserClient());

        final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);

        final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh);
        ivRefresh.startAnimation(rotation);
        loadingItem.setActionView(ivRefresh);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {

                if (!isFinishing() && progress == 100 && loadingItem != null && loadingItem.getActionView() != null)
                {
                    loadingItem.getActionView().clearAnimation();
                    loadingItem.setActionView(null);
                }
            }
        }); 
    }

解决方案

onCreateOptionsMenu will always be called after onCreate. In fact, onCreate will always be the first method called when an activity is created.

You should either assign the loadingItem variable before you use it, or declare it as static so that the reference isn't deleted when the activity is destroyed (which still may happen, in which case I recommend the first option).

Why not set the animation in onCreateOptionsMenu?