android自定义缩放,android – SurfaceView使用自定义相机缩放IN和OUT功能

我已经编写了使用Custom Camera的代码,为此我使用的是SurfaceView,现在我想知道如果我需要实现Zoom IN和OUT功能,我从未使用过这种应用程序.

我的代码,我用来创建自定义相机,见下文:

MainActivity.java:-

public class MainActivity extends Activity {

private SurfaceView preview=null;

private SurfaceHolder previewHolder=null;

public Camera camera ;

private boolean inPreview=false;

ImageButton btnCapture;

@SuppressWarnings("deprecation")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

preview=(SurfaceView)findViewById(R.id.surface);

previewHolder=preview.getHolder();

previewHolder.addCallback(surfaceCallback);

previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

previewHolder.setFixedSize(getWindow().getWindowManager()

.getDefaultDisplay().getWidth(),getWindow().getWindowManager()

.getDefaultDisplay().getHeight());

btnCapture = (ImageButton) findViewById(R.id.btnCapture);

btnCapture.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Capture Image(s)",Toast.LENGTH_LONG).show();

}

});

}

@Override

public void onResume() {

super.onResume();

camera=Camera.open();

}

@Override

public void onPause() {

super.onPause();

if (inPreview) {

camera.stopPreview(); }

camera.release();

camera=null;

inPreview=false;

}

private Camera.Size getBestPreviewSize(int width,int height,Camera.Parameters parameters){

Camera.Size result=null;

for (Camera.Size size : parameters.getSupportedPreviewSizes())

{

if (size.width<=width && size.height<=height)

{

if (result==null) {

result=size;

} else {

int resultArea=result.width*result.height;

int newArea=size.width*size.height;

if (newArea>resultArea) {

result=size;

}

}

}

}

return(result);

}

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

public void surfaceCreated(SurfaceHolder holder) {

try {

camera.setPreviewDisplay(previewHolder);

} catch (Throwable t) {

Log.e("PreviewDemo-surfaceCallback","Exception in setPreviewDisplay()",t);

Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();

}

}

public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {

Camera.Parameters parameters=camera.getParameters();

Camera.Size size=getBestPreviewSize(width,height,parameters);

if (size!=null) {

parameters.setPreviewSize(size.width,size.height);

camera.setParameters(parameters);

camera.startPreview();

inPreview=true;

}

}

public void surfaceDestroyed(SurfaceHolder holder) {

}

};

}

activity_main.xml中: –

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/splash_background" >

android:id="@+id/surface"

android:layout_width="fill_parent"

android:layout_height="fill_parent" />

android:id="@+id/btnCapture"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:background="@drawable/capture"

android:contentDescription="@string/app_name" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值