一起用代码吸猫!本文正在参与【喵星人征文活动】
思路
看到活动之后我就上网搜了一下逗猫app,搜到了snapCat这个app的介绍,就参考这个app的思路——用动图吸引猫的注意力,当猫爪子按在图上的时候偷拍一张。试试能不能给格格拍点好看的照片。
step1.偷偷拍照
用android.hardware.Camera搭配SurfaceView。
- 在布局文件添加预览界面(虽然猫不需要看预览,但还是要startpreview)
Important: Call
startPreview()to start updating the preview surface. Preview must be started before you can take a picture.
<SurfaceView
android:id="@+id/sf"
android:layout_width="1dp"
android:layout_height="1dp" />
- 初始化前置摄像头
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
myCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
try {
myCamera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
myCamera.stopPreview();
myCamera.release();
myCamera = null;
}
myCamera.startPreview();
- 拍照保存到相册
private void takePhoto() {
myCamera.autoFocus(myAutoFocus);
myCamera.takePicture(null, null, myPicCallback);
}
private Camera.AutoFocusCallback myAutoFocus = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
};
private Camera.PictureCallback myPicCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 将得到的照片进行270°旋转,使其竖直
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
matrix.preRotate(270);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
saveImage(timeStamp+".jpg", bitmap);
}
};
@RequiresApi(api = Build.VERSION_CODES.Q)
private void saveImage(String fileName, Bitmap bitmap) {
try {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/cat/");
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/JPEG");
Uri uri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
if (uri != null) {
OutputStream outputStream = mContext.getContentResolver().openOutputStream(uri);
if (outputStream != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
outputStream.flush();
outputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
关闭摄像头
myCamera.stopPreview();
myCamera.release();
myCamera = null;
step2.吸引注意
- 在布局文件添加gif图片
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true" />
Glide.with(this).asGif().load(R.mipmap.bird).into(image);
- 点击图片-随机移动-拍照
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
x = (float) (Math.random() * width);
y = (float) (Math.random() * height);
Path path = new Path();
path.moveTo(x,y);
ObjectAnimator animator = ObjectAnimator.ofFloat(v, View.X, View.Y, path);
animator.setDuration(2000);
animator.start();
takePhoto();
}
});
width和height限制随机移动的范围
int defaultMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,150,getResources().getDisplayMetrics());
width = getWindowManager().getDefaultDisplay().getWidth()-defaultMargin;
height = getWindowManager().getDefaultDisplay().getHeight()-defaultMargin;
效果
大失败……完全吸引不了格格的注意,目中无手机,用小图移动的效果不好,之后做大图的动画试试,并且猫爪不太容易触发点击事件……放个勉强的效果图……