我有一个问题。我使用Glide 3.8.0。
我需要从服务器下载图像并将其放在谷歌地图上的标记中。
Glide.with(getBaseActivity())
.load(place.getIconUrl())
.asBitmap()
.fitCenter()
.into(new SimpleTarget<Bitmap>(50,50) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(resource))
.position(place.getLatLng()));
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_default_logo))
.position(place.getLatLng()));
}
});
此外,如果有一些加载错误,我有默认徽标,其大小为50x50。
我在2台设备上测试加载 - nexus 5而没有名称设备(我不知道屏幕分辨率和屏幕尺寸,但尺寸几乎与nexus 5相同)
在不同的设备上,我有不同大小的标记标识,我试验
.into(new SimpleTarget<Bitmap>(50,50)
尺寸
与默认徽标相比,Nexus 5 50x50非常小,75x75比默认值小,150x150与默认相同
没有名称设备:75x75与默认徽标相同,比默认值小50x50
那么,我怎样才能使用Glide在不同的设备上使其与默认徽标50x50相同(默认徽标在不同的设备上看起来相同)
Glide.with(this).load("url").listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
return true
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
callmethod(resource) //pass drawable to your method and set the drawable for marker there
imageSource=resource
return true
}
})
运用
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(convertToBitmap(d,100,100));
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng).icon(icon)
.title(getString(titleResId))
.draggable(true);
也用于从drawable获取位图
public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mutableBitmap);
drawable.setBounds(0, 0, widthPixels, heightPixels);
drawable.draw(canvas);
return mutableBitmap;
}
或者你可以使用
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
决定重新创建位图
Glide.with(getBaseActivity())
.load(place.getIconUrl())
.asBitmap()
.dontTransform()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (50 * scale + 0.5f);
Bitmap bitmap = Bitmap.createScaledBitmap(resource, pixels, pixels, true);
markerImageView.setImageBitmap(bitmap);
addMarker(place.getLatLng());
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
markerImageView.setImageResource(R.drawable.ic_marker_default_logo);
addMarker(place.getLatLng());
}
});