SurfaceView in Android Wear (API 21)

∥☆過路亽.° 提交于 2019-12-11 02:17:49

问题


I want to use a SurfaceView in an Android Wear project in order to render a parallax background (see attached screenie).

The problem: the SurfaceView disappears after about 1000ms but the car remains so there is a pink car on a black background.

The SurfaceView is added via XML layout definition:

<?xml version="1.0" encoding="utf-8"?>
<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"
            tools:context=".MainActivity"
            tools:deviceIds="wear_round"
            android:id="@+id/rlMain"
            android:background="#FF3333FF"
            android:keepScreenOn="true"
            >
    <com.kiteflo.sniper.BackgroundView
        android:id="@+id/backgroundView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
    />
</RelativeLayout>

The car is added programmatically:

BackgroundView bgView = (BackgroundView)findViewById(R.id.backgroundView);
RelativeLayout rlMain = (RelativeLayout)findViewById(R.id.rlMain);

// add car
CarView cv = new CarView(selfReference, new Car(0, 0,   BitmapFactory.decodeResource(getResources(),
                    R.drawable.car_pink)), bgView.getBackgroundModel());
rlMain.addView(cv);
...

Did anybody experience the same behavior and maybe found a workaround for this one?

Thanx in advance, Florian

// edit: I have found a workaround for this issue!

Basically there are two workrounds: the first workaround can be used in case you dont want to add anything on top of the SurfaceView, in this case you simply can set the zOrderOnTop property:

setZOrderOnTop(true);

The second possibility is way more complex but works as well. The behavior does not appear if the Activity holding the SurfaceView launches another Activity with corresponding view and then returns from this activity. In this case the SurfaceView will render correctly.

In conclusion: 1) launch the Activity holding the SurfaceView 2) immediately launch another Activity (for example an intro screen) 3) close the (Intro-)Activity and the SurfaceView will remain stable...

Hope this helps...took me a couple of hours :(

来源:https://stackoverflow.com/questions/28232939/surfaceview-in-android-wear-api-21

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!