2

更新:我切换到 Glide,现在它可以工作了

我有一个以编程方式创建的 MenuItems 的 NavigationView(抽屉)。是否可以用毕加索为他们加载图标?我试过了,但它不起作用。我使用 Picasso 的 Target 进行了尝试。

4

2 回答 2

0

我最近找到了一种用 Kotlin 语言使用 Picasso 的方法


//Implemented Databinding in project so able to
//call views directly with the help of binding object
val menu: Menu = binding.navigationView.menu

//Picasso form com.squreup.picasso.Picasso
Picasso.get().load("""**SOURCE URL**""")
        .placeholder(R.drawable.<placeholder_name>)
        .into(object: Target{ //Target interface from com.squareup.picasso.Target 
            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                println("icon loaded $bitmap")
                menu[0].icon = BitmapDrawable(resources, bitmap)
            }
            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                println("Loading failed... ${e?.message}")
            }
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                println("Loading your icon...")
                menu[0].icon = placeHolderDrawable
            }
        })

对于java会有一点不同的代码


//Implemented Databinding in project so able to
//call views directly with the help of binding object
Menu menu = binding.navigationView.getMenu();

//Picasso form com.squreup.picasso.Picasso
Picasso.get().load("""**SOURCE URL**""")
        .placeholder(R.drawable.<placeholder_name>)
        .into(new Target(){ //Target interface from com.squareup.picasso.Target 
            @Override
            fun onBitmapLoaded(@Nullable Bitmap bitmap, @Nullable Picasso.LoadedFrom from) {
                System.out.println("icon loaded " + bitmap.toString())
                menu[0].setIcon(new BitmapDrawable(getResources(), bitmap))
            }
            @Override
            fun onBitmapFailed(@Nullable Exception e, @Nullable Drawable errorDrawable) {
                System.out.println("Loading failed... " + e.getMessage())
            }
            @Override
            fun onPrepareLoad(@nullable Drawable placeHolderDrawable) {
                System.out.println("Loading your icon...")
                menu[0].setIcon(placeHolderDrawable)
            }
        })

注意:请注意在您的应用级别 build.gradle 文件中实现 Picasso 依赖项

//latest_version is 2.71828 at the time of posting this answer
implementation 'com.squareup.picasso:picasso:$latest_version'
于 2021-03-12T11:50:32.480 回答
0

我们必须通过 ID 获取“NavigationView”,然后使用“NavigationView”对象应用“毕加索”图像。

    NavigationView navView =(NavigationView)findViewById(R.id.nav_view);

    navView.setNavigationItemSelectedListener(this);
    ImageView header=(ImageView)navView.getHeaderView(0).findViewById(R.id.event_logo);
    Picasso.with(this).load( "https://d30y9cdsu7xlg0.cloudfront.net/png/17241-200.png").into(header);
于 2016-11-28T13:55:02.057 回答