TextureView getBitmap()在角落中创建小窗口反射

时间:2013-05-06 19:46:10

标签: android android-camera

目前我在Android上使用相机。

我的问题是:

我的正常布局 http://img19.imageshack.us/img19/2402/screenshot2013050622260.jpg

当我将TextureView.getBitmap()传递给ImageView时,左下角的所有屏幕apier的反射有多小。 我不需要!

被修改 http://img834.imageshack.us/img834/4734/screenshot2013050622263.jpg

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextureView
        android:id="@+id/textureView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</LinearLayout>

MyCameraActivity.java

package lt.mk.mycam2;

import java.io.IOException;

import lt.mk.mycam2.SocketStringClient.SocketListener;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class MyCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;
    private ImageView mImageView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mTextureView = (TextureView) findViewById(R.id.textureView1);
        mTextureView.setSurfaceTextureListener(this);
    }

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open(1);
        try {
            mCamera.setPreviewTexture(surface);
            Size size = mCamera.getParameters().getPreviewSize();
            mTextureView.setLayoutParams(new LayoutParams(size.width, size.height));
            mCamera.startPreview();
        } catch (IOException ioe) {}
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // if i comment following line everything looks OK, like in first image
        mImageView.setImageBitmap(mTextureView.getBitmap());
    }
}

那么为什么会出现这种反思,以及如何让它消失呢?

修改2

解决

当我被包围时,我不知道为什么会顺利运作:

mImageView.setImageBitmap(mTextureView.getBitmap());

with Handler:

new Handler().post(new Runnable() {
    public void run() {
        mImageView.setImageBitmap(mTextureView.getBitmap());
    }
});

Handler以某种方式解决了这个问题:)

1 个答案:

答案 0 :(得分:0)

可能是你的ImageView没有背景吗?

如果您这样做会发生什么:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        android:src="@drawable/ic_launcher" />

确保您的colors.xml具有:

<color name="transparent">#00000000</color>