郑州解放路派出所电话:Android进阶:在地图MapView中画一个图标并带阴影效果

来源:百度文库 编辑:中财网 时间:2024/04/29 14:39:50

Android进阶:在地图MapView中画一个图标并带阴影效果

分类:android学习2011-04-02 16:34440人阅读评论(1)收藏举报

看看效果先

 

自己调试了一下午终于搞定了这里来整理一下吧 直接上代码吧

 

 

 

 

view plain
  1. Paint paint = new Paint();  
  2.             Point myScreenCoords = new Point();  
  3.             //转换当前地图位置到屏幕坐标点  
  4.             mv.getProjection().toPixels(gp1, myScreenCoords);  
  5.             paint.setStrokeWidth(1);  
  6.             paint.setARGB(255,255,0,0);  
  7.             paint.setStyle(Paint.Style.FILL);  
  8.             Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon001);  
  9.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  10.             //加载两次图片资源  
  11.             Drawable drawable = getResources().getDrawable(R.drawable.icon001);  
  12.             //这里调用mutate 做测试  
  13.             Drawable drawable1 = getResources().getDrawable(R.drawable.icon001).mutate();  
  14.             drawable.setBounds(myScreenCoords.x, myScreenCoords.y, myScreenCoords.x+drawable.getIntrinsicWidth(), myScreenCoords.y+drawable.getIntrinsicHeight());  
  15.             drawable1.setBounds(0, 0, drawable1.getIntrinsicWidth(), drawable1.getIntrinsicHeight());  
  16.             //位置的调节操作  
  17.             boundCenterBottom(drawable);  
  18.             drawable.draw(canvas);  
  19.             canvas.restore();  
  20.             canvas.save(Canvas.MATRIX_SAVE_FLAG);  
  21.             //颜色的过滤  
  22.             drawable1.setColorFilter(0x7f000000, PorterDuff.Mode.SRC_IN);  
  23.             //位移操作  
  24.             canvas.translate(myScreenCoords.x, myScreenCoords.y);  
  25.             //倾斜操作  
  26.             canvas.skew(-0.9F, 0.0F);  
  27.             //进行缩放  
  28.             canvas.scale(1.0F, 0.5F);  
  29.             boundCenterBottom(drawable1);  
  30.             drawable1.draw(canvas);  
  31.             //这里清除颜色过滤   
  32.             drawable1.clearColorFilter();  
  33.             canvas.restore();  

 

刚开始做的时候老是出现图片本身和阴影的颜色都被过滤了 但是我仅仅是调用了drawable1 的颜色过滤方法

 

后来发现了资源状态共享的问题

 

下面了解一下 mutate()方法

 

Make this drawable mutable. This operation cannot be reversed. A mutabledrawable is guaranteed to not share its state with any other drawable.This is especially useful when you need to modify properties of drawablesloaded from resources. By default, all drawables instances loaded fromthe same resource share a common state; if you modify the state of oneinstance, all the other instances will receive the same modification.Calling this method on a mutable Drawable will have no effect

 

大概就是改变当前资源状态 变为活动的可变的 因为 drawable资源不管加载几次 它的状态是共享的,所以在改变一个对象的状态的时候其它的引用到该资源的对象也会改变 所以让drawable1调用该方法 锁定这个对象调用的资源状态

 

下面clearColorFilter()这个方法也是起到同样的作用 可以解决这个问题

 

反正解决了就行了。。具体有什么不同就不深究了

 

还有注意的就是 setBounds() 两个设置的不同, 因为drawable1还要经过位移

 

中间还有个方法

 

 

view plain
  1. protected  Drawable boundCenterBottom(Drawable balloon) {  
  2.         int markerWidth=balloon.getIntrinsicWidth();  
  3.         int markerHeight=balloon.getIntrinsicHeight();  
  4.           
  5.         Rect srcRect=balloon.getBounds();  
  6.         srcRect.offset(-markerWidth/2, -markerHeight);  
  7.         balloon.setBounds(srcRect);  
  8.         return balloon;  
  9.     }  

 

 

就是移动位置到点坐标的正上方 图标嘛 就是这个坐标的具体位置的解释嘛 不能把它给覆盖了