Android - 自定義view【2】(WIFI動(dòng)畫(huà))

1.效果圖

GIF.gif

2.具體實(shí)現(xiàn)代碼

繪制這個(gè)扇形和弧線,首先需要去創(chuàng)建一個(gè)自定義的view重寫(xiě)它的onDraw()方法,在繪制之前可以在view創(chuàng)建的時(shí)候先將畫(huà)筆初始化出來(lái)。

具體的難點(diǎn)在于第二步如何動(dòng)態(tài)去繪制,可以定義一個(gè)具體的數(shù)值比如 shouldExistSignalSize 來(lái)控制每次繪制的時(shí)候繪制哪個(gè)信號(hào),從最開(kāi)始的時(shí)候只繪制第一個(gè)信號(hào)(也就是扇形),接著第二次繪制的時(shí)候需要繪制第一個(gè)和第二個(gè)信號(hào),往后就是第一個(gè)信號(hào),第二個(gè)信號(hào),第三個(gè)信號(hào);當(dāng)四格信號(hào)都繪制完畢的時(shí)候記得將 shouldExistSignalSize 重置。

package cc.willread.www.viewapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class WIFIView extends View {
    public WIFIView(Context context) {
        this(context, null);
    }

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

    public WIFIView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Paint paint;
    /**
     * 初始化準(zhǔn)備
     */
    private void init() {
        paint = new Paint();
        //畫(huà)筆顏色
        paint.setColor(Color.BLACK);
        //畫(huà)筆粗細(xì)
        paint.setStrokeWidth(6);
        //抗鋸齒
        paint.setAntiAlias(true);

    }


    /**WIFI控件寬高較小一邊的長(zhǎng)度*/
    private int wifiLength;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        wifiLength = Math.min(w, h);
    }

    /**
     * 開(kāi)始角度
     */
    private float startAngle = -135;
    /**
     * 扇形或者弧的旋轉(zhuǎn)角度
     */
    private float sweepAngle = 90;
    /**
     * 信號(hào)大小,默認(rèn)4格
     */
    private int signalSize = 4;

    /**每次應(yīng)該繪制的信號(hào)個(gè)數(shù)*/
    private float shouldExistSignalSize = 0f;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        shouldExistSignalSize++;
        if(shouldExistSignalSize>4){
            shouldExistSignalSize=1;
        }
        canvas.save();
        //計(jì)算最小的扇形信號(hào)所在的圓的半徑
        float signalRadius = wifiLength/2/signalSize;
        //向下平移畫(huà)布,保證繪制的圖形能夠完全顯示
        canvas.translate(0,signalRadius);
        for (int i = 0; i < signalSize; i++) {
            if(i>=signalSize-shouldExistSignalSize) {
                //定義每個(gè)信號(hào)所在圓的半徑
                float radius = signalRadius * i;
                RectF rectf = new RectF(radius, radius, wifiLength - radius, wifiLength - radius);
                if (i < signalSize - 1) {
                    paint.setStyle(Paint.Style.STROKE);
                    canvas.drawArc(rectf, startAngle, sweepAngle, false, paint);
                } else {
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawArc(rectf, startAngle, sweepAngle, true, paint);
                }
            }
        }
        canvas.restore();

        if(!isDetached) {
            try {
                Thread.sleep(500);
                invalidate();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**
     * 自定義控件是否脫離窗體
     */
    private boolean isDetached;

    /**
     * 當(dāng)自定義控件脫離窗體,即將銷(xiāo)毀
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isDetached = true;
    }
}


3.添加自定義view

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <cc.willread.www.viewapp.WIFIView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

https://www.willread.cc/p/5be507828a71a

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,958評(píng)論 2 59
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,954評(píng)論 25 709
  • 自定義繪制 自定義繪制的方式是重寫(xiě)繪制方法,其中最常用的是 onDraw() 繪制的關(guān)鍵是 Canvas 的使用C...
    android小菜雞一枚閱讀 873評(píng)論 1 1
  • 洞天”意謂山中有洞室通達(dá)上天,貫通諸山。東晉《道跡經(jīng)》云:“五岳及名山皆有洞室?!彼惺笊蕉疵c十大洞天一一對(duì)應(yīng)...
    蒼穹一韻閱讀 2,900評(píng)論 0 0
  • 你說(shuō) 遇到了 你要去 翻山越嶺 哪怕 哪怕遍山荊棘 你也要去 去追隨 那吐著鮮血的玫瑰 何時(shí)成了最?lèi)?ài) 而我終將守著...
    擱淺墨染流年閱讀 190評(píng)論 0 0

友情鏈接更多精彩內(nèi)容