/
Practice13CameraRotateHittingFaceView.java
98 lines (82 loc) · 2.97 KB
/
Practice13CameraRotateHittingFaceView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.geekholt.practiceui.view.lesson4;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.geekholt.practiceui.R;
public class Practice13CameraRotateHittingFaceView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Point point = new Point(200, 50);
Camera camera = new Camera();
Matrix matrix = new Matrix();
int degree;
ObjectAnimator animator = ObjectAnimator.ofInt(this, "degree", 0, 360);
public Practice13CameraRotateHittingFaceView(Context context) {
super(context);
}
public Practice13CameraRotateHittingFaceView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice13CameraRotateHittingFaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() * 2, bitmap.getHeight() * 2, true);
bitmap.recycle();
bitmap = scaledBitmap;
animator.setDuration(5000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ValueAnimator.INFINITE);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float newZ = - displayMetrics.density * 6;
//设置相机位置,小孔成像原理近大远小
camera.setLocation(0, 0, newZ);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animator.start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
animator.end();
}
@SuppressWarnings("unused")
public void setDegree(int degree) {
this.degree = degree;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int centerX = point.x + bitmapWidth / 2;
int centerY = point.y + bitmapHeight / 2;
camera.save();
matrix.reset();
camera.rotateX(degree);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
canvas.save();
canvas.concat(matrix);
canvas.drawBitmap(bitmap, point.x, point.y, paint);
canvas.restore();
}
}