直接上代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mSurface"/>
<Button
android:id="@+id/btn_change"
android:text="给动画,SurfaceView不支持设置透明度"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
package com.etrans.camerasurfaceview;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.util.StringTokenizer;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
//Android openGl开发详解(二)——通过SurfaceView,TextureView,GlSurfaceView显示相机预览(附Demo)
//https://blog.csdn.net/qq_32175491/article/details/79755424
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
@BindView (R.id.mSurface)
SurfaceView mSurfaceView;
public SurfaceHolder mHolder;
public Camera mCamera;
private Camera.Parameters mParameters;
private String TAG = "myCamera";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
ButterKnife.bind (this);
mHolder = mSurfaceView.getHolder ();
mHolder.addCallback (this);
mHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
mCamera = Camera.open (0);
mCamera.setDisplayOrientation (90);
mCamera.setPreviewDisplay (holder);
mCamera.startPreview ();
}catch (IOException e){
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
mCamera.autoFocus (new Camera.AutoFocusCallback () {
@Override
public void onAutoFocus(boolean success,Camera camera) {
if (success){
mParameters = mCamera.getParameters ();
String flattened = mParameters.flatten ();
//Log.e ("mParameters",flattened );
// StringTokenizer tokenizer = new StringTokenizer (flattened,";");
// Log.d (TAG,"Dump all parameters:");
// while (tokenizer.hasMoreElements ())Log.d (TAG,tokenizer.nextToken ());
mParameters.setExposureCompensation (-40);
mParameters.set("iso","ISO3200");
mParameters.setPictureFormat (PixelFormat.JPEG);
//mParameters.setFlashMode (Camera.Parameters.FLASH_MODE_TORCH);//预览持续发光
mParameters.setFocusMode (Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//持续对焦模式
mCamera.setParameters (mParameters);
mCamera.startPreview ();
mCamera.cancelAutoFocus ();
}
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null){
//mCamera.setPreviewCallback (null);
mCamera.stopPreview ();
mCamera.release ();
mCamera = null;
}
}
@OnClick(R.id.btn_change)
public void onViewClicked(){
PropertyValuesHolder valuesHolder = PropertyValuesHolder.ofFloat ("rotationY",0.0f,360.0f,0.0f);
PropertyValuesHolder valuesHolder1 = PropertyValuesHolder.ofFloat ("scaleX",1.0f,0.5f,1.0f);
//PropertyValuesHolder valuesHolder2 = PropertyValuesHolder.ofFloat ("rotationX",0.0f,360.0f,0.0f);
PropertyValuesHolder valuesHolder3 = PropertyValuesHolder.ofFloat ("scaleY",1.0f,0.5f,1.0f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder (
mSurfaceView,valuesHolder,valuesHolder1,valuesHolder3);
objectAnimator.setDuration (5000).start ();
}
}