从URL设置MenuItem图标-Android [英] Setting MenuItem icon from URL - Android

查看:100
本文介绍了从URL设置MenuItem图标-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ActionBar中设置一个MenuItem,该菜单项会导致我的应用程序中的用户个人资料页面.我希望将其图标用作他的个人资料照片,并为其提供URL,并可以创建一个BitMap.

I want to set a MenuItem in my ActionBar which leads to the user profile page in my app. I would like the icon for that to be his profile picture for which I have the URL and can create a BitMap out of.

该图像未存储在我的项目文件夹中或本地任何地方,因此我无法从R.drawable中拾取它.

The image isn't stored in my project folder or anywhere locally so I can't pick it up from R.drawable.

有人可以帮助我设置以URL作为MenuItem图标创建的位图吗?感谢您的帮助!

Can somebody help me with setting a bitmap created with the URL as the MenuItem icon? Thanks for the help!

推荐答案

您可以执行以下操作从位图中设置图标:

You could do something like this to set the icon from a bitmap:

myMenuItem.setIcon(new BitmapDrawable(getResources(), myBitmap));

在您的代码中,这看起来像这样:

In your code this would looks a bit like this:

public boolean onCreateOptionsMenu( Menu menu ) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.actionbar, menu );
    userItem = menu.findItem(R.id.userItem);

    Bitmap myBitmap = //get your bitmap
    userItem.setIcon(new BitmapDrawable(getResources(), myBitmap));

    return menu;
}

您需要从URL获取文件,然后首先将其转换为Bitmap.请注意,这将很慢,因为如果在启动应用程序时执行此操作,则用户将必须等到文件下载后才能显示该应用程序.如果您的图标很少更改,建议您将其缓存在设备上,然后重新使用本地存储的副本.

You'll need to get the file from the URL and turn it into a Bitmap first. Note that this will be slow, since if you are doing this when your app is started, the user will have to wait until the file is downloaded before the app will be shown. If your icon changes infrequently, I'd recommend caching it on the device and reusing the locally stored copy.

也请在"此处.

这篇关于从URL设置MenuItem图标-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆