这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。
首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:
activity_main。xml
& lt; FrameLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=癿atch_parent”比; & lt; SurfaceView android: id=癅 + id/surface_view” android: layout_width=" match_parent " android: layout_height=" match_parent "/比; & lt;使用 android: layout_width=" match_parent " android: layout_height=" match_parent " 面向android:=按怪薄北? & lt; ImageView android: id=癅 + id/开始” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_centerHorizontal=" true " android: layout_alignParentBottom=" true " android: layout_marginBottom=" 10 dp” android: src=" https://www.yisu.com/zixun/@drawable捕捉”/比; & lt; TextView android: id=癅 + id/count_down” android: layout_width=" match_parent " android: layout_height=" match_parent " android: layout_gravity="中心" android:重力="中心" android: textSize=" 80 sp/比; & lt;/RelativeLayout> & lt;/FrameLayout> >之前MainActivity。java
包com.jackie.timercamera; 进口android.content.Context; 进口android.graphics.Bitmap; 进口android.graphics.BitmapFactory; 进口android.graphics.Matrix; 进口android.hardware.Camera; 进口android.media.AudioManager; 进口android.media.MediaPlayer; 进口android.net.Uri; 进口android.os.Bundle; 进口android.os.Environment; 进口handler; 进口android.support.v7.app.AppCompatActivity; 进口android.util.Log; 进口android.view.SurfaceHolder; 进口android.view.SurfaceView; 进口android.view.View; 进口android.widget.ImageView; 进口android.widget.TextView; 进口java.io.File; 进口java.io.FileNotFoundException; 进口java.io.FileOutputStream; 进口java.io.IOException; 公开课MainActivity AppCompatActivity实现SurfaceHolder.Callback延伸, 视图。OnClickListener,相机。PictureCallback { 私人SurfaceView mSurfaceView; 私人ImageView mIvStart; 私人TextView mTvCountDown; 私人SurfaceHolder mHolder; 私人相机mCamera; 私人处理程序mHandler=new处理程序(); 私人int mCurrentTimer=10; 私人布尔mIsSurfaceCreated=false; 私人布尔mIsTimerRunning=false; 私有静态最终int CAMERA_ID=0;//后置摄像头//私有静态最终int CAMERA_ID=1;//前置摄像头 私有静态最终字符串标签=MainActivity.class.getSimpleName (); @Override 保护空白>//设置相机预览方向 mCamera.setDisplayOrientation(90), >之前保存图片的时候调整角度:
效果图如下: