【问题标题】:Android Google maps transparencyAndroid 谷歌地图透明度
【发布时间】:2015-12-07 03:08:17
【问题描述】:

我正在开发地图应用程序。我想在 MapView 下显示一些视图。 我用的是方法:

    mapView.setAlpha(0.5f);

mapView 没有任何反应。当我尝试将此方法应用于我的按钮时,它可以正常工作,即使将其应用于 mapView 的父视图也会使除 mapView 之外的每个子视图都是透明的。

我们怎样才能使 mapView 透明?我在这里查看了其他问题,但似乎找不到解决方案。

mapView.getMap() 中是否有一些特殊的方法可以用来使其透明?

【问题讨论】:

  • 我不认为你可以用 mapview 来实现这一点......即使是一个 ViewGroup,谷歌也为它添加了很多东西。尝试使用其他地图库或重新考虑您的 UI/UX。顺便说一句......你为什么想要这样的东西,如果可以实现的话,看起来真的很糟糕。
  • ^ 他说的话,反正看起来很糟糕。避免像瘟疫一样的想法。而是让顶部的东西透明(按钮,图标等......)
  • 我知道它看起来很糟糕,但是你将如何实现它呢?谷歌添加了什么让它总是不透明的?还有什么其他的地图库是最好的选择? @MarianoZorrilla
  • 这里有一个库列表:wiki.openstreetmap.org/wiki/Android 但是,再次...尽量避免使用那种 UI。

标签: android google-maps google-maps-android-api-2 android-mapview transparent


【解决方案1】:

您可以使用View.setAlpha() (documentation) 设置地图的透明度。这是一个工作示例:

activity_maps.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="THIS TEXT IS VISIBLE"
        android:textSize="24sp"
        android:gravity="center"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Marker m = googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        m.showInfoWindow();

        View v = getSupportFragmentManager().findFragmentById(R.id.map).getView();
        v.setAlpha(0.5f); // Change this value to set the desired alpha
    }
}

结果如下所示(如您所见,由于地图的透明度,文本可见):

【讨论】:

    【解决方案2】:

    您可以通过 MapView 参考的 setAlpha(float) 方法执行此操作,如下所示:

    mMapView = (MapView) findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this);
    mMapView.setAlpha(0.5f);
    

    如果您使用的是 MapFragment,那么只需将 setAlpha(float) 用于您的 MapFragment。

    【讨论】:

      【解决方案3】:

      使用 Framelayout 将 2 个视图(CameraView 和 MapView)放在它们自己之上。 并使用 xml 属性 alpha 使 MapView 透明。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        相关资源
        最近更新 更多