使用 Glide 将图像加载到 Google 地图标记
Load image with Glide to Google Maps Marker
我有一个问题。我使用 Glide 3.8.0.
我需要从服务器下载图像并将其放入 google 地图上的标记。
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());
}
});
可以用这个方法!
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 3.8.0.
我需要从服务器下载图像并将其放入 google 地图上的标记。
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());
}
});
可以用这个方法!
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;
}