[android]代码库
package com.dgchina.activity;
import com.dgchina.camera.CameraInterface;
import com.dgchina.camera.CameraInterface.CamOpenOverCallback;
import com.dgchina.preview.*;
import com.dgchina.util.DisplayUtil;
import com.example.playcamera.R;
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class CameraActivity extends Activity implements CamOpenOverCallback {
private static final String TAG = "yanzi";
CameraSurfaceView surfaceView = null;
Button shutterBtn;
float previewRate = -1f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread openThread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
CameraInterface.getInstance().doOpenCamera(CameraActivity.this);
}
};
openThread.start();
//去掉标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
initUI();
initViewParams();
shutterBtn.setOnClickListener(new BtnListeners());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.camera, menu);
return true;
}
private void initUI(){
surfaceView = (CameraSurfaceView)findViewById(R.id.camera_surfaceview);
shutterBtn = (Button)findViewById(R.id.btn_shutter);
}
private void initViewParams(){
LayoutParams params = surfaceView.getLayoutParams();
Point p = DisplayUtil.getScreenMetrics(this);
params.width = p.x;
params.height = p.y;
previewRate = DisplayUtil.getScreenRate(this); //默认全屏的比例预览
surfaceView.setLayoutParams(params);
//手动设置拍照ImageButton的大小为120dip×120dip,原图片大小是64×64
// LayoutParams p2 = shutterBtn.getLayoutParams();
// p2.width = DisplayUtil.dip2px(this, 80);
// p2.height = DisplayUtil.dip2px(this, 80);;
// shutterBtn.setLayoutParams(p2);
}
@Override
public void cameraHasOpened() {
// TODO Auto-generated method stub
SurfaceHolder holder = surfaceView.getSurfaceHolder();
CameraInterface.getInstance().doStartPreview(holder, previewRate);
}
private class BtnListeners implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_shutter:
//CameraInterface.getInstance().doTakePicture();
break;
default:break;
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction())
{
//触摸屏幕时刻
case MotionEvent.ACTION_DOWN:
CameraInterface.getInstance().doTakePicture();
// Toast.makeText(MainActivity.this,"点击啦!",Toast.LENGTH_LONG).show();
break;
//触摸并移动时刻
case MotionEvent.ACTION_MOVE:
//Toast.makeText(MainActivity.this,"移动中!",Toast.LENGTH_LONG).show();
break;
//终止触摸时刻
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(event);
}
}
[源代码打包下载]