自定義某多多跑馬燈廣告圈2

效果

代碼:

public class MddPmdView2 extends View {

    Paint mPaint;
    private int mW;
    private int mH;
    private int mStockWidth = 10;
    private int mRadius = 50;
    private float mTrans = 0.05f;

    private float angle = 0;

    private int[] colors = null;

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

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

    public MddPmdView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mStockWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        MddPmdView2.this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startPlay(new int[]{Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE});
            }
        });
    }

    public void startPlay(int[] colors) {
        this.colors = new int[colors.length * 2 + 4];
        //double  兩截漸變顏色 + 兩截透明間隔
        for (int i = 0; i < this.colors.length; i++) {
            int nIndex = i % (this.colors.length / 2);
            this.colors[i] = nIndex < colors.length ? colors[nIndex] : Color.TRANSPARENT;
        }
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = w;
        mH = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (colors == null || colors.length <= 1) {
            return;
        }
        int nHalfWidth = mStockWidth / 2;//線寬的一半,以此計算繪製區域,避免出現被截斷或者遮擋
        SweepGradient sweepGradient = new SweepGradient(mW / 2, mH / 2, colors, getPositions());
        Matrix matrix = new Matrix();
        matrix.setRotate(angle, mW / 2, mH / 2);//以繪製區域中心爲遠點旋轉,並將旋轉角度遞增,達到轉圈的效果
        sweepGradient.setLocalMatrix(matrix);
        mPaint.setShader(sweepGradient);
        //繪製邊框
        canvas.drawRoundRect(new RectF(nHalfWidth, nHalfWidth, mW - nHalfWidth, mH - nHalfWidth), mRadius, mRadius, mPaint);

        angle += 1;//遞增旋轉角度
        invalidate();
    }

    /**
     * 計算每個顏色對應的區域,控制漸變和透明區域
     * @return
     */
    private float[] getPositions() {
        float[] result = new float[colors.length];
        int half = result.length / 2;
        float mult = (0.5f - mTrans) / (half - 3);
        for (int i = 0; i < result.length; i++) {
            float start = i < half ? 0 : 0.5f;
            if (colors[i] == Color.TRANSPARENT) {
                if (colors[i - 1] == Color.TRANSPARENT && i != result.length - 1) {
                    //第一個截斷區域,最後一個   透明
                    result[i] = 0.5f;
                } else {
                    //非第一個截斷區域最後一個,取值與集合內上一個值相同,可以避免出現漸變至透明的問題
                    result[i] = result[i - 1];
                }
            } else {
                //有顏色值時,按比例取值
                result[i] = (i % half) * mult + start;
            }
        }
        return result;
    }
}

上一篇:https://blog.csdn.net/qq_24179679/article/details/103644550

發佈了38 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章