引用网友文章:Android进阶:在地图MapView中画一个图标并带阴影效果

看看效果先

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

Paint paint = new Paint();
            Point myScreenCoords = new Point();
            //转换当前地图位置到屏幕坐标点
            mv.getProjection().toPixels(gp1, myScreenCoords);
            paint.setStrokeWidth(1);
            paint.setARGB(255,255,0,0);
            paint.setStyle(Paint.Style.FILL);
            Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon001);
            canvas.save(Canvas.MATRIX_SAVE_FLAG);
            //加载两次图片资源
            Drawable drawable = getResources().getDrawable(R.drawable.icon001);
            //这里调用mutate 做测试
            Drawable drawable1 = getResources().getDrawable(R.drawable.icon001).mutate();
           drawable.setBounds(myScreenCoords.x, myScreenCoords.y, myScreenCoords.x+drawable.getIntrinsicWidth(), myScreenCoords.y+drawable.getIntrinsicHeight());
            drawable1.setBounds(0, 0, drawable1.getIntrinsicWidth(), drawable1.getIntrinsicHeight());
            //位置的调节操作
            boundCenterBottom(drawable);
            drawable.draw(canvas);
            canvas.restore();
            canvas.save(Canvas.MATRIX_SAVE_FLAG);
            //颜色的过滤
            drawable1.setColorFilter(0x7f000000, PorterDuff.Mode.SRC_IN);
            //位移操作
            canvas.translate(myScreenCoords.x, myScreenCoords.y);
            //倾斜操作
            canvas.skew(-0.9F, 0.0F);
            //进行缩放
            canvas.scale(1.0F, 0.5F);
            boundCenterBottom(drawable1);
            drawable1.draw(canvas);
            //这里清除颜色过滤
            drawable1.clearColorFilter();
            canvas.restore();

 

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

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

下面了解一下 mutate()方法

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, 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还要经过位移

中间还有个方法

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