筆記20171127-Android仿滴滴呼叫車(chē)時(shí)轉(zhuǎn)圈圈的View

需求:

類(lèi)似滴滴打車(chē),呼叫車(chē),但是司機(jī)還未接單,等待計(jì)時(shí)的自定義View

要達(dá)到的效果:

轉(zhuǎn)圈圈.jpeg

hongyang大神的自定義View教程中的思路實(shí)現(xiàn)的

相關(guān)代碼:

1.attrs.xml(這個(gè)文件里放的是自定義view的屬性)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="firstColor" format="color"/>
    <attr name="secondColor" format="color"/>
    <attr name="circleWidth" format="dimension"/>
    <attr name="speed" format="integer"/>
    <attr name="textSize" format="dimension"/>
    <attr name="textColorFirst" format="color"/>
    <attr name="textColorSecond" format="color"/>

    <declare-styleable name="circle">
        <attr name="firstColor"/>
        <attr name="secondColor"/>
        <attr name="circleWidth"/>
        <attr name="speed"/>
        <attr name="textSize"/>
        <attr name="textColorFirst"/>
        <attr name="textColorSecond"/>
    </declare-styleable>
</resources>
2.自定義的View 我給它起名叫CircleView(反正就是個(gè)圓)
package weekimwee.cn.circleviewdemo;

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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * Created by Wee Kim Wee on 2017/10/23.
 */

public class CircleView extends View {
    private int firstColor;
    private int secondColor;
    private int circleWidth;
    private int speed;
    private int textSize;
    private float progress;
    private boolean isNext;
    private String bigCircleText;
    private int bigCircleTextColor;
    private int time;
    private DrawThread drawThread;
    private SimpleDateFormat formatter = new SimpleDateFormat("mm分ss秒");
    private final String LOADING ="已等待";
    private Paint paint;
    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.circle, defStyleAttr, 0);
        int n = a.getIndexCount();
        for(int i = 0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.circle_firstColor:
                    firstColor = a.getColor(attr, Color.GRAY);
                    break;
                case R.styleable.circle_secondColor:
                    secondColor = a.getColor(attr,Color.GRAY);
                    break;
                case R.styleable.circle_circleWidth:
                    circleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.circle_speed:
                    speed=a.getInt(attr, 20);
                    break;
                case R.styleable.circle_textSize:
                    textSize =a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.circle_textColorFirst:
                    bigCircleTextColor = a.getColor(attr,Color.BLACK);
                    break;
            }
        }
        a.recycle();
        bigCircleText = "00分00秒";

        paint = new Paint();
        drawThread = new DrawThread();
        drawThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //大圓
        int centre = getWidth() / 2; // 獲取圓心的x坐標(biāo)
        int radius = (centre - circleWidth / 2)-25;// 半徑
        paint.setStrokeWidth(circleWidth); // 設(shè)置圓環(huán)的寬度
        paint.setAntiAlias(true); // 消除鋸齒
        paint.setStyle(Paint.Style.STROKE); // 設(shè)置空心
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定義的圓弧的形狀和大小的界限

        //跟著跑的小圓
        float h = (float) (2*( Math.PI / 360) * progress);
        float x = (float) ( radius+25+Math.sin(h) * radius);
        float y = (float) (radius+25-Math.cos(h) * radius);
        //在180度位置上的圓
        //float hc = (float) (2*( Math.PI / 360) * 180);
        //float xc = (float) ( radius+25+Math.sin(hc) * radius);
        //float yc = (float) (radius+25-Math.cos(hc) * radius);

        //大圓邊框繪制顏色
        if (!isNext) {// 第一顏色的圈完整,第二顏色跑
            paint.setColor(firstColor); // 設(shè)置圓環(huán)的顏色
            canvas.drawCircle(centre, centre, radius, paint); // 畫(huà)出圓環(huán)
            paint.setColor(secondColor); // 設(shè)置圓環(huán)的顏色
            canvas.drawArc(oval, -90, progress, false, paint); // 根據(jù)進(jìn)度畫(huà)圓弧

            paint.setColor(secondColor);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(x,y,10,paint);
            //paint.setColor(firstColor);
            //paint.setStyle(Paint.Style.FILL);
            //canvas.drawCircle(xc,yc,10,paint);
        } else {
            paint.setColor(secondColor); // 設(shè)置圓環(huán)的顏色
            canvas.drawCircle(centre, centre, radius, paint); // 畫(huà)出圓環(huán)
            paint.setColor(firstColor); // 設(shè)置圓環(huán)的顏色
            canvas.drawArc(oval, -90, progress, false, paint); // 根據(jù)進(jìn)度畫(huà)圓弧

            paint.setColor(firstColor);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(x,y,8,paint);
            //paint.setColor(secondColor);
            //paint.setStyle(Paint.Style.FILL);
            //canvas.drawCircle(xc,yc,10,paint);
        }


        paint.setTextSize(textSize);
        //已等待
        float loadingTextWidth = paint.measureText(LOADING);
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(1);
        canvas.drawText(LOADING,centre-loadingTextWidth/2,centre-textSize,paint);
        //幾分幾秒
        float bigCircleTextWidth = paint.measureText(bigCircleText);
        paint.setColor(bigCircleTextColor);
        canvas.drawText(bigCircleText, centre - bigCircleTextWidth / 2, centre + textSize , paint);
    }

    public void stop(){
        drawThread.close();
    }

    class DrawThread extends Thread {

        private boolean isRun = true;

        @Override
        public void run() {
            while (isRun) {
                BigDecimal b1 = new BigDecimal(Float.toString(progress));
                BigDecimal b2 = new BigDecimal(Float.toString(0.3f));
                progress = b1.add(b2).floatValue();
                if (progress == 360) {
                    progress = 0;
                    if (!isNext)
                        isNext = true;
                    else
                        isNext = false;
                }
                time = time+speed;
                postInvalidate();
                bigCircleText = formatter.format(time);
                try {
                    Thread.sleep(speed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void close() {
            isRun = false;
        }
    }
}
3.activity_main.xml布局中使用上述View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="weekimwee.cn.circleviewdemo.MainActivity">

    <TextView
        android:layout_alignParentTop="true"
        android:padding="20dp"
        android:id="@+id/helloworld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <weekimwee.cn.circleviewdemo.CircleView
        android:id="@+id/view"
        android:layout_centerInParent="true"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:firstColor="#ff4500"
        app:secondColor="#d3d3d3"
        app:circleWidth="2dp"
        app:speed="25"
        app:textSize="20sp"
        app:textColorFirst ="#ff4500"
        app:textColorSecond="#d3d3d3"
        />
</RelativeLayout>
4.MainActivity.java中使用View開(kāi)始計(jì)時(shí)
package weekimwee.cn.circleviewdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        helloworld.setOnClickListener {
            view.stop()
        }
    }
}

實(shí)現(xiàn)思路:

必要條件:
  • 空心大圓,兩種顏色
  • 實(shí)心的小圓,也是兩種顏色
  • 計(jì)時(shí)線程,30秒小圓要繞著大圓走一圈并更換顏色,大圓中間的text顯示計(jì)時(shí)
具體代碼中的實(shí)現(xiàn):
  • 在構(gòu)造方法中把屬性獲取下來(lái)
  • 計(jì)時(shí)的線程開(kāi)始啟動(dòng),線程中的邏輯:
    360度的大圓,每25毫秒轉(zhuǎn)0.3f度,轉(zhuǎn)1200次剛好轉(zhuǎn)完,剛好30秒(因?yàn)槲覜](méi)有用動(dòng)畫(huà),直接是在View中重繪的,如果是3度這樣的,會(huì)顯的一卡一卡的不平滑)
  • onDraw方法:
    (1)先畫(huà)大圓(大圓位置是固定的)
    (2)計(jì)算小圓的位置(每次重繪角度已經(jīng)變了,所以要重新計(jì)算小圓的位置)
    (3)大圓邊框繪制顏色,以及繪制小圓(小圓顏色與大圓邊框一致)
    (4)繪制大圓中間的字
  • stop方法
    這個(gè)方法供外部調(diào)用停止計(jì)時(shí)線程,例如呼叫到車(chē)了,這個(gè)頁(yè)面finish了之類(lèi)的

以上是這個(gè)呼叫車(chē)計(jì)時(shí)等待View的筆記,demo在這里在這里

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

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