本文主要是介绍Android音律灯编写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很久之前写的代码了,现在贴出来,以便记忆
详细思路:就是使用两个for循环,然后计算出间隔,下面的格子是上面的倒影,所以计算出来上面格子的初始位置之后,再 加上计算出相对应下边格子的高度,就可以画出来就好了。下面的注意加上透明度
public class MusicView extends View {
private Paint imgPaint;
//测量控件的宽度
private int width;
//测量控件的高度
private int height;
//计算每一格的大小
private int perHeight,perWidth;
//上边的透明度
private int shTouming=99;
//下边的透明度
private int xiaTouming=33;
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);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int specMode=MeasureSpec.getMode(widthMeasureSpec);
int specSize=MeasureSpec.getSize(widthMeasureSpec);
width=specSize;
height=width/2;
perHeight=(height-dp2px(2))/24;
perWidth=(width-10*dp2px(10)-2*dp2px(30))/11;
setMeasuredDimension(width,height);
}
private void init() {
imgPaint=new Paint();
imgPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawMusic(canvas);
}
private void drawMusic(Canvas canvas) {
for (int i = 0; i < 11; i++) {
canvas.save();
if (i>0){
canvas.translate(dp2px(30)+perWidth*i+dp2px(10)*i,height/2);
}else {
//第一列不需要间隔
canvas.translate(dp2px(30)+perWidth*i,height/2);
}
for (int j = 0; j <(int)(2+10*Math.random()); j++) {
if (j>0){
canvas.translate(0,-perHeight-dp2px(2));
}else {
//第一排不需要间隔
canvas.translate(0,-perHeight);
}
switch (j){
case 0:{
imgPaint.setColor(Color.parseColor("#"+shTouming+"FF0000"));
break;
}
case 1:{
imgPaint.setColor(Color.parseColor("#"+shTouming+"FF2600"));
break;
}
case 2:{
imgPaint.setColor(Color.parseColor("#"+shTouming+"FF5100"));
break;
}
case 3:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-20)+"FF6F00"));
break;
}
case 4:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-25)+"FF9900"));
break;
}
case 5:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-30)+"FFB700"));
break;
}
case 6:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-35)+"FFC800"));
break;
}
case 7:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-40)+"FFF200"));
break;
}
case 8:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-45)+"EEFF00"));
break;
}
case 9:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-80)+"EEFF00"));
break;
}
case 10:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-85)+"EEFF00"));
break;
}
case 11:{
imgPaint.setColor(Color.parseColor("#"+(shTouming-88)+"EEFF00"));
break;
}
}
canvas.drawRect(0,0,perWidth,perHeight,imgPaint);
//倒影的编写
if (j==0){
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FF0000"));
canvas.drawRect(0,perHeight,perWidth,perHeight*2,imgPaint);
} else {
switch (j){
case 1:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FF2600"));
break;
}
case 2:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FF5100"));
break;
}
case 3:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FF6F00"));
break;
}
case 4:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FF9900"));
break;
}
case 5:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FFB700"));
break;
}
case 6:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FFC800"));
break;
}
case 7:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"FFF200"));
break;
}
case 8:{
imgPaint.setColor(Color.parseColor("#"+xiaTouming+"EEFF00"));
break;
}
case 9:{
imgPaint.setColor(Color.parseColor("#22EEFF00"));
break;
}
case 10:{
imgPaint.setColor(Color.parseColor("#11EEFF00"));
break;
}
case 11:{
imgPaint.setColor(Color.parseColor("#05EEFF00"));
break;
}
}
canvas.drawRect(0,2*j*(perHeight+dp2px(2))+perHeight,perWidth,2*j*(perHeight+dp2px(2))+2*perHeight,imgPaint);
}
}
canvas.restore();
}
}
private int sp2px(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,sp,getResources().getDisplayMetrics());
}
private int dp2px(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,dp,getResources().getDisplayMetrics());
}
public void startMusic(){
invalidate();
}
}
这篇关于Android音律灯编写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!