6

我正在尝试在服务中创建纹理视图,但我似乎没有收到“onSurfaceTextureAvailable”回调。“TextureView 或子类只能在启用硬件加速的情况下使用。” 当我为应用程序提供 android:hardwareAccelerated="true" 时出现在日志中,是否有任何权限明确提供给服务

package com.example.textures;



import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FlyMaadi extends Service implements SurfaceTextureListener {


    private final String TAG = "FlyMaadi";
    private TextureView mTextureView;
    private WindowManager mWindowManager;
    private LayoutInflater minflater;
    private ViewGroup mrel_layout;
    private TextureView mTextureView1;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    public void createSurfaceTexture()
    {
        Log.d(TAG,"Create Surface Texture\n");
        mTextureView = new TextureView(this);

        Log.d(TAG,"Is hardware accelerated:: "+ mTextureView.isHardwareAccelerated());


        mTextureView.setSurfaceTextureListener(this);

        //mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        minflater = (LayoutInflater)getSystemService
                (LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        FrameLayout mParentView = new FrameLayout(getApplicationContext());


        final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params1.width = 352;
        params1.height = 288;

        //mWindowManager.addView(mrel_layout, params1);

        mWindowManager.addView(mParentView, params1);


        if(mTextureView.isAvailable())
        {
            Log.d(TAG,"Texture view is already available");
        }
        else
        {
            Log.d(TAG,"Texture view is not available");
        }


        Log.d(TAG,"Adding surface texture:: ");


        mParentView.addView(mTextureView);






    }


    @Override 
    public void onCreate() {
        super.onCreate();

        Log.d(TAG,"onCreate");
        createSurfaceTexture();

    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureAvailable");
    }


    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureDestroyed");
        return false;
    }


    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureSizeChanged");

    }


    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureUpdated");

    }

}

我的 Androidmanifest xml 文件如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textures"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true" 
        >
        <activity
            android:name="com.example.textures.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service            
            android:name="com.example.textures.FlyMaadi"
            />


    </application>

</manifest>
4

3 回答 3

6

对于"A TextureView or a subclass can only be used with hardware acceleration enabled.",您可能想要替换此行:

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED

HTH。

于 2014-02-06T16:29:49.800 回答
1

一旦视图在屏幕上可见,就会调用 onSurfaceTextureAvailable。到那时它不会被调用。我看到您没有将纹理视图(及其父级)添加到活动窗口或其任何视图中。尝试将活动视图 ID 或视图传递给服务并将纹理视图添加到其中。一旦显示视图,就应该调用被调用的对象。(顺便说一句,不建议处理来自服务的视图)

于 2015-05-18T10:31:00.247 回答
0

我的问题是我正在使用 AndroidAnnotations 并且我必须在这个类中处理没有 androidannotations 的所有内容。

于 2014-12-31T01:28:33.323 回答