【发布时间】:2015-09-29 14:17:57
【问题描述】:
我正在尝试将视频流式传输到自定义 TextureView。视频以全屏模式显示。纵向模式和横向模式的布局相同:
<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"
android:id="@+id/root_layout"
tools:context=".SampleActivity">
<com.my.package.MyCustomTextureView
android:id="@+id/texture_custom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:keepScreenOn="true" />
...
</RelativeLayout>
Manifest 中的 Activity 设置为自行处理方向变化:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SampleActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
当方向改变时,Activity 似乎找到了正确的大小:
public class SampleActivity extends FragmentActivity {
....
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
RelativeLayout root = (RelativeLayout) findViewById(R.id.root_layout);
MyCustomTextureView view = (MyCustomTextureView) findViewById(R.id.texture_custom);
Log.d(TAG, "onConfigurationChanged newConfig: " + newConfig.screenWidthDp + "x" + newConfig.screenHeightDp + " root: "+root.getWidth() + "x" + root.getHeight() + " view: " + view.getWidth() + "x" + view.getHeight());
....
}
}
活动日志输出,当我从纵向传递到横向时(注意:这是在布局更改之前):
09-29 15:30:28.975 22303-22303/com.my.package D/com.my.package.SampleActivity onConfigurationChanged newConfig: 598x335 root: 1080x1920 view: 1080x1920
...但是自定义纹理视图接收到错误的值:
public class MyCustomTextureView extends TextureView implements TextureView.SurfaceTextureListener {
....
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
Log.v(TAG, "onSurfaceTextureSizeChanged params: " + width + "x" + height + ", old values " + mMyWidth + "x" + mMyHeight + ", method values: " + getWidth() + "x" + getHeight());
}
....
}
TextureView 日志输出,当我从纵向传递到横向时:
09-29 15:30:29.037 22303-22303/com.my.package V/com.my.package.MyCustomTextureView﹕ onSurfaceTextureSizeChanged params: 1080x1080, old values 1080x1920, method values: 1080x1080
如果我回到纵向模式,它似乎会再次检索到正确的尺寸:
活动:
09-29 15:30:45.019 22303-22303/com.my.package D/com.my.package.SampleActivity onConfigurationChanged newConfig: 360x567 layout: 1920x1080 view: 1080x1080
纹理视图:
09-29 15:30:45.096 22303-22303/com.my.package V/com.my.package.MyCustomTextureView onSurfaceTextureSizeChanged params: 1080x1920, old values 1080x1920, method values: 1080x1920
我做错了什么?
【问题讨论】:
标签: android android-layout android-orientation textureview