简体   繁体   English

Android:生成具有资源图像和颜色的 Drawable

[英]Android: generate Drawable having resource image and Color

I'm using Glide to load image from url and need to have a placeholder which is generated with a transparent pattern stored as a resource and a random background color.我正在使用 Glide 从 url 加载图像,并且需要有一个占位符,该占位符使用存储为资源的透明图案和随机背景颜色生成。 Glide allows using Drawable as a placeholder. Glide 允许使用 Drawable 作为占位符。 I have a transparent drawable resource int resId = R.drawable.placeholder;我有一个透明的可绘制资源int resId = R.drawable.placeholder; and have Color randomColor = generateRandomColor();并有Color randomColor = generateRandomColor(); How can I create a Drawable which is created in combination of resource and background random color, smth.如何创建一个结合资源和背景随机颜色创建的 Drawable,smth。 like Drawable d = resId + randomColor;Drawable d = resId + randomColor; ? ? To have as a result:结果是:

Glide.with(getActivity).load(imageUl)
         .asBitmap().placeholder(d)
         .into(imageView); 

You can use the method setColorFilter of Drawable可以使用 Drawable 的setColorFilter方法

So the code will be like:所以代码将是这样的:

Drawable drawable = getDrawable(resourceId);
drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

Glide.with(getActivity).load(imageUl)
         .asBitmap().placeholder(drawable)
         .into(imageView); 

PorterDuff has a lot of modes , choose the one that's right for you PorterDuff 有很多模式,选择适合你的模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM