绘制自定义形状

时间:2015-09-07 14:04:00

标签: android

我想在谷歌地图中显示一个自定义标记,当某些特定事件发生时,标记的颜色会动态变化。所以我想知道哪个是绘制形状的最佳方式,如地图标记{{3 }}

3 个答案:

答案 0 :(得分:2)

我创建了一个自定义视图,可以像默认标记一样绘制形状。 这是代码:

public class CustomView extends View {
private Paint paint;
private RectF oval;
private Path fillPath;

public CustomView(Context context) {
    super(context);
    init();
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();

}

@Override
protected void onDraw(Canvas canvas) {

    oval.bottom = getHeight();
    oval.left = 0;
    oval.right = getWidth();
    oval.top = 0;
    // canvas.drawRect(oval, paint);
    canvas.drawArc(oval, 0, -180, true, paint);

    fillPath.moveTo(0, getHeight() / 2); // Your origin point
    fillPath.lineTo(getWidth(), getHeight() / 2); // First point
    // Repeat above line for all points on your line graph
    fillPath.lineTo(getWidth() / 2, getHeight()); // Final point
    fillPath.lineTo(0, getHeight() / 2); // Same origin point
    canvas.drawPath(fillPath, paint);

}

private void init() {
    android.view.ViewGroup.LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    oval = new RectF();
    fillPath = new Path();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    setMeasuredDimension(width, height);
    Log.e("H" + width, "W" + height);

}

  }

希望这对你有所帮助。

答案 1 :(得分:1)

@sijeesh:是。您可以设置自定义标记(图片应该很小)。

标记表示地图上的单个位置。 您可以通过更改默认颜色或使用自定义图像替换标记图标来自定义标记

首先导入以下类:

import com.google.android.gms.maps.model.MarkerOptions;

现在您可以这样设置自定义标记

MarkerOptions custommarker = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.your_custom_icon));

更多信息请查看Android Custom marker with ImageView

我的建议

请关注官方文件。我希望它会对您有所帮助。

  

https://developers.google.com/maps/documentation/android-api/marker

答案 2 :(得分:0)

如果您只需要更改标记的颜色,最好将标记图标设为简单图像,并在需要时更改其着色颜色。

Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.ic_brush_black_24dp);
        Bitmap colored = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(colored);
        canvas.drawBitmap(original, 0, 0, null);
        canvas.drawColor(Color.RED, PorterDuff.Mode.SRC_ATOP);
        marker.setIcon(BitmapDescriptorFactory.fromBitmap(colored));