来自文件的工具栏徽标

Toolbar logo from file

我尝试使用以下代码段从文件中添加图像作为工具栏徽标

Picasso.with(toolbar.getContext())
        .load(file)
        .placeholder(R.drawable.ic_action_camera)
        .error(R.drawable.ic_action_camera)
        .transform(new CircleTransform())
        .into(target);

目标是

target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Logger.d("HIYA", "onBitmapLoaded");
        Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
        BitmapDrawable icon = new BitmapDrawable(toolbar.getResources(), b);
        toolbar.setLogo(icon);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Logger.d("HIYA", "onBitmapFailed");
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Logger.d("HIYA", "onPrepareLoad");
    }
};

试试下面的方法

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) {
        }
   });

这对我有用。