使用 Glide 将图像加载到 Google Maps Marker
Posted
技术标签:
【中文标题】使用 Glide 将图像加载到 Google Maps Marker【英文标题】:Load image with Glide to Google Maps Marker 【发布时间】:2018-08-01 02:15:20 【问题描述】:我有一个问题。我使用 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 相同(默认徽标在不同设备上看起来相同)
【问题讨论】:
或许能帮到你futurestud.io/tutorials/glide-image-resizing-scaling 不幸的是,没有,据我了解,Glide 不适用于 dp,覆盖仅适用于像素,不适用于 dp 您可以通过issuetracker.google.com/issues/…向 Google 报告此问题 【参考方案1】: 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;
【讨论】:
【参考方案2】:决定重新创建位图
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());
);
【讨论】:
【参考方案3】:你可以用这个方法!
Glide.with(getApplicationContext()).asBitmap()
.load(dataSnapshot.child("dpURL").getValue(String.class))
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition)
mMap.addMarker(new MarkerOptions().position(origin).title(pno).icon(BitmapDescriptorFactory.fromBitmap(bitmappros(resource))));
if(firstload)
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin,16));
firstload = false;
else
mMap.animateCamera(CameraUpdateFactory.newLatLng(origin));
);
用于bitmappros功能!
private Bitmap bitmappros(Bitmap res)
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
markerImage = marker.findViewById(R.id.user_dp);
markerImage.setImageBitmap(res);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.setLayoutParams(new ViewGroup.LayoutParams(52, ViewGroup.LayoutParams.WRAP_CONTENT));
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap2 = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap2);
marker.draw(canvas);
return bitmap2;
【讨论】:
以上是关于使用 Glide 将图像加载到 Google Maps Marker的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Firebase 中的 Glide 仅加载部分图像?
Android Glide 和 Firebase 存储:将连续图像从 Firebase 存储加载到 Imageview - 闪烁