小白上手-自定義風扇型Loading控件

最近手頭有些時間,就逛逛知乎看看有什么好玩的。結(jié)果發(fā)現(xiàn)一個帖子說是冷門網(wǎng)站的,就進去看了看,結(jié)果發(fā)現(xiàn)一個有趣的網(wǎng)站: codewars

個人感覺這是個挺有趣的網(wǎng)站,它里面有很多種語言,用戶可以選擇自己想要挑戰(zhàn)的語言,糾錯!然后里面也有互動社區(qū),同一道題,往往會有多個解法,可以在社區(qū)里看到別人成熟簡潔的解題方式和思路,這也是對個人編程能力的一種提升。

然后就說說為什么會想要做這么一個自定義View吧,在玩codewars的過程中,我發(fā)現(xiàn)它左上角有個有趣的圖標,就是這個


codewars.png

當網(wǎng)頁有刷新的時候,這個圖標就像風扇一樣轉(zhuǎn)呀轉(zhuǎn),嘿嘿,還挺有趣,于是,自己也想著做一個出來。

然后我仿著做著一個出來,效果是這樣的。


GIF.gif

啊~說到這個GIF我要吐槽一下了,真真啊,第一次錄GIF,各種不順利,不是效果出不來就不理想,最后調(diào)呀調(diào),錄啊錄,最后才選擇這一個上傳的。好了,廢話不多說,先說說我的實現(xiàn)思路吧。

實現(xiàn)思路

  • 先畫外面的圓角矩形
  • 再畫中間那個小圓圈
  • 最后畫扇葉
  • 讓扇葉轉(zhuǎn)起來

view的測量部分就不介紹了,都是常規(guī)代碼。我就說說里面扇葉的部分和控制扇葉轉(zhuǎn)動以及轉(zhuǎn)動速度這部分吧。

canvas.rotate(-rotateDegree, mWidth / 2, mHeight / 2);//控制風扇轉(zhuǎn)速
for (int i = 0; i < fanCount; i++) { 
  canvas.drawArc(rectFan, -90, -180, true, bgPaint);    
  canvas.rotate(90, mWidth / 2, mHeight / 2);}
if (running)
  rotateHandler.sendEmptyMessageDelayed(1, 10);//控制風扇轉(zhuǎn)動

一開始先旋轉(zhuǎn)一下畫布,是為了使扇葉有個旋轉(zhuǎn)偏移量,已制造出旋轉(zhuǎn)的效果,同時還能控制轉(zhuǎn)速的效果。
for循環(huán)內(nèi)部就是畫出四片扇葉。
然后handler呢,主要是用來持續(xù)刷新view,已實現(xiàn)轉(zhuǎn)動的效果。

FanLoading.java
package com.kenny.fcmpushtest.UI.customView;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.os.Handler;
    import android.os.Message;
    import android.util.AttributeSet;
    import android.view.View;

    import com.kenny.fcmpushtest.R;

    /**
     * Created by Kenny on 2016/10/19 10:33.
     * Desc:
     */
    public class FanLoading extends View {
        private static final String TAG = FanLoading.class.getSimpleName();
        private int mWidth;
        private int mHeight;
        private int defaultWidth = 200;
        private int defaultHeight = 200;
        private int withinRadius;//內(nèi)圓半徑
        private int fanRadius;//畫風扇葉的半徑
        private int fanCount = 4;//扇葉數(shù)

        //畫圖相關(guān)
        private Paint bgPaint;//畫底色和畫風扇
        private int bgColor;
        private Paint circlePaint;//內(nèi)圓畫筆
        private int circleColor;
        private RectF rect;//外部圓角矩形
        private RectF rectFan;//扇葉畫圖范圍

        private int rotateDegree = 0;//旋轉(zhuǎn)角度,用于旋轉(zhuǎn)畫布,實現(xiàn)動態(tài)旋轉(zhuǎn)效果
        private int speed = 10;//旋轉(zhuǎn)速率
        private boolean running;

        private Handler rotateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (!running)
                    return;
                rotateDegree += speed;
                if (rotateDegree == Integer.MAX_VALUE)
                    rotateDegree = 0;
                postInvalidate();
            }
        };

        public FanLoading(Context context) {
            this(context, null);
        }

        public FanLoading(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public FanLoading(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FanLoading);
            bgColor = ta.getColor(R.styleable.FanLoading_rectBackgroundColor, Color.parseColor("#982C22"));
            circleColor = ta.getColor(R.styleable.FanLoading_circleColor, Color.parseColor("#ff0000"));
            speed = ta.getInt(R.styleable.FanLoading_fanSpeed, 10);
            running = ta.getBoolean(R.styleable.FanLoading_autoStart, false);
            ta.recycle();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            mWidth = getMyDefaultSize(defaultWidth, widthMeasureSpec);
            mHeight = getMyDefaultSize(defaultHeight, heightMeasureSpec);
            withinRadius = mWidth / 2 - 20;
            fanRadius = withinRadius - 10;
            setMeasuredDimension(mWidth, mHeight);
            init();
        }

        private void init() {
            bgPaint = new Paint();
            bgPaint.setColor(bgColor);
            bgPaint.setAntiAlias(true);
            bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);

            circlePaint = new Paint();
            circlePaint.setColor(circleColor);
            circlePaint.setAntiAlias(true);
            circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);

            rect = new RectF(0, 0, mWidth, mHeight);
            float left = mWidth / 2 - fanRadius / 2;
            float top = mHeight / 2 - fanRadius;
            float right = mWidth / 2 + fanRadius / 2;
            float bottom = mHeight / 2;
            rectFan = new RectF(left, top, right, bottom);
        }

        private int getMyDefaultSize(int size, int measureSpec) {
            int result = size;
            //獲得測量模式
            int specMode = View.MeasureSpec.getMode(measureSpec);
            //獲得測量大小
            int specSize = View.MeasureSpec.getSize(measureSpec);
            //判斷模式是否是 EXACTLY
            if (specMode == View.MeasureSpec.EXACTLY) {
                //如果模式是 EXACTLY 則直接使用specSize的測量大小
                result = specSize;
            } else {
                //如果是其他兩個模式,先設(shè)置一個默認大小值 200
                //如果是 AT_MOST 也就是 wrap_content 的話,就取默認值 200 和 specSize 中小的一個為準。
                if (specMode == View.MeasureSpec.AT_MOST) {
                    result = Math.min(result, specSize);
                }
            }
            return result;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawRoundRect(rect, 20, 20, bgPaint);
            canvas.drawCircle(mWidth / 2, mHeight / 2, (mHeight / 2) - 20, circlePaint);
            canvas.rotate(-rotateDegree, mWidth / 2, mHeight / 2);
            for (int i = 0; i < fanCount; i++) {
                canvas.drawArc(rectFan, -90, -180, true, bgPaint);
                canvas.rotate(90, mWidth / 2, mHeight / 2);
            }
            if (running)
                rotateHandler.sendEmptyMessageDelayed(1, 10);
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            running = false;
        }

        public void start() {
            running = true;
            postInvalidate();
        }

        public void stop() {
            running = false;
            rotateHandler.removeMessages(1);
        }

        public void setRectBackgroundColor(int color) {
            bgColor = color;
            bgPaint.setColor(bgColor);
            postInvalidate();
        }

        public void setCircleColor(int color) {
            circleColor = color;
            circlePaint.setColor(circleColor);
            postInvalidate();
        }

        public void setSpeed(int s) {
            speed = s;
        }

        public boolean isRunning() {
            return running == true;
        }
    }

自定義屬性部分

attr.xml
<?xml version="1.0" encoding="utf-8"?>
    <resources>

        <declare-styleable name="FanLoading">
            <attr name="rectBackgroundColor" format="color" />
            <attr name="circleColor" format="color" />
            <attr name="fanSpeed" format="integer" />
            <attr name="autoStart" format="boolean" />
        </declare-styleable>

    </resources>

恩,然后就大概這么多吧。

總結(jié)

總的來說,這個自定義view差不多已經(jīng)是實現(xiàn)codewars的那個圖標效果了,但是還是有一些沒能實現(xiàn),比如它中間并不是一個圓,而是像花瓣一樣的。
整個過程花了差不多一天的時間,代碼也有許多需要優(yōu)化的地方,希望大家多多指教。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,695評論 2 370

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,582評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,147評論 4 61
  • 文/嘯谷 01 又有一個親戚的親戚倒下了,27歲,企業(yè)白領(lǐng),送到醫(yī)院時雙腎衰竭,并發(fā)多處臟器感染,生命垂危,孩子僅...
    嘯谷閱讀 635評論 0 10
  • 養(yǎng)貓的感覺就像談戀愛,養(yǎng)貓這個“愛情故事”要從一年前談起了,去年元旦家里領(lǐng)導(dǎo)帶我出去逛街,偶然的機會看到了柱子,從...
    柱先森閱讀 654評論 0 2
  • 如果我們注定要在一起。我何不提早行使我的權(quán)利。何以琛是這么說的吧
    豬貓比巴卜閱讀 139評論 0 0