我的xml文件:
<SurfaceView
android:id="@+id/surfaceView"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="300dp" />
Run Code Online (Sandbox Code Playgroud)
我的setDisplay功能:
public void playVideo() {
MediaPlayer mp = new MediaPlayer();
SurfaceView sv = (SurfaceView) this.findViewById(R.id.surfaceView);
try {
mp.setDataSource("sdcard/test/a.3gp");
SurfaceHolder sh = sv.getHolder();
mp.setDisplay(sh);***----the exception occured here***
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
日志如下:
04-24 22:19:33.645: W/System.err(16106): java.lang.IllegalArgumentException: The surface has been released
04-24 22:19:33.645: W/System.err(16106): at android.media.MediaPlayer._setVideoSurface(Native Method)
04-24 22:19:33.645: W/System.err(16106): …Run Code Online (Sandbox Code Playgroud) 我刚开始在android中进行游戏开发,而我正在开发一款超级简单的游戏.
这场比赛基本上就像飞扬的小鸟.
我设法让一切工作,但我得到了很多口吃和滞后.
我用来测试的手机是LG G2,所以它应该并且确实运行的游戏比这更重,更复杂.
基本上有4个"障碍"是全屏宽度彼此分开.
当游戏开始时,障碍物开始以恒定速度移动(朝向角色).玩家角色的x值在整个游戏中是一致的,而其y值会发生变化.
滞后主要发生在角色穿过障碍物时(有时也会在障碍物之后).会发生的是,游戏状态的每个图形都存在不均匀的延迟,导致运动中出现断断续续的情况.
我的想法是有一行或多行代码导致某种延迟发生(因此跳过帧).我也认为这些线路应该是围绕update()和draw()方法PlayerCharacter,Obstacle以及MainGameBoard.
问题是,我还是专门针对Android开发和Android 游戏开发的新手,所以我不知道是什么原因导致这种延迟.
我试着在网上寻找答案......不幸的是,我发现所有这些都指向了GC.但是,我不相信这种情况(如果我错了,请纠正我)这些答案对我不适用.我也阅读了android开发者的Performance Tips页面,但找不到任何帮助.
所以,请帮助我找到解决这些令人讨厌的滞后的答案!
MainThread.java:
public class MainThread extends Thread {
public static final String TAG = MainThread.class.getSimpleName();
private final static int MAX_FPS = 60; // desired fps
private final static int MAX_FRAME_SKIPS = 5; // maximum number of frames to be skipped
private final static int FRAME_PERIOD = 1000 / MAX_FPS; // …Run Code Online (Sandbox Code Playgroud) 借助MediaProjectionAndroid L中提供的API,可以实现
将主屏幕的内容(默认显示)捕获到Surface对象中,然后您的应用可以通过网络发送该对象
我已经设法让VirtualDisplay工作,我SurfaceView正确显示屏幕的内容.
我想要做的是捕获显示在其中的框架Surface,并将其打印到文件.我尝试过以下内容,但我得到的只是一个黑色文件:
Bitmap bitmap = Bitmap.createBitmap
(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
surfaceView.draw(canvas);
printBitmapToFile(bitmap);
Run Code Online (Sandbox Code Playgroud)
有关如何从中检索显示数据的任何想法Surface?
因此,作为@j__m建议,现在我的设置VirtualDisplay使用Surface的ImageReader:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;
imageReader = ImageReader.newInstance(displayWidth, displayHeight, ImageFormat.JPEG, 5);
Run Code Online (Sandbox Code Playgroud)
然后我创建虚拟显示传递Surface到MediaProjection:
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;
mediaProjection.createVirtualDisplay("test", …Run Code Online (Sandbox Code Playgroud) 在我的Android应用程序中,我用a SurfaceView来绘制东西.它在数千台设备上运行良好 - 除了现在用户开始在以下设备上报告ANR:
所以我得到了LG G4,确实能够验证问题.它与...直接相关SurfaceView.
现在猜猜经过几个小时的调试后修复了什么问题?它正在取代......
mSurfaceHolder.unlockCanvasAndPost(c);
Run Code Online (Sandbox Code Playgroud)
...... ......
mSurfaceHolder.unlockCanvasAndPost(c);
System.out.println("123"); // THIS IS THE FIX
Run Code Online (Sandbox Code Playgroud)
怎么会这样?
以下代码是我的渲染线程,除了提到的设备之外一直工作正常:
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MyThread extends Thread …Run Code Online (Sandbox Code Playgroud) 我已设法使用此处的答案将相机预览旋转为肖像:http: //code.google.com/p/zxing/issues/detail?id = 178#c46
然而,预览本身是拉伸/扭曲的 - 高度似乎是拉伸以填充框(但可能是宽度对于屏幕太大而且它被压扁以适应)
有没有办法"包装"内容而不是让它拉伸以适应表面视图?
编辑:我通过改变'CameraConfigurationManager'中的'findBestPreviewSizeValue'方法并反转X和Y点(对于下面指针的sean owen!)进行排序,这里是代码:
private static Point findBestPreviewSizeValue(CharSequence previewSizeValueString,
Point screenResolution) {
int bestX = 0;
int bestY = 0;
int diff = Integer.MAX_VALUE;
for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {
previewSize = previewSize.trim();
int dimPosition = previewSize.indexOf('x');
if (dimPosition < 0) {
Log.w(TAG, "Bad preview-size: " + previewSize);
continue;
}
int newX;
int newY;
try {
newY = Integer.parseInt(previewSize.substring(0, dimPosition));
newX = Integer.parseInt(previewSize.substring(dimPosition + 1));
//original:
//newX = Integer.parseInt(previewSize.substring(0, dimPosition));
//newY = …Run Code Online (Sandbox Code Playgroud) 我在我的Android应用程序中有一个相机预览.你可能都知道它是由surfaceviewandroid 实现的.
在我的照片应用程序中,允许用户拍照,我想模糊相机预览(表面视图),如果用户还没有登录,如果用户登录,我将显示正常预览(没有模糊)
模糊如同 
但似乎没有办法做到这一点
我想到了几件事,但我不确定如何实现它
另一种方法是更改窗口的属性,但我无法对surfaceview执行此操作,
getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Run Code Online (Sandbox Code Playgroud)那么我们可以创建一个窗口覆盖surfaceview并设置这样的标志吗?我不这么认为
谁能告诉我如何模糊相机预览,这是一个表面视图
注意:我试图模糊一个区域,这是相机预览的输出,所以它不像我模糊静态图像,模糊区域将根据你的手机相机的位置而改变
是否有Surface,SurfaceHolder,EGLSurface,SurfaceView,GLSurfaceView,SurfaceTexture和TextureView的详细说明?特别是:
android surfaceview glsurfaceview surfaceflinger textureview
我想在SurfaceView上打开相机硬件.在布局中,我创建了一个SurfaceView,然后打开相机,如下面的代码所示.当我运行代码时,CameraAvailableCB中的toast出现并显示"onCameraAvailable",但SurfaceView上没有任何内容.
如何在SurfaceView上显示相机显示?
码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mBtnCapture = (Button) findViewById(R.id.actMain_btn_capture);
this.mSurfaceView = (SurfaceView) findViewById(R.id.actMain_surfaceView);
this.mSurfaceHolder = this.mSurfaceView.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
try {
cameraIDsList = this.mCameraManager.getCameraIdList();
for (String id : cameraIDsList) {
Log.v(TAG, "CameraID: " + id);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
cameraStateCB = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
Toast.makeText(getApplicationContext(), "onOpened", Toast.LENGTH_SHORT).show();
//requesting permission
int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) …Run Code Online (Sandbox Code Playgroud) 我正在使用SurfaceView和渲染线程来开发基于LunarLander等结构的游戏.
但是,我遇到了很多问题,我想在这里指出一切.我希望任何想要开发游戏的人都不需要再与他们斗争了.任何对结构有更好了解的人都可以分享他们的经验,因为我还是新手,并渴望学习:)
[1] thread.start()不应多次调用该函数.
在创建表面时创建线程时提到的许多文章,以便在使用该方法暂停活动后再次渲染:
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
if (thread.getState() == Thread.State.TERMINATED)
{
thread = new LunarThread(getHolder(), getContext(), getHandler());
}
thread.setRunning(true);
thread.start();
}
Run Code Online (Sandbox Code Playgroud)
您可以看到,如果线程未终止且函数被调用,则活动崩溃.
[2]如果您按下"电源"或"红色电话"按钮,或者电话闲置几分钟,则活动将处于onPause()状态,但线程仍在运行.这是一个非常糟糕的做法,所以我需要找到让线程停止的方法,然后重新开始onResume().
[3]如果屏幕锁定是纵向/横向,并且您的游戏粘贴到另一个方向,则屏幕锁定会强制您"定向"一次.这意味着再次开始活动.我仍然无法找到解决方案.(正如我在Android屏幕方向错误中提到的那样)
以下是我修复这些问题的代码:
UIThread
public class UIThread extends Activity
{
private gameView gameview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gameview = (gameView) findViewById(R.id.gameview);
}
protected …Run Code Online (Sandbox Code Playgroud) 当我运行我的Android应用程序时,我在LogCat窗口中注意到以下警告:
Tag: SurfaceView
Text: CHECK surface infomation creating=false formatChanged=false
sizeChanged=false visible=false visibleChanged=true surfaceChanged=true
realSizeChanged=false redrawNeeded=false left=false top=false
Run Code Online (Sandbox Code Playgroud)
这是什么意思?它似乎没有引起任何问题,但摆脱警告会很好.
android ×10
surfaceview ×10
java ×2
bitmap ×1
blur ×1
camera ×1
game-loop ×1
portrait ×1
stretching ×1
textureview ×1
zxing ×1