jluzc@aliyun.com
使用SurfaceView和TextureView进行Camera开发的时候不可避免的会遇到一个问题:Camera 显示图像变形,图像被拉伸或者手机旋转一定角度(小于90度)图像变形。
总结有三种方法可以解决这个问题:
1. 自定义layout,将SurfaceView 和 TextureView addView到此layout,然后通过onLayou, onMeasue,控制SurfaceView及TextureView大小。
2. 使用SurfaceView和TextureView自带的setLayoutParams(params) 属性设置控件大小。
3. 通过TextureView 的setTransform(matrix)方法,控制Camera显示范围 (SurfaceView没有 setTransform 属性)
下面是TextureView使用 setTransform(matrix)方法控制Camera显示范围的代码
public class MainActivity extends Activity implements SurfaceTextureListener,
OnLayoutChangeListener {private TextureView textureView;
Matrix matrix;
Camera camera;
int mWidth = 0;
int mHeight = 0;
int mDisplayWidth = 0;
int mDisplayHeight = 0;
int mPreviewWidth = 640;
int mPreviewHeight = 480;
int orientation = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = (TextureView)