自定义控件继承View------Music

来源:互联网 发布:数据整理的英语 编辑:程序博客网 时间:2024/05/19 05:41

这里写图片描述

public class MusicView extends View {

private Paint paint;private int count;private float offetx;private float left;private float right;public MusicView(Context context) {    super(context);    init();}public MusicView(Context context, @Nullable AttributeSet attrs) {    super(context, attrs);    init();}public MusicView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    init();}private  void init(){    paint = new Paint();    paint.setColor(Color.RED);    paint.setStyle(Paint.Style.FILL);    paint.setStrokeWidth(3);    count = 20;    offetx = 40;    left = 20;    right = 50;}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(w, h, oldw, oldh);    LinearGradient gradient = new LinearGradient(            0,            0,            left ,            getHeight(),            new int[]{Color.RED , Color.GREEN , Color.BLUE},            null,            Shader.TileMode.CLAMP    );    Log.d("onSizeChanged:  " , "jig: " + getHeight() );    paint.setShader(gradient);}@Overrideprotected void onDraw(Canvas canvas) {    super.onDraw(canvas);    /*        android 坐标系是左上角为原点 。 详情见下期的博客 Android 坐标系详解        这里我们只需要控制x坐标 , 以及右下角的坐标。 左上角的y坐标控制music的显示高度     */    for(int i = 0; i < count; i++){        RectF rect = new RectF(left ,                (float)(Math.random() * 800 + 20),                right,                820);        canvas.drawRect(rect , paint);        left += offetx;        right += offetx;    }    left -= offetx * count;    right -= offetx * count;    this.postInvalidateDelayed(300);}

}

0 0
原创粉丝点击