加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用XML在android中绘制自定义形状

发布时间:2020-12-16 23:14:25 所属栏目:百科 来源:网络整理
导读:我正在为 android地图视图添加自定义标记.这是我正在使用它的图像.看起来像放置更多的标记大约100整个应用程序变慢.这是我正在使用的图像. 我计划将此图像绘制为XML中的形状,而不是使用此图像.怎么做? 此外,我正在关注this tutorial以显示自定义标记.这个自
我正在为 android地图视图添加自定义标记.这是我正在使用它的图像.看起来像放置更多的标记大约100整个应用程序变慢.这是我正在使用的图像.

enter image description here

我计划将此图像绘制为XML中的形状,而不是使用此图像.怎么做?

此外,我正在关注this tutorial以显示自定义标记.这个自定义绘图是否延迟了应用程序?

什么是最好的方法呢?

解决方法

这是可能的,但看起来没有意义.因为Google Map只需要位图.所以你需要创建位图并借助画布绘制你的形状.

marker.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:width="20dp"
    android:height="20dp"
    android:top="10dp">
    <rotate
        android:fromDegrees="45"
        android:pivotX="75%"
        android:pivotY="40%">
        <shape>
            <solid android:color="#fe696d" />
        </shape>
    </rotate>
</item>

<item
    android:width="@dimen/marker_width"
    android:height="@dimen/marker_height"
    android:bottom="8dp">
    <shape>
        <solid android:color="#4b4b4b" />
        <corners
            android:topLeftRadius="@dimen/marker_corner_radius"
            android:topRightRadius="4dp" />
    </shape>

</item>


<item
    android:width="32dp"
    android:height="26dp"
    android:bottom="4dp"
    android:top="16dp">
    <shape>
        <solid android:color="#fe696d" />
        <corners
            android:bottomLeftRadius="@dimen/marker_corner_radius"
            android:bottomRightRadius="@dimen/marker_corner_radius" />
    </shape>

</item>

dimen.xml

<resources>
    <dimen name="marker_corner_radius">4dp</dimen>
    <dimen name="marker_height">40dp</dimen>
    <dimen name="marker_width">32dp</dimen>
</resources>

将形状转换为位图的代码的一部分(来自使用Map的片段/活动)

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.marker,null);
    Canvas canvas = new Canvas();
    int width = getResources().getDimensionPixelOffset(R.dimen.marker_width);
    int height = getResources().getDimensionPixelOffset(R.dimen.marker_height);
    drawable.setBounds(0,width,height);
    Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.draw(canvas);
    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
    LatLng sydney = new LatLng(-34,151);
    mMap.addMarker(new MarkerOptions().icon(bitmapDescriptor).position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

结果将是下一个

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读