更新:我切换到Glide,现在可以正常使用
我有一个NavigationView(抽屉),以编程方式创建MenuItems。 用Picasso加载它们的图标是否可行? 我尝试过,但它没有用。 我尝试使用毕加索的目标。
答案 0 :(得分:0)
我们必须通过ID获取'NavigationView',然后使用'NavigationView'对象应用'picasso'图像。
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);
答案 1 :(得分:0)
我最近找到了一种用 kotlin 语言用毕加索做到这一点的方法
//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'