这篇文章主要围绕Android:如何在googlemapAPIandroid中将imageView设置为标记?和怎么在googlemap添加标记展开,旨在为您提供一份详细的参考资料。我们将全面介绍An
这篇文章主要围绕Android:如何在google map API android中将imageView设置为标记?和怎么在google map添加标记展开,旨在为您提供一份详细的参考资料。我们将全面介绍Android:如何在google map API android中将imageView设置为标记?的优缺点,解答怎么在google map添加标记的相关问题,同时也会为您带来Android Glide加载图片文件应用叠加并设置为imageview、Android ImageView设置非int ID、android – Google Maps API v2:如何使标记不可点击?、android – Google Maps Utility:如何从ClusterManager <?>获取所有标记?的实用方法。
本文目录一览:- Android:如何在google map API android中将imageView设置为标记?(怎么在google map添加标记)
- Android Glide加载图片文件应用叠加并设置为imageview
- Android ImageView设置非int ID
- android – Google Maps API v2:如何使标记不可点击?
- android – Google Maps Utility:如何从ClusterManager <?>获取所有标记?
Android:如何在google map API android中将imageView设置为标记?(怎么在google map添加标记)
到目前为止,我正在使用drawable来填充我的地图上的标记.现在我想知道如果我能在地图中显示自定义imageview作为标记会很酷.
到现在为止我喜欢这样做
itemized= new Itemazide(drawable,mContext);
我希望实现类似的目标
话虽这么说,取决于你想要实现的目标,可能会破解mapview-baloon库来实现类似的效果.
编辑:
既然你已经展示了你想要实现的目标,我认为你应该覆盖OverlayItem中的draw()并在这个方法中膨胀你的ImageView.但我并没有尝试这样做,所以可能存在一些缺点(我记得在类似的事情上有一个SO线程声称它会中断这个OverlayItem上的所有触摸事件).
Android Glide加载图片文件应用叠加并设置为imageview
我有图像文件(png / jpg).当加载到列表视图时,其中一些我需要覆盖另一个透明图像.我使用以下方法进行此操作:
public Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromresource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawabletoBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
其中BitmapUtils是一个实现类的按位方法的自定义类.
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.media.ThumbnailUtils;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class BitmapUtils {
public static Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromresource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawabletoBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
public static Bitmap drawabletoBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
static Bitmap decodeSampledBitmapFromresource(Resources res, int resId, int reqWidth, int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Compute inSampleSize
options.inSampleSize = computeInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
使用适用于Android(UIL)的Universal Image Loader库,可以通过编写自定义imagedecoder并将其应用于ImageLoaderConfiguration来实现此效果.
问题是,如何使用Glide做到这一点?
谢谢.
解决方法:
对我有用的东西:
我使用以下答案:https://stackoverflow.com/a/33709650/3106975
public void display(final Context context, String mediaPath, final ImageView imageView, final boolean doApplyOverlay){
try{
Glide.with(context)
.load(mediaPath)
.asBitmap()
.placeholder(R.drawable.placeholder_default)
.error(R.drawable.error_default)
.diskCacheStrategy(diskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (doApplyOverlay) {
resource = BitmapUtils.applyOverlay(context, resource, R.drawable.overlay);
}
imageView.setimageBitmap(resource);
}
});
}catch (Exception ex){}
}
因此,要在向用户显示之前加载位图并与其进行交互,您需要实现一个自定义目标并在其中进行交互. Glide提供的SimpleTarget类实现了Target接口并允许这种事情.有关更多信息,您可以访问以下页面的Glide的CustomTargets Wiki页面:https://github.com/bumptech/glide/wiki/Custom-targets
Android ImageView设置非int ID
是否可以使用String设置ImageViews id?
我正在以编程方式添加ImageViews,我想将ID设置为类似于“ IMG12a”
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = X;
params.topMargin = Y;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setimageResource(IMG);
image.setId(?????);
theLayout.addView(image);
解决方法:
setId是不可能的,
您应该使用setTag(Object tag)(可以使用字符串,而无需将其转换为Object).
并使用View.findViewWithTag(Object tag)来获取View并对其进行修改.
如果您需要获取标签-使用getTag().和getTag().toString()以便以字符串形式获取它.
android – Google Maps API v2:如何使标记不可点击?
如何为所有用户交互设置标记透明?
解决方法
如果您确实需要它们作为标记,请在gmaps-api-issues上为MarkerOptions.clickable和Marker.setClickable发布功能请求.
如果可以,请考虑使用其他可视对象,例如:地面叠加层.唯一的问题是它们都与地图一起扩展,与标记不同.最接近的是半径为零且行程宽度为20-50 dp的圆,但这只是一个单色点.
android – Google Maps Utility:如何从ClusterManager <?>获取所有标记?
我尝试了ClusterManager<?> .getMarkerCollection().getMarkers()方法,但它返回空集合.
我在我的应用中使用Google Maps Utility Library.每次屏幕旋转后,我都会创建AsynkTask并在后台线程中从DB读取数据并将项目添加到ClusterManager:
cursor.movetoFirst(); while (!cursor.isAfterLast()) { SomeData row = readSomeDaTarow(cursor); clusterManager.addItem(new ClusterItemImpl(row)); cursor.movetoNext(); }
当AsyncTask完成其工作(即在主线程中)时,我试图从ClusterManager获取所有标记:
clusterManager.cluster(); // cluster manager returns empty collection \|/ markers = clusterManager.getMarkerCollection().getMarkers();
但ClusterManager返回空集合.
可能正好在我调用getMarkers()的时候,ClusterManager还没有在地图上放置标记,稍后会这样做(可能在后台线程中).如果是这样,那我怎么能抓住那一刻呢?
解决方法
背景:让我们首先看一下库代码中ClusterManager.addItem的实现:
public void addItem(T myItem) { this.mAlgorithmlock.writeLock().lock(); try { this.mAlgorithm.addItem(myItem); } finally { this.mAlgorithmlock.writeLock().unlock(); } }
如您所见,当您调用clusterManager.addItem时,ClusterManager会调用this.mAlgorithm.addItem. mAlgorithm是存储项目的地方.现在让我们看一下ClusterManager的默认构造函数:
public ClusterManager(Context context,GoogleMap map,MarkerManager markerManager) { ... this.mAlgorithm = new PreCachingalgorithmDecorator(new NonHierarchicaldistanceBasedAlgorithm()); ... }
mAlgorithm被实例化为包含NonHierarchicaldistanceBasedAlgorithm的PreCachingalgorithmDecorator.不幸的是,由于mAlgorithm被声明为私有,我们无权访问正在添加到算法中的项目.但是,很高兴有一个简单的解决方法!我们只是使用ClusterManager.setAlgorithm实例化mAlgorithm.这允许我们访问算法类.
解决方法:这是您的代码插入了变通方法.
>将此声明与您的类变量一起使用:
private Algorithm<Post> clusterManagerAlgorithm;
>在实例化ClusterManager的位置,之后立即放置:
// Instantiate the cluster manager algorithm as is done in the ClusterManager clusterManagerAlgorithm = new NonHierarchicaldistanceBasedAlgorithm(); // Set this local algorithm in clusterManager clusterManager.setAlgorithm(clusterManagerAlgorithm);
>您可以保留用于将项目插入群集的代码完全相同.
>如果要访问插入的项目,只需使用算法而不是ClusterManager:
Collection<ClusterItemImpl> items = clusterManagerAlgorithm.getItems();
这将返回项目而不是Marker对象,但我相信这正是您所需要的.
关于Android:如何在google map API android中将imageView设置为标记?和怎么在google map添加标记的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android Glide加载图片文件应用叠加并设置为imageview、Android ImageView设置非int ID、android – Google Maps API v2:如何使标记不可点击?、android – Google Maps Utility:如何从ClusterManager <?>获取所有标记?的相关知识,请在本站寻找。
本文标签: