类似音频播放器的动感条
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 = 20;
int mWidth = 20 ;
int offset = 10;
double mRandom;
//矩形条的宽度
int mRectWidth =10;
int mRectHight = 50;
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(4);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for (int i = 0; i < mRectCount; i++)
{
mRandom = Math.random();
float currentHight = (float) (mRectHight * mRandom);
canvas.drawRect(
(float) (mWidth * 0.4 / 2 + mRectWidth * i + offset),
currentHight,
(float) (mWidth * 0.4 / 2 + mRectWidth * (i + 1)),
mRectHight,
mPaint
);
}
postInvalidateDelayed(300);
}
@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 * 0.6 / mRectCount);
mLinearGradient = new LinearGradient(
0,
0,
mRectWidth,
mRectHight,
Color.YELLOW,
Color.BLUE,
Shader.TileMode.CLAMP
);
mPaint.setShader(mLinearGradient);
}
}