我可以把毕加索的照片加载到动作栏吗



我一直在广泛使用Picasso,通过互联网将图像检索到我的应用程序中。现在,我遇到了一种情况,需要在操作栏中检索一个小图像(比如标题文本旁边的徽标)。

有可能和毕加索一起做这件事吗?如果是,我该怎么做?

我找到了一个使用Picasso的Target类并且不需要自定义Action Bar的解决方案。

final ActionBar ab = getSupportActionBar();
Picasso.with(this)
       .load(imageURL)
       .into(new Target()
       {
           @Override
           public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
           {
               Drawable d = new BitmapDrawable(getResources(), bitmap);
               ab.setIcon(d);
               ab.setDisplayShowHomeEnabled(true);
               ab.setDisplayHomeAsUpEnabled(true);
           }
           @Override
           public void onBitmapFailed(Drawable errorDrawable)
           {
           }
           @Override
           public void onPrepareLoad(Drawable placeHolderDrawable)
           {
           }
       });

您可以像加载任何其他毕加索图像一样加载图片,但还有一个额外的步骤是添加自定义动作栏。类似于:

    final View actionBarLayout = getLayoutInflater().inflate(R.layout.custom_action_bar, null);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setCustomView(actionBarLayout);

然后

 myIcon = (ImageView) actionBarLayout.findViewById(R.id.myIcon);
 Picasso.with(this).load("http://myimage.png").into(myIcon);

最新更新