想起之前看到的一段H5動畫,在Android平臺“臨摹”了一遍频敛。
效果如下圖:其構(gòu)圖還是比較簡單的司顿,樹枝加上由心形花瓣構(gòu)成的心形樹冠(后面做成動畫之后會有隨機的花瓣飄落)。
一盈蛮、樹枝
樹枝是通過貝塞爾曲線來構(gòu)造的废菱,二階貝塞爾曲線。
準備數(shù)據(jù)
getBranches()函數(shù)中抖誉,定義各個樹枝的位置和形狀殊轴,最終返回樹干。
繪制的時候袒炉,先繪制樹干旁理,然后繪制其分支,最后繪制分支的分支(只有三層)我磁。
public static Branch getBranches() {
// 共10列孽文,分別是id, parentId, 貝塞爾曲線控制點(3點,6列)夺艰, 最大半徑芋哭, 長度
int[][] data = new int[][]{
{0, -1, 217, 490, 252, 60, 182, 10, 30, 100},
{1, 0, 222, 310, 137, 227, 22, 210, 13, 100},
{2, 1, 132, 245, 116, 240, 76, 205, 2, 40},
{3, 0, 232, 255, 282, 166, 362, 155, 12, 100},
{4, 3, 260, 210, 330, 219, 343, 236, 3, 80},
{5, 0, 221, 91, 219, 58, 216, 27, 3, 40},
{6, 0, 228, 207, 95, 57, 10, 54, 9, 80},
{7, 6, 109, 96, 65, 63, 53, 15, 2, 40},
{8, 6, 180, 155, 117, 125, 77, 140, 4, 60},
{9, 0, 228, 167, 290, 62, 360, 31, 6, 100},
{10, 9, 272, 103, 328, 87, 330, 81, 2, 80}
};
int n = data.length;
Branch[] branches = new Branch[n];
for (int i = 0; i < n; i++) {
branches[i] = new Branch(data[i]);
int parent = data[i][1];
if (parent != -1) {
branches[parent].addChild(branches[i]);
}
}
return branches[0];
}
封裝Branch類
主要包含樹枝的構(gòu)建(構(gòu)造函數(shù),addChild函數(shù))郁副,以及繪制楷掉。
繪制樹枝時,不斷地調(diào)用grow函數(shù)霞势,繪制點(currLen)逐漸靠近末端(maxLen), 樹枝的半徑逐漸變小斑鸦;
最終控制點到達樹枝末端(currLen==maxLen), 繪制結(jié)束愕贡。
如果是繪制靜態(tài)畫面,while循環(huán)直到grow返回false;
如果是繪制動畫巷屿, 可通過調(diào)用postInvalidate()固以,不斷地對回調(diào)繪制函數(shù), 每一幀樹枝成長一截。
public class Branch {
private static final int BRANCH_COLOR = Color.rgb(35, 31, 32);
// control point
Point[] cp = new Point[3];
int currLen;
int maxLen;
float radius;
float part;
float growX;
float growY;
LinkedList<Branch> childList;
public Branch(int[] a){
cp[0] = new Point(a[2], a[3]);
cp[1] = new Point(a[4], a[5]);
cp[2] = new Point(a[6], a[7]);
radius = a[8];
maxLen = a[9];
part = 1.0f / maxLen;
}
public boolean grow(Canvas canvas, float scareFactor){
if(currLen <= maxLen){
bezier(part * currLen);
draw(canvas, scareFactor);
currLen++;
radius *= 0.97f;
return true;
}else{
return false;
}
}
private void draw(Canvas canvas, float scareFactor){
Paint paint = CommonUtil.getPaint();
paint.setColor(BRANCH_COLOR);
canvas.save();
canvas.scale(scareFactor, scareFactor);
canvas.translate(growX, growY);
canvas.drawCircle(0,0, radius, paint);
canvas.restore();
}
private void bezier(float t) {
float c0 = (1 - t) * (1 - t);
float c1 = 2 * t * (1 - t);
float c2 = t * t;
growX = c0 * cp[0].x + c1 * cp[1].x + c2* cp[2].x;
growY = c0 * cp[0].y + c1 * cp[1].y + c2* cp[2].y;
}
public void addChild(Branch branch){
if(childList == null){
childList = new LinkedList<>();
}
childList.add(branch);
}
}
效果圖如下:
二嘱巾、花瓣
花瓣的繪制憨琳,是通過一條曲線實現(xiàn)的:本文的主角,自帶愛情故事的心形線旬昭。
心形線有很多種篙螟,有的用標準方程表示,有的用參數(shù)方程表示问拘。
對于繪制曲線來說遍略,參數(shù)方程更方便一些惧所。
在網(wǎng)站wolframalpha上,可以輸入方程直接預覽曲線绪杏。
計算心形線
因為要繪制很多花瓣下愈,所以可以將其形狀預先計算好,緩存起來蕾久。
或許是因為精度的原因势似, 如果直接采樣上圖的點,繪制時如果有scale(縮放)操作僧著,可能會顯示不平滑履因;
所以在采樣心形線的點時我們放大一定比率(SCALE_FACTOR )。
就像一張圖片霹抛,如果分辨率是200x200搓逾, 縮小到100x100顯示,圖片還是清晰的杯拐,如果放大到400x400霞篡,可能會模糊。
public class Heart {
private static final Path PATH = new Path();
private static final float SCALE_FACTOR = 10f;
private static final float RADIUS = 18 * SCALE_FACTOR;
static {
// x = 16 sin^3 t
// y = 13 cos t - 5 cos 2t - 2 cos 3t - cos 4t
// http://www.wolframalpha.com/input/?i=x+%3D+16+sin%5E3+t%2C+y+%3D+(13+cos+t+-+5+cos+2t+-+2+cos+3t+-+cos+4t)
int n = 101;
Point[] points = new Point[n];
float t = 0f;
float d = (float) (2 * Math.PI / (n - 1));
for (int i = 0; i < n; i++) {
float x = (float) (16 * Math.pow(Math.sin(t), 3));
float y = (float) (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
points[i] = new Point(SCALE_FACTOR * x , -SCALE_FACTOR * y );
t += d;
}
PATH.moveTo(points[0].x, points[0].y);
for (int i = 1; i < n; i++) {
PATH.lineTo(points[i].x, points[i].y);
}
PATH.close();
}
public static Path getPath(){
return PATH;
}
public static float getRadius(){
return RADIUS;
}
}
封裝Bloom類
一片花瓣端逼,除了形狀之外朗兵,還有方位,顏色顶滩,方向余掖,大小等參數(shù)。
故此礁鲁,和Branch一樣盐欺,封裝了一個類。
花瓣的顏色和方向參數(shù)是隨機初始化的仅醇。
顏色方面冗美,ARGB中Red通道固定為最大值0xff, 效果就是花瓣的顏色為紅,紫析二,黃粉洼,白等。
因為要適應移動設(shè)備的多分辨率叶摄,所以一些參數(shù)要根據(jù)分辨率來動態(tài)設(shè)置属韧。
public class Bloom {
protected static float sMaxScale = 0.2f;
protected static int sMaxRadius = Math.round(sMaxScale * Heart.getRadius());
protected static float sFactor;
/**
* 初始化顯示參數(shù)
* @param resolutionFactor 根據(jù)屏幕分辨率設(shè)定縮放因子
*/
public static void initDisplayParam(float resolutionFactor){
sFactor = resolutionFactor;
sMaxScale = 0.2f * resolutionFactor;
sMaxRadius = Math.round(sMaxScale * Heart.getRadius());
}
Point position;
int color;
float angle;
float scale;
// 調(diào)速器,控制開花動畫的快慢
int governor = 0;
public Bloom(Point position) {
this.position = position;
this.color = Color.argb(CommonUtil.random(76, 255), 0xff, CommonUtil.random(255), CommonUtil.random(255));
this.angle = CommonUtil.random(360);
}
public boolean grow(Canvas canvas) {
if (scale <= sMaxScale) {
if((governor & 1) == 0) {
scale += 0.0125f * sFactor;
draw(canvas);
}
governor++;
return true;
} else {
return false;
}
}
protected float getRadius() {
return Heart.getRadius() * scale;
}
private void draw(Canvas canvas) {
Paint paint = CommonUtil.getPaint();
paint.setColor(color);
float r = getRadius();
canvas.save();
canvas.translate(position.x, position.y);
canvas.saveLayerAlpha(-r, -r, r, r, Color.alpha(color));
canvas.save();
canvas.rotate(angle);
canvas.scale(scale, scale);
canvas.drawPath(Heart.getPath(), paint);
canvas.restore();
canvas.restore();
canvas.restore();
}
}
三蛤吓、樹冠
樹冠是由數(shù)百片花瓣構(gòu)成宵喂,關(guān)鍵點在于確定這些花瓣的位置。
這里用到另一條心形線(x^2 + y^2 -1)^3 - x^2 * y^3 = 0会傲。
我們需要做的樊破,是在心形內(nèi)部選取位置愉棱,而非繪制曲線,故此哲戚,標準方程相對于參數(shù)方程更合適奔滑。
坐標系中的點(x,y), 計算ax+by, 大于0和小于0分別在直線的兩側(cè), x^2 + y^2 - r^2 則分別在圓外和圓內(nèi)顺少;
這個現(xiàn)象還蠻奇妙的朋其,雖然我不知道這在數(shù)學中叫什么-_-。
類似的脆炎,在x=[-c, c], y=[-c,c]的范圍內(nèi)隨機選取(x^2 + y^2 -1)^3 - x^2 * y^3<0的點梅猿,即可使得花瓣的位置錯落于心形線中。
private static float r;
private static float c;
/**
* 初始化參數(shù)
* @param canvasHeight 畫布的高度
* @param crownRadiusFactor 樹冠半徑的縮放因子
*/
public static void init(int canvasHeight, float crownRadiusFactor){
r = canvasHeight * crownRadiusFactor;
c = r * 1.35f;
}
public static void fillBlooms(List<Bloom> blooms, int num) {
int n = 0;
while (n < num) {
float x = CommonUtil.random(-c, c);
float y = CommonUtil.random(-c, c);
if (inHeart(x, y, r)) {
blooms.add(new Bloom(new Point(x, -y)));
n++;
}
}
}
private static boolean inHeart(float px, float py, float r) {
// (x^2+y^2-1)^3-x^2*y^3=0
float x = px / r;
float y = py / r;
float sx = x * x;
float sy = y * y;
float a = sx + sy - 1;
return a * a * a - sx * sy * y < 0;
}
繪制動畫
不斷地觸發(fā)onDraw()回調(diào)秒裕,在每一幀里面改變繪制參數(shù)袱蚓,就形成動畫了。
在這個例子中几蜻,劃分了幾個動畫階段喇潘,每個階段各自變化自己的參數(shù),到達一定的狀態(tài)就切換到下一階段梭稚。
總之颖低,就是分而治之,然后串聯(lián)起來弧烤。
public class TreeView extends View {
private static Tree tree;
public TreeView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
if(tree == null){
tree = new Tree(getWidth(), getHeight());
}
tree.draw(canvas);
// 這個函數(shù)只是標記view為invalidate狀態(tài)忱屑,并不會馬上觸發(fā)重繪;
// 標記invalidate狀態(tài)后暇昂,下一個繪制周期(約16s), 會回調(diào)onDraw()莺戒;
// 故此,要想動畫平滑流暢急波,tree.draw(canvas)需在16s內(nèi)完成脏毯。
postInvalidate();
}
}
public void draw(Canvas canvas) {
// 繪制背景顏色
canvas.drawColor(0xffffffee);
// 繪制動畫元素
canvas.save();
canvas.translate(snapshotDx + xOffset, 0);
switch (step) {
case BRANCHES_GROWING:
drawBranches();
drawSnapshot(canvas);
break;
case BLOOMS_GROWING:
drawBlooms();
drawSnapshot(canvas);
break;
case MOVING_SNAPSHOT:
movingSnapshot();
drawSnapshot(canvas);
break;
case BLOOM_FALLING:
drawSnapshot(canvas);
drawFallingBlooms(canvas);
break;
default:
break;
}
canvas.restore();
}
后記
- 調(diào)整參數(shù)消耗不少時間,寫代碼比較客觀幔崖,調(diào)參數(shù)則比較主觀:方位擺放,顯示大小渣淤,動畫快慢……
- 構(gòu)圖中左上角有留白赏寇,可以在那里輸出一些表白文字。
- 考慮到移動端的流量价认,動圖部分只截取最后一個階段的動畫嗅定。
- 篇幅限制,文中只是貼了部分代碼用踩,完整代碼可到github下載HeartTree渠退。