问题描述
我在将一个补丁图片从网址加载到视图中作为视图的背景时出现问题。
我可以从资源中加载9个补丁图片,效果很好。
我为Picasso设定目标如下:
view.setTag( new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap,LoadedFrom from){
Log.d(LOG,bitmap.getWidth()+ + bitmap.getHeight());
BitmapDrawable bitmapDrawable = new BitmapDrawable(activity.getResources(),bitmap);
byte [] ninePatchChunk = bitmap.getNinePatchChunk();
if(NinePatch.isNinePatchChunk(ninePatchChunk)){
view.setBackground(new NinePatchDrawable(activity.getResources(),bitmap,ninePatchChunk,new Rect(),null));
} else {
view.setBackground(bitmapDrawable);
}
}
}
此功能(从图像加载图像) ssets)工作正常:
Picasso.with(activity)
.load(R.drawable.nine_patch_button)
.into(view.getTag()); //view.getTag()是目标
但我需要从互联网上下载背景图片。
Picasso.with(activity)
.load(uri_to_nine_patch_button)
.into(view.getTag( )); //view.getTag()是目标
在第二种情况下,图像被拉伸而不显示作为一个九贴片图像。当我从URI加载图像时,日志输出将始终相同(41,28),但是当我从资源加载图像时,日志输出因设备而异(108,75和38,27)。 / p>
在第一种具有相同输出的情况下,bitmap.getNinePatchChunk()为空,图像拉伸,无效。
任何解决方案的想法?
祝你好运
我的解决方案是使用此课程:
I have a problem loading nine patch images from url into a view as the view's background.
I can load nine patch images from resources which works fine.
I set the target for Picasso as follows:
view.setTag(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
Log.d("LOG", bitmap.getWidth() + " " + bitmap.getHeight());
BitmapDrawable bitmapDrawable = new BitmapDrawable(activity.getResources(), bitmap);
byte[] ninePatchChunk = bitmap.getNinePatchChunk();
if (NinePatch.isNinePatchChunk(ninePatchChunk)) {
view.setBackground(new NinePatchDrawable(activity.getResources(), bitmap, ninePatchChunk, new Rect(), null));
} else {
view.setBackground(bitmapDrawable);
}
}
}
This function (loading image from assets) works fine:
Picasso.with(activity)
.load(R.drawable.nine_patch_button)
.into(view.getTag()); //view.getTag() is the target
But I need to download the background image from internet.
Picasso.with(activity)
.load(uri_to_nine_patch_button)
.into(view.getTag()); //view.getTag() is the target
In the second case the image is stretched and not displayed as a nine-patch-image. When I load an image from URI, the log output will always be the same (41, 28), but when I load the image from assets, the log output differs from device to device (108, 75 and 38, 27).
In the first case with same output bitmap.getNinePatchChunk() is null, image stretched, nothing works.
Any ideas for solution?
Best regards
My solution was using this class: https://gist.github.com/knight9999/86bec38071a9e0a781ee
这篇关于来自url的Android Picasso九补丁图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!