标签: surfaceview

如何拥有透明的ImageButton:Android

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@drawable/transparent"></ImageButton>
Run Code Online (Sandbox Code Playgroud)

这就是我试图获得透明的ImageButton以便将这些按钮放在SurfaceView上.但是,只要我在xml中包含透明线,Eclipse就会在项目中出现错误.

请帮忙.

android transparent imagebutton surfaceview

472
推荐指数
9
解决办法
28万
查看次数

扩展类时出错错误

我正在尝试创建一个GhostSurfaceCameraView扩展的自定义视图SurfaceView.这是我的类定义文件

GhostSurfaceCameraView.java:

public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    GhostSurfaceCameraView(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where to draw.
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = …
Run Code Online (Sandbox Code Playgroud)

java xml android class surfaceview

188
推荐指数
3
解决办法
13万
查看次数

Android Camera Preview Stretched

我一直在努力在Android上制作自定义相机活动,但是当旋转相机时,表面视图的纵横比会变得混乱.

在我的oncreate活动中,我设置了framelayout,它保存了显示摄像机参数的表面视图.

//FrameLayout that will hold the camera preview
        FrameLayout previewHolder = (FrameLayout) findViewById(R.id.camerapreview);

        //Setting camera's preview size to the best preview size
        Size optimalSize = null;
        camera = getCameraInstance();
        double aspectRatio = 0;
        if(camera != null){
            //Setting the camera's aspect ratio
            Camera.Parameters parameters = camera.getParameters();
            List<Size> sizes = parameters.getSupportedPreviewSizes();
            optimalSize = CameraPreview.getOptimalPreviewSize(sizes, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
            aspectRatio = (float)optimalSize.width/optimalSize.height;
        }

        if(optimalSize!= null){
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
            previewHolder.setLayoutParams(params);
            LayoutParams surfaceParams = new LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
            cameraPreview.setLayoutParams(surfaceParams);

        }

        cameraPreview.setCamera(camera);

        //Adding the preview …
Run Code Online (Sandbox Code Playgroud)

android preview aspect-ratio surfaceview android-camera

129
推荐指数
5
解决办法
11万
查看次数

了解Canvas和Surface概念

我很难理解绘制的过程,SurfaceView因此也很难理解在Android中使用的整个Surface/ Canvas/ Bitmap系统.

我已经阅读了所有文章和API文档页面,我可以在android-developers网站上找到,一些android图形教程,LunarLander源代码和这个问题.

请告诉我,这些陈述中哪些是真的,哪些不是,为什么.

  1. Canvas有它自己的Bitmap附加.Surface有它自己的Canvas附加.
  2. 所有View的窗口共享相同Surface,因此共享相同Canvas.
  3. SurfaceView是子类View,它与其他View子类View本身不同,它有自己Surface的绘制.

还有一个问题:

  • Surface如果已经有一个Canvas用于位图的高级操作,为什么需要一个类.举一个Canvas不适合做Surface可以做的工作的情况的例子.

android surfaceview android-canvas

114
推荐指数
3
解决办法
4万
查看次数

Android,canvas:如何清除(删除内容)画布(= bitmaps),生活在surfaceView中?

为了制作一个简单的游戏,我使用了一个模板来绘制带有这样的位图的画布:

private void doDraw(Canvas canvas) {
    for (int i=0;i<8;i++)
        for (int j=0;j<9;j++)
            for (int k=0;k<7;k++)   {
    canvas.drawBitmap(mBits[allBits[i][j][k]], i*50 -k*7, j*50 -k*7, null); } }
Run Code Online (Sandbox Code Playgroud)

(画布在"run()"中定义/ SurfaceView存在于GameThread中.)

我的第一个问题是如何清除(或重绘)整个画布以获得新的布局?
其次,如何更新屏幕的一部分?

// This is the routine that calls "doDraw":
public void run() {
    while (mRun) {
        Canvas c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                if (mMode == STATE_RUNNING) 
                    updateGame();
                doDraw(c);          }
        } finally {
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);  }   }   }       }
Run Code Online (Sandbox Code Playgroud)

android surfaceview android-canvas

88
推荐指数
7
解决办法
14万
查看次数

如何正确设置Android相机方向?

我想根据Android中的设备方向设置相机方向,但似乎没有任何效果.我尝试旋转Surface以及相机参数,但是纵向模式下的相机预览总是颠倒过来.我需要顺时针旋转90度才能使其正确.这是我现在使用的代码,仅适用于横向模式.

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {          
        initCamera();           
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.2;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size …
Run Code Online (Sandbox Code Playgroud)

camera android orientation surfaceview

87
推荐指数
7
解决办法
16万
查看次数

如何使surfaceview透明

大家好我想让我的DrawingSurface视图透明.我试了很多东西,但它不起作用.

这是我的表面视图透明的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/icon" >
        </ImageView>

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00000000" >

            <codewalla.android.drawings.DrawingSurface
                android:id="@+id/drawingSurface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </codewalla.android.drawings.DrawingSurface>
        </LinearLayout>
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/colorRedBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="R" />

        <Button
            android:id="@+id/colorBlueBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="G" />

        <Button
            android:id="@+id/colorGreenBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="B" />

        <Button
            android:id="@+id/undoBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="U" />

        <Button
            android:id="@+id/redoBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="R" />
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android surfaceview

81
推荐指数
4
解决办法
6万
查看次数

SurfaceHolder回调如何与Activity生命周期相关?

我一直在尝试实现一个需要在表面上进行相机预览的应用程序.在我看到事物时,活动和表面生命周期都包含以下状态:

  1. 当我第一次启动我的活动: onResume()->onSurfaceCreated()->onSurfaceChanged()
  2. 当我离开我的活动: onPause()->onSurfaceDestroyed()

在此方案中,我可以像打开/释放相机相应的调用和启动/停止在预览onPause/onResumeonSurfaceCreated()/onSurfaceDestroyed().

它工作正常,除非我锁定屏幕.当我启动应用程序时,然后锁定屏幕并在以后解锁它我看到:

onPause()- 屏幕锁定后没有别的 - 然后onResume()解锁后 - 之后没有表面回调.实际上,onResume()在按下电源按钮并且屏幕打开后调用,但是锁定屏幕仍然处于活动状态,因此,在活动变得均匀可见之前.

通过这种方案,我在解锁后得到一个黑屏,并且没有调用表面回调.

这是一个代码片段,不涉及相机的实际工作,而是SurfaceHolder回调.即使在我的手机上使用此代码,也会重现上述问题(按"后退"按钮时会以正常顺序调用回调,但在锁定屏幕时会丢失):

class Preview extends SurfaceView implements SurfaceHolder.Callback {

    private static final String tag= "Preview";

    public Preview(Context context) {
        super(context);
        Log.d(tag, "Preview()");
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(tag, "surfaceCreated");
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(tag, "surfaceDestroyed");
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { …
Run Code Online (Sandbox Code Playgroud)

camera android surfaceview

71
推荐指数
2
解决办法
4万
查看次数

如何在Android上使用Camera的SurfaceView上绘制叠加层?

我有一个简单的程序,将预览绘制Camera成一个SurfaceView.我正在尝试做的是使用该onPreviewFrame方法,每次将新帧绘制到SurfaceViewinvalidate方法时调用该onDraw方法,以便执行应该调用该方法的方法.事实上,该onDraw方法正在被调用,但没有任何东西被打印(我猜相机预览正在覆盖我正在尝试绘制的文本).

这是SurfaceView我所拥有的子类的简化版本:

public class Superficie extends SurfaceView implements SurfaceHolder.Callback {
 SurfaceHolder mHolder;
 public Camera camera;
 Superficie(Context context) {
  super(context);
  mHolder = getHolder();
  mHolder.addCallback(this);
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }
 public void surfaceCreated(final SurfaceHolder holder) {
  camera = Camera.open();
  try {
   camera.setPreviewDisplay(holder);
   camera.setPreviewCallback(new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera arg1) {
     invalidar();
    }
   });
  } catch (IOException e) {}
 }
 public void invalidar(){
  invalidate();
 }
 public void surfaceChanged(SurfaceHolder …
Run Code Online (Sandbox Code Playgroud)

camera android surfaceview

69
推荐指数
2
解决办法
10万
查看次数

片段 - 指定的子节点已有父节点.您必须首先在孩子的父母上调用removeView()

我收到了这个错误.我尝试了很多解决方案,但我还是想解决这个问题.帮我!我需要使用片段将表面视图和按钮添加到活动中.

CamActivity.java:

public class CamActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cam);

        FragmentManager fm = getSupportFragmentManager();

        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if(fragment == null) {
            fragment = new CamFragment();
            fm.beginTransaction()
            .add(R.id.fragmentContainer, fragment)
            .commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

CamFragment.java:

public class CamFragment extends Fragment {

    private static final String TAG = "CamFragment";

    private Camera mCamera;
    private SurfaceView mSurfaceView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.camera_fragment, parent);

    Button capturePic = (Button)v.findViewById(R.id.img_capture);
    capturePic.setOnClickListener(new View.OnClickListener() { …
Run Code Online (Sandbox Code Playgroud)

android surfaceview android-fragments

69
推荐指数
4
解决办法
7万
查看次数