天天看點

自定義View之 繼承View2

類似音頻播放器的動感條

自定義View之 繼承View2
package com.example.administrator.youku_animi.MyView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/7/24 0024.
 */

public class NewView2 extends View
{
    int mRectCount = ;
    int mWidth =  ;
    int offset = ;
    double mRandom;
    //矩形條的寬度
    int mRectWidth =;
    int mRectHight = ;
    Paint mPaint;
    LinearGradient mLinearGradient;
    public NewView2(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mPaint = new Paint();
        //給畫筆設定顔色
        mPaint.setColor(getResources().getColor(android.R.color.holo_blue_bright));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        for (int i = ; i < mRectCount; i++)
        {
            mRandom = Math.random();
            float currentHight = (float) (mRectHight * mRandom);
            canvas.drawRect(
                    (float) (mWidth *  /  + mRectWidth * i + offset),
                    currentHight,
                    (float) (mWidth *  /  + mRectWidth * (i + )),
                    mRectHight,
                    mPaint
            );
        }

        postInvalidateDelayed();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mRectHight = getHeight();
        mRectWidth = (int) (mWidth *  / mRectCount);
        mLinearGradient = new LinearGradient(
                ,
                ,
                mRectWidth,
                mRectHight,
                Color.YELLOW,
                Color.BLUE,
                Shader.TileMode.CLAMP
        );
        mPaint.setShader(mLinearGradient);
    }
}
           

繼續閱讀