栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android自定义波浪加载动画的实现代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android自定义波浪加载动画的实现代码

本文实例为大家分享了android自定义波浪加载动画的具体代码,供大家参考,具体内容如下

效果图

1.自定义控件 WaveView

package com.example.wh.myapplication;
 
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
 
import java.text.DecimalFormat;
public class WaveView extends View {
 
 
 private final int WAVE_LENGTH1 = 600;
 
 
 private final int WAVE_HEIGHT1 = 30;
 
 
 private int mWaveHeight1 = WAVE_HEIGHT1;
 
 
 private int mWaveLenght1 = WAVE_LENGTH1;
 
 
 private final int WAVE_COLOR1 = Color.parseColor("#0000ff");
 
 
 private final int BORDER_COLOR = Color.parseColor("#800000ff");
 
 
 private final int DEFAULT_TEXT_COLOR = Color.parseColor("#ff0000");
 
 private final int DEFAULT_TEXT_SIZE = 30;
 
 
 private int mTextColor = DEFAULT_TEXT_COLOR;
 
 private int mTextSize = DEFAULT_TEXT_SIZE;
 
 
 private int mWaveColor1 = WAVE_COLOR1;
 
 
 private final int WAVE_OFFSET1 = 8;
 
 
 private int mOffset1 = WAVE_OFFSET1;
 
 
 private final int BORDER_WIDTH = 2;
 
 
 private int mBorderColor = BORDER_COLOR;
 
 
 private int mBorderWidth = BORDER_WIDTH;
 
 
 private float mPrecent = 0.5f;
 
 
 public enum ShowShape {
 RECT
 }
 
 
 private ShowShape mShape = ShowShape.RECT;
 
 
 private final int DEFAULT_TIME = 5;
 
 
 private int mTime = DEFAULT_TIME;
 
 
 public WaveView setTime(int time) {
 this.mTime = time;
 return this;
 }
 
 
 private Paint mWavePaint1;
 
 
 private Paint mBorderPaint;
 
 private Paint mTextPaint;
 
 
 private Path mWavePath1;
 
 
 private DecimalFormat mFormat;
 
 
 private int mWidth;
 
 private int mHeight;
 
 
 private float mChangeY;
 
 
 private float mFinalY;
 
 
 private int mWaveCount = 8;
 
 
 private boolean isReset = true;
 
 
 
 private float mCurrentPrecent = 0.0f;
 
 
 private int mMoveSum1;
 
 
 private boolean invalidateFlag = false;
 
 
 private PrecentChangeListener mPrecentChangeListener;
 
 
 
 public interface PrecentChangeListener {
 
 void precentChange(double precent);
 }
 
 
 public WaveView(Context context) {
 this(context, null);
 }
 
 public WaveView(Context context, @Nullable AttributeSet attrs) {
 this(context, attrs, 0);
 }
 
 public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 
 initAttrs(context, attrs); // 获取布局文件中dingy9i的属性
 init();
 }
 
 //获取布局中的初始属性
 private void initAttrs(Context context, AttributeSet attrs) {
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveView);
 mWaveLenght1 = typedArray.getInteger(R.styleable.WaveView_wave1Length, WAVE_LENGTH1);
 mWaveHeight1 = typedArray.getInteger(R.styleable.WaveView_wave1Height, WAVE_HEIGHT1);
 mWaveColor1 = typedArray.getColor(R.styleable.WaveView_wave1Color, WAVE_COLOR1);
 mOffset1 = typedArray.getInteger(R.styleable.WaveView_wave1Offset, WAVE_OFFSET1);
 
 mBorderWidth = typedArray.getDimensionPixelSize(R.styleable.WaveView_borderWidth, BORDER_WIDTH);
 mBorderColor = typedArray.getColor(R.styleable.WaveView_borderColor, BORDER_COLOR);
 
 mTime = typedArray.getInteger(R.styleable.WaveView_intervalTime, DEFAULT_TIME);
 mPrecent = typedArray.getFloat(R.styleable.WaveView_precent, 0.5f);
 
 int shapevalue = typedArray.getInteger(R.styleable.WaveView_showShape, 0);
 
 mShape = ShowShape.RECT;
 typedArray.recycle();
 }
 
 private void init() {
 mWavePaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
 mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 
 mWavePath1 = new Path();
 
 mWavePaint1.setColor(mWaveColor1);
 mWavePaint1.setStyle(Paint.Style.FILL);
 
 mBorderPaint.setColor(mBorderColor);
 mBorderPaint.setStrokeWidth(mBorderWidth);
 mBorderPaint.setStyle(Paint.Style.STROKE);
 
 
 mTextPaint.setColor(mTextColor);
 mTextPaint.setTextSize(mTextSize);
 
 // 定义数字显示个格式
 mFormat = new DecimalFormat("###,###,##0.00");
 }
 
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 super.onSizeChanged(w, h, oldw, oldh);
 mWidth = w;
 mHeight = h;
 
 mChangeY = mHeight;
 // 计算波峰个数,为了实现移动效果,保证至少绘制两个波峰
 mFinalY = (1 - mPrecent) * mHeight; // 计算水位最终高度
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 mWavePath1.reset();
 
 
 if (mBorderWidth > 0) {
 // 边框大于0,表示需要绘制边框
 if(mShape == ShowShape.RECT) {
 canvas.drawRect(0, 0, mWidth, mHeight, mBorderPaint);
 }
 }
 
 mWavePath1.moveTo(-mWaveLenght1, mChangeY);
 
 if (!isReset) { // 判断重置标记
 // 利用贝塞尔曲线绘制波浪
 for (int i = 0; i < mWaveCount; i++) {
 // 绘制波浪1的曲线
 mWavePath1.quadTo((-mWaveLenght1 * 3 / 4) + (i * mWaveLenght1) + mMoveSum1, mChangeY + mWaveHeight1, (-mWaveLenght1 / 2) + (i * mWaveLenght1) + mMoveSum1, mChangeY);
 mWavePath1.quadTo((-mWaveLenght1 * 1 / 4) + (i * mWaveLenght1) + mMoveSum1, mChangeY - mWaveHeight1, (i * mWaveLenght1) + mMoveSum1, mChangeY);
 
 }
 
 // 不断改变高度,实现逐渐水位逐渐上涨效果
 mChangeY -= 1;
 if (mChangeY < mFinalY) mChangeY = mFinalY;
 
 // 波峰1往右移动,波峰2往左移动
 mMoveSum1 += mOffset1;
 if (mMoveSum1 >= mWaveLenght1) mMoveSum1 = 0;
 
 // 填充矩形,让上涨之后的水位下面填充颜色
 mWavePath1.lineTo(mWidth, mHeight);
 mWavePath1.lineTo(0, mHeight);
 mWavePath1.close();
 
 canvas.drawPath(mWavePath1, mWavePaint1);
 } else {
 // 是重置
 canvas.drawColor(Color.TRANSPARENT);
 }
 
 // 计算当前的百分比
 mCurrentPrecent = 1 - mChangeY / mHeight;
 // 格式化数字格式
 String format1 = mFormat.format(mCurrentPrecent);
 // 绘制文字,将百分比绘制到界面。此处用的是 "50%" 的形式,可以根据需求改变格式
 double parseDouble = Double.parseDouble(format1);
 canvas.drawText((int) (parseDouble * 100) + " %", (mWidth - mTextPaint.measureText(format1)) / 2, mHeight / 5, mTextPaint);
 // 监听对象不为null并且没有达到设置高度时,调用监听方法
 if (mPrecentChangeListener != null && mChangeY != mFinalY) {
 mPrecentChangeListener.precentChange(parseDouble);
 }
 
 //高度到达设置高度
 if (mChangeY == mFinalY){
 canvas.drawColor(ContextCompat.getColor(getContext(), R.color.bowencolor));
 }
 
 // 判断绘制标记
 if (invalidateFlag) postInvalidateDelayed(mTime);
 }
 
 
 public WaveView setBorderWidth(int borderWidth) {
 this.mBorderWidth = borderWidth;
 return this;
 }
 
 
 public WaveView setWaveColor1(int waveColor1) {
 this.mWaveColor1 = waveColor1;
 return this;
 }
 
 
 public WaveView setBorderColor(int borderColor) {
 this.mBorderColor = borderColor;
 return this;
 }
 
 
 public WaveView setTextColor(int textColor) {
 this.mTextColor = textColor;
 return this;
 }
 
 
 public WaveView setPrecent(float precent) {
 this.mPrecent = precent;
 return this;
 }
 
 
 
 public WaveView setTextSize(int textSize) {
 this.mTextSize = textSize;
 return this;
 }
 
 
 
 public WaveView setShape(ShowShape shape) {
 this.mShape = shape;
 return this;
 }
 
 
 public void start() {
 invalidateFlag = true;
 isReset = false;
 postInvalidateDelayed(mTime);
 }
 
 
 public void stop() {
 invalidateFlag = false;
 isReset = false;
 }
 
 
 public void reset() {
 invalidateFlag = false;
 isReset = true;
 mChangeY = mHeight;
 postInvalidate();
 }
}

2.attrs



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

3.布局



 
 
 
 
 
 

4.MainActivity

package com.example.wh.myapplication;
 
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
 private WaveView waveview1;
 private Button btStart;
 private Button btStop;
 private Button btReset;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 waveview1 = (WaveView) findViewById(R.id.waveview1);
 
 btStart = (Button) findViewById(R.id.bt_start);
 btStop = (Button) findViewById(R.id.bt_stop);
 btReset = (Button) findViewById(R.id.bt_reset);
 
 // 代码设置相关属性
 waveview1.setBorderWidth(2)
 .setWaveColor1(Color.RED)
 .setBorderColor(Color.GREEN)
 .setTextColor(Color.BLUE)
 .setShape(WaveView.ShowShape.RECT)
 .setTextSize(36)
 .setPrecent(1f)//设置水波纹的百分比
 .setTime(2);
 
 btStart.setonClickListener(new View.onClickListener() {
 @Override
 public void onClick(View v) {
 waveview1.start();
 }
 });
 
 btStop.setonClickListener(new View.onClickListener() {
 @Override
 public void onClick(View v) {
 waveview1.stop();
 }
 });
 
 btReset.setonClickListener(new View.onClickListener() {
 @Override
 public void onClick(View v) {
 waveview1.reset();
 }
 });
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号