-
Notifications
You must be signed in to change notification settings - Fork 2
/
Practice14FlipboardView.java
99 lines (82 loc) · 2.81 KB
/
Practice14FlipboardView.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
99
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.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.geekholt.practiceui.R;
public class Practice14FlipboardView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Camera camera = new Camera();
int degree;
ObjectAnimator animator = ObjectAnimator.ofInt(this, "degree", 0, 180);
public Practice14FlipboardView(Context context) {
super(context);
}
public Practice14FlipboardView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice14FlipboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
animator.setDuration(2500);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
}
@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 = getWidth() / 2;
int centerY = getHeight() / 2;
int x = centerX - bitmapWidth / 2;
int y = centerY - bitmapHeight / 2;
// 第一遍绘制:上半部分
canvas.save();
canvas.clipRect(0, 0, getWidth(), centerY);
canvas.drawBitmap(bitmap, x, y, paint);
canvas.restore();
// 第二遍绘制:下半部分
canvas.save();
if (degree < 90) {
canvas.clipRect(0, centerY, getWidth(), getHeight());
} else {
canvas.clipRect(0, 0, getWidth(), centerY);
}
camera.save();
camera.rotateX(degree);
canvas.translate(centerX, centerY);
camera.applyToCanvas(canvas);
canvas.translate(-centerX, -centerY);
camera.restore();
canvas.drawBitmap(bitmap, x, y, paint);
canvas.restore();
}
}