当前位置: 代码迷 >> Android >> android marker zoom with 地图
  详细解决方案

android marker zoom with 地图

热度:459   发布时间:2016-05-01 15:59:57.0
android marker zoom with map

可以通过android.graphics.Matrix 中 Martix来设定图片的大小。
问题1:如何保存放缩后的图片?
问题2:如何根据google map 的放缩来设置marker的放缩级别?
带着这两个问题,我们来看下到底应该怎么去做。现在android上还找不到如何放缩marker的一些资料。所以只好自己写下。
对于图片的放缩,由于marker标记的时候使用的是drawable,而martix 是对bitmap的设置,所以我们必须对drawable和bitmap进行转换。
这个很简单只需一行代码:Drawable marker = new BitmapDrawable(resizeBmp);//resizeBmp 是Bitmap 类型
然后对于放缩的级别,可以自己定义,我在zoom out 的时候设置图片的放大级别是以1.1倍的倍数增大,当然你可以自己去设置。而缩小的时候,我是以0.5倍缩小,这样做主要是因为我一开始设置的图片是在zoom=11的时候,所以如果以2倍的速度增加的话,那么图片就会很大很难看。还有一个问题就是,当图片放缩的很小的时候,在进行matrx设置的时候会有问题,会提示图片的width跟height不能小于0,我在图片的width<0.01(或者height<0.01)的时候就返回了,不进行放缩了,或者直接把图片标记去掉。
下面给出代码:
//scale是当前地图的级别-11,因为我初始化的时候地图级别设置为11,所以在这个基础上进行放缩
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.ball_green);
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
float scaleWidth = (float) 0.2;
float scaleHeight = (float) 0.2;
if (scale > 0) {//放大时1.1倍放大
// scaleWidth = (float)(bmpWidth /Math.pow(2,scale));
scale=scale*1.1;
scaleWidth=(float)scale;
scaleHeight=(float)scale;
// scaleHeight = (float) (bmpHeight /Math.pow(2,scale));
} else if (scale < 0) {//0.5倍缩小
scale=Math.abs(scale);
double a=Math.abs(0.5/scale);;
for(int i=0;i<scale-1;i++)
{
a=Math.abs(a/scale);
}
scaleWidth=(float) (scale*a);
scaleHeight=(float)(scale*a);
System.out.println(scaleWidth);
System.out.println(scaleHeight);
} else {//回到zoom=11时恢复原来图片大小
scaleWidth = (float) (1);
scaleHeight = (float) (1);
}
Matrix matrix = new Matrix();
matrix.setScale(scaleWidth, scaleHeight);
String str;
if(scaleWidth<0.01)
return;
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
matrix, true);
Drawable marker = new BitmapDrawable(resizeBmp);// 转换bitmap to drawable
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());//
//然后再对地图进行标记,标记的时候用marker进行标记就可以,标记的方法可以看我的另外一篇文章。
看下图片效果:
开始时:
缩小1次后:
看下缩放更小的情况下:

放大2次后:

  相关解决方案