幾條曲線構(gòu)建Android表白程序

想起之前看到的一段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渠退。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忙迁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子碎乃,更是在濱河造成了極大的恐慌姊扔,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梅誓,死亡現(xiàn)場離奇詭異恰梢,居然都是意外死亡,警方通過查閱死者的電腦和手機梗掰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門嵌言,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人及穗,你說我怎么就攤上這事摧茴。” “怎么了埂陆?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵苛白,是天一觀的道長。 經(jīng)常有香客問我猜惋,道長丸氛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任著摔,我火速辦了婚禮缓窜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谍咆。我一直安慰自己禾锤,他們只是感情好,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布摹察。 她就那樣靜靜地躺著恩掷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪供嚎。 梳的紋絲不亂的頭發(fā)上黄娘,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機與錄音克滴,去河邊找鬼逼争。 笑死,一個胖子當著我的面吹牛劝赔,可吹牛的內(nèi)容都是我干的誓焦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼着帽,長吁一口氣:“原來是場噩夢啊……” “哼杂伟!你這毒婦竟也來了移层?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赫粥,失蹤者是張志新(化名)和其女友劉穎观话,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體傅是,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡匪燕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了喧笔。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帽驯。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖书闸,靈堂內(nèi)的尸體忽然破棺而出尼变,到底是詐尸還是另有隱情,我是刑警寧澤浆劲,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布嫌术,位于F島的核電站,受9級特大地震影響牌借,放射性物質(zhì)發(fā)生泄漏度气。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一膨报、第九天 我趴在偏房一處隱蔽的房頂上張望磷籍。 院中可真熱鬧,春花似錦现柠、人聲如沸院领。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽比然。三九已至,卻和暖如春周循,著一層夾襖步出監(jiān)牢的瞬間强法,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工湾笛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留饮怯,地道東北人。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓迄本,卻偏偏與公主長得像,于是被迫代替她去往敵國和親课竣。 傳聞我的和親對象是個殘疾皇子嘉赎,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容