android 百度地圖 marker設定忽明忽暗閃爍點
阿新 • • 發佈:2019-01-08
說下思路
百度地圖設定marker的時候可以這是icon和icons
思路就是設定幾個不同透明度的相同的圖片的bitmap作為icons
話不多說 直接上碼
1。這是核心程式碼
//閃爍點
bb是獲取的image的bitmap bitmaps = makeRoundCorner(bb); Bitmap sbitmap = zoomImage(bitmaps, 90, 90); BitmapDescriptor bitmap1 = BitmapDescriptorFactory.fromBitmap(sbitmap); BitmapDescriptor bitmap2 = BitmapDescriptorFactory.fromBitmap(getAlplaBitmap(sbitmap,75)); BitmapDescriptor bitmap3 = BitmapDescriptorFactory.fromBitmap(getAlplaBitmap(sbitmap,50)); BitmapDescriptor bitmap4 = BitmapDescriptorFactory.fromBitmap(getAlplaBitmap(sbitmap,25)); BitmapDescriptor bitmap5 = BitmapDescriptorFactory.fromBitmap(getAlplaBitmap(sbitmap,0)); ArrayList<BitmapDescriptor> list = new ArrayList<>(); list.add(bitmap1); list.add(bitmap2); list.add(bitmap3); list.add(bitmap4); list.add(bitmap5); OverlayOptions options = new MarkerOptions() .position(latLng)//設定marker的位置 .icons(list) //設定marker圖示!!!!!這是重點
.zIndex(5) //設定marker所在層級 .title(i + "") .draggable(true); //設定手勢拖拽 //將marker新增到地圖上 Marker m= (Marker) mBaiduMap.addOverlay(options); markers.add(m);
2.這是對bitmap的處理的幾個方法
/**
*從網路獲取bitmap
**/
@Nullable public static Bitmap GetLocalOrNetBitmap(String url) throws IOException { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(), 10 * 10); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, 10 * 10); copy(in, out); out.flush(); byte[] data = dataStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); data = null; return bitmap; } catch (IOException e) { e.printStackTrace(); return null; } finally { assert in != null; in.close(); assert out != null; out.close(); } } /* * bitmap設定任意透明度 * */ public static Bitmap getAlplaBitmap(Bitmap sourceImg, int number) { int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()]; sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight()); number = number * 255 / 100; for (int i = 0; i < argb.length; i++) { argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF); } sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Bitmap.Config.ARGB_8888); return getRoundedCornerBitmap(sourceImg); } /* * bitmap設定圓角 * */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = Color.RED; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 200; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }