首先看一下效果图,不太会用动态的,就将就看一下图吧
这个一般适用于网络加载数据,网慢时,出现的小动画,
由于是音频条,所以在音乐播放器的项目中常用,
我也是在做音乐类项目的时候,找的这个
其实很简单 , 只需要一个自定义view就好
下面就是我写的自定义view
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.util.Log; import android.view.View; /** * 自定义view * 作者:聂 */ public class AudioView extends View{ private int width; private int height; private int rectCount = 10; private Paint paint; private int offset; private int rectWidth; public AudioView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画矩形 for (int i=0;i<rectCount;i++) { canvas.drawRect((float)(width*0.1+rectWidth*i+offset), (float)getRectHight(), (float)(width*0.1+rectWidth*(i+1)), (float)height, paint); } postInvalidateDelayed(300);//300毫秒刷新 } private double getRectHight() { double h = 0; h = Math.random()*height; return h; } private void init() { paint = new Paint(); paint.setColor(Color.RED); rectWidth = width/15; offset = width/30; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = measureWidth(widthMeasureSpec);//得到View的宽 height = measureHight(heightMeasureSpec);//得到View的高 setMeasuredDimension(width,height); init();//初始化 } private int measureWidth(int widthMeasureSpec) { int result = 0; int specMode = MeasureSpec.getMode(widthMeasureSpec); int specSize = MeasureSpec.getSize(widthMeasureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { result = 200; if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } private int measureHight(int heightMeasureSpec) { int result = 0; int specMode = MeasureSpec.getMode(heightMeasureSpec); int specSize = MeasureSpec.getSize(heightMeasureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { result = 200; if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }自定义view完了就可以在main里面直接调用就好了
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <com.example.dell.zidingyidh.AudioView android:layout_width="match_parent" android:layout_height="match_parent" /> <com.example.dell.zidingyidh.AudioView android:layout_width="100dp" android:layout_height="100dp" /> </FrameLayout>这里一定要注意的就是要写自己的包名
com.example.dell.zidingyidh.AudioView
本demo为转载的,
希望对各位老铁有帮助