本文实例为大家分享了Android简单自定义音乐波动特效图的具体代码,供大家参考,具体内容如下
最终效果:
思路:就是绘制一个不断变化高度的矩形或者是宽虚线
1.自定义属性:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
2.编写自定义MusicPlayview
/** * 音乐播放波动动画 */ public class MusicPlayView extends View { //坐标原点x private float mBasePointX; //坐标原点y private float mBasePointY; //指针的数量 默认10 private int mPointNum; //指针间的间隙 默认5dp private float mPointSpace; //每个指针的宽度 默认5dp private float mPointWidth; //指针的颜色 private int mPointColor = Color.RED; //指针的集合 private ListmPoints; //控制开始/停止 private boolean mIsPlaying = false; //播放线程 private Thread mPlayThread; //指针波动速度 private int mPointSpeed; //画笔 private Paint mPaint; public MusicPlayView(Context context) { super(context); init(); } public MusicPlayView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //取出自定义属性 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.musicPlayViewAttr); mPointNum = ta.getInt(R.styleable.musicPlayViewAttr_point_num, 10); mPointWidth = dp2px(getContext(), ta.getFloat(R.styleable.musicPlayViewAttr_point_width, 5f)); mPointColor = ta.getColor(R.styleable.musicPlayViewAttr_point_color, Color.RED); mPointSpeed = ta.getInt(R.styleable.musicPlayViewAttr_point_speed, 40); init(); } public MusicPlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.musicPlayViewAttr); mPointNum = ta.getInt(R.styleable.musicPlayViewAttr_point_num, 10); mPointWidth = dp2px(getContext(), ta.getFloat(R.styleable.musicPlayViewAttr_point_width, 5f)); mPointColor = ta.getColor(R.styleable.musicPlayViewAttr_point_color, Color.RED); mPointSpeed = ta.getInt(R.styleable.musicPlayViewAttr_point_speed, 40); init(); } /** * 初始化画笔 */ private void init() { mPoints = new ArrayList<>(); //绘制虚线 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(mPointColor); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(mPointWidth); mPaint.setPathEffect(new DashPathEffect(new float[]{25, 15}, 0));//虚线间隔 setLayerType(LAYER_TYPE_SOFTWARE, null); } /** * 设置指针高度和即那个 */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); //获取逻辑原点的Y mBasePointY = getHeight() - getPaddingBottom(); Random random = new Random(); if (mPoints != null) mPoints.clear(); for (int i = 0; i
3.在activity_main2布局中使用MusicPlayView
<&#63;xml version="1.0" encoding="utf-8"&#63;>
4.MainActivity中使用
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener { private Button mBtPlay,mBtStop; private MusicPlayView mMusicPlayView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mMusicPlayView = findViewById(R.id.music_play); mBtPlay = findViewById(R.id.bt_play); mBtStop = findViewById(R.id.bt_stop); mBtPlay.setOnClickListener(this); mBtStop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_play: //开始播放 mMusicPlayView.start(); break; case R.id.bt_stop: //停止播放 mMusicPlayView.stop(); break; } } }
因为注释都挺详细的,就没有做太多的介绍,我这里也只是提供一个思路,里面有很多可以优化的地方比方说线程使用和循环的时候,如果有不懂的地方可以留言。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。