Android(安卓)自定义充电动画
16lz
2021-12-04
不多说,直接上代码
1 首先创建类 ,类名自定义我这边是 直接 BubbleViscosity。
public class BubbleViscosity extends SurfaceView implements SurfaceHolder.Callback, Runnable { private static ScheduledExecutorService scheduledThreadPool; private static String texta; private Context context; private String paintColor = "#25DA29";// 不透明圆弧的颜色 private String centreColor = "#00000000"; // 中间圆的颜色 private String minCentreColor = "#9025DA29"; //透明的圆弧 private int screenHeight; private int screenWidth; private float lastRadius; // 底部半圆的半径 private float rate = 0.32f;// 底部曲线的控制点 private float rate2 = 0.32f;// 底部曲线的控制点 private PointF lastCurveStrat = new PointF();//底部圆的起点坐标 private PointF lastCurveEnd = new PointF();//底部圆的结束坐标 private PointF centreCirclePoint = new PointF();// 中间圆的坐标 private float centreRadius;//中间圆的 半径 private float bubbleRadius; // 所有圆弧的坐标数组 private PointF[] arcPointStrat = new PointF[8]; private PointF[] arcPointEnd = new PointF[8]; private PointF[] control = new PointF[8]; private PointF arcStrat = new PointF(); private PointF arcEnd = new PointF(); private PointF controlP = new PointF(); // 气泡的其实点保存集合 List bubbleList = new ArrayList<>(); List bubbleBeans = new ArrayList<>(); private int rotateAngle = 0; //旋转的角度 private float controlrate = 1.66f; // 圆弧的控制点 private float controlrateS = 1.3f; // 可变圆弧的控制点 private int i = 0;// 无限循环的下标 private SurfaceHolder mHolder; private float scale = 0;// 圆得开口值 private Paint arcPaint; private Paint minCentrePaint; private Paint bubblePaint; private Paint centrePaint; private Paint lastPaint; private Path lastPath; // 所有的路劲 private Random random; private Paint textPaint; private Rect rect; public BubbleViscosity(Context context) { this(context, null); } public BubbleViscosity(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public BubbleViscosity(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; initzi(texta); initTool(); } public static BubbleViscosity initzi(String text1) { texta=text1; return null; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); screenHeight = getMeasuredHeight(); screenWidth = getMeasuredWidth(); setBubbleList(); } private void initTool() { rect = new Rect(); mHolder = getHolder();//获取SurfaceHolder对象 mHolder.addCallback(this);//注册SurfaceHolder的回调方法 setFocusable(true); // 设置焦点 //TODO:保证该SurfaceView在最上层,避免两个SurfaceView叠加,遮挡问题 mHolder.setFormat(PixelFormat.TRANSPARENT); setZOrderOnTop(true); lastRadius = dip2Dimension(40f, context);//底部圆的半径 centreRadius = dip2Dimension(100f, context);//中间圆的半径 bubbleRadius = dip2Dimension(10f, context);//气泡的半径 random = new Random(); //底部圆 lastPaint = new Paint(); lastPaint.setAntiAlias(true); lastPaint.setStyle(Paint.Style.FILL); lastPaint.setColor(Color.parseColor(paintColor)); lastPaint.setStrokeWidth(2); lastPath = new Path(); //中间圆的画笔 centrePaint = new Paint(); centrePaint.setAntiAlias(true); centrePaint.setStyle(Paint.Style.FILL); centrePaint.setStrokeWidth(2); centrePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); centrePaint.setColor(Color.parseColor(centreColor)); // 不透明圆弧的画笔 arcPaint = new Paint(); arcPaint.setAntiAlias(true); arcPaint.setStyle(Paint.Style.FILL); arcPaint.setColor(Color.parseColor(paintColor)); arcPaint.setStrokeWidth(2); // 透明圆弧的画笔 minCentrePaint = new Paint(); minCentrePaint.setAntiAlias(true); minCentrePaint.setStyle(Paint.Style.FILL); minCentrePaint.setColor(Color.parseColor(minCentreColor)); minCentrePaint.setStrokeWidth(2); // 气泡的画笔 bubblePaint = new Paint(); bubblePaint.setAntiAlias(true); bubblePaint.setStyle(Paint.Style.FILL); bubblePaint.setColor(Color.parseColor(minCentreColor)); bubblePaint.setStrokeWidth(2); //文字画笔 textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setStyle(Paint.Style.FILL); textPaint.setColor(Color.parseColor("#00FF00")); textPaint.setStrokeWidth(2); textPaint.setTextSize(dip2Dimension(40f, context)); }
2 这中间有个 BubbleBean 是自己生成出来的
public class BubbleBean { private float randomY=3; private float x; private float y; private int index; public BubbleBean(float x, float y,float randomY,int index) { this.x = x; this.y = y; this.randomY = randomY; this.index = index; } public void set(float x, float y,float randomY,int index){ this.x = x; this.y = y; this.randomY = randomY; this.index = index; } public void setMove(int screenHeight,int maxDistance){ if (y-maxDistance<110){ this.y-=2; return; } if (maxDistance<=y&&screenHeight-y>110){ this.y-=randomY; }else { this.y-=0.6;//气泡开始的移动的时候比较慢,造成气泡开始的 黏性气泡 } if (index==0){ this.x-=0.4; }else if (index==2){ this.x+=0.4; } } public int getIndex(){ return index; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; }}
3 在你想要引用的XML上面直接引用
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
到这里也就好了,只是显示电量的数字是直接定义好的,大家可以更改的 ,如果想要获取更全面的代码,欢迎大家去关注一下我公众号 在这里我会不定时更新一些新的东西
更多相关文章
- Android自定义进度条
- Android井字棋
- Android圆环形自定义进度条控件的绘制
- android 自定义进度条
- canvas 画一个圆上有 旋转角度 的直线
- android 微信 listview 气泡
- Android(安卓)shape
- Android(安卓)白板代码实现
- Android之十三刮刮卡中奖功能