hak*_*kim 20 android imageview picasso
在Android KitKat(Nexus 7)中显示透明图像时出现问题,在nexus 4(KitKat)和其他以前的Android操作系统中都可以,这里的图像:

和ImageView布局:
<ImageView
android:id="@+id/avatar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="21dp"
android:padding="3dp"
android:src="@drawable/icon_button_profile_new"
android:tag="@string/avatar" />
Run Code Online (Sandbox Code Playgroud)
这里是在Nexus 7(Android 4.4)上运行时的屏幕截图

另外,我使用Picasso从URL下载和缓存图像.
经过一些试验:首先我尝试使用图像作为资源可绘制并且它仍然发生(图像的透明部分变为黑色),其次我将图像转换为png图像并且它是工作的,所以问题在于文件类型(gif ).因为在我的真实应用程序中从服务器获取的图像我无法像png格式一样请求图像,我使用此链接的解决方案:Android ImageView中的透明GIF
因为我使用毕加索,我只使用目标从图像头像中删除黑色,这样就很容易只显示一个图像(就像在我的问题中一样):
target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if (Build.VERSION.SDK_INT == 19/*Build.VERSION_CODES.KITKAT*/){
Bitmap editedavatar = AndroidUtils.eraseColor(bitmap, -16777216);
avatar.setImageBitmap(editedavatar);
}
}
@Override
public void onBitmapFailed(Drawable arg0) {
avatar.setImageResource(R.drawable.ic_profile_default);
Run Code Online (Sandbox Code Playgroud)
其中擦除颜色是静态方法
public static Bitmap eraseColor(Bitmap src, int color) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap b = src.copy(Config.ARGB_8888, true);
b.setHasAlpha(true);
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
if (pixels[i] == color) {
pixels[i] = 0;
}
}
b.setPixels(pixels, 0, width, 0, 0, width, height);
return b;
}
Run Code Online (Sandbox Code Playgroud)
但是因为我使用Picasso在列表视图中显示图像,所以我在ViewHolder中使用了Target,它到目前为止工作得非常好.
| 归档时间: |
|
| 查看次数: |
19627 次 |
| 最近记录: |