嘿我在我的Android应用程序中将谷歌地图制作者定制为图像,它工作正常,但目前加载标记需要时间,因为200左右有很多标记。实际上,我从后端获取图像(解析) .com)然后裁剪它以适合标记形状。
有没有办法更快地加载标记?
View marker2 = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.marker, null);
ImageView numTxt = (ImageView) marker2.findViewById(R.id.num_txt);
Bitmap bitmap1 = getBitmap(url1, MapActivity.this);
numTxt.setImageBitmap(bitmap1);
markerOpts = markerOpts.title("Company").snippet("branch").icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapActivity.this, marker2)));
public static Bitmap getBitmap(String url2, Context context) {
FileCache fileCache = new FileCache(context);
MemoryCache memoryCache = new MemoryCache();
File f = fileCache.getFile(url2);
//from SD cache
//CHECK : if trying to decode file which not exist in cache return null
Bitmap b = decodeFile(f);
if (b != null)
return b;
// Download image file from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url2);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
// Constructs a new FileOutputStream that writes to file
// if file not exist then it will create file
OutputStream os = new FileOutputStream(f);
// See Utils class CopyStream method
// It will each pixel from input stream and
// write pixels to output stream (file)
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
//Now file created and going to resize file with defined height
// Decodes image and scales it to reduce memory consumption
b = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

答案 0 :(得分:0)
是的,有。你肯定知道你需要这些照片,所以要以一种不突兀的方式做到这一点。在初始化时将它们加载到背景一次。加载一次后,只需在本地环境中使用它们即可。所以,改进:
答案 1 :(得分:0)
您可以使用图片下载库Picasso。 它将处理图像下载和缓存,内存使用,您可以将图像裁剪为您需要的任何大小。如果您多次加载相同的图像(您可能会做什么)毕加索将使用缓存图像而不是每次下载它,这样可以加快所有图像下载过程。
要使用此库,您需要在Android Studio中项目的build.gradle
文件夹的app
文件的依赖关系树中声明它,所以它看起来像这样:
dependencies {
//some other dependencies
//...
compile 'com.squareup.picasso:picasso:2.5.2'//add this line
}
然后在代码中使用它:
Picasso.with(MapActivity.this).load(url1).into(numTxt);