====
代码实现
====
第一步:编写自定义属性
res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>第二步:编写自定义java类
package com.wust.jingdutiao;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import androidx.annotation.Nullable;
/**
-
ClassName: MyLodingView
-
Description:
-
date: 2021/7/21 15:59
-
@author yiqi
-
@QQ 1820762465
-
@微信 yiqiideallife
-
@技术交流QQ群 928023749
*/
public class MyLoadingView extends View {
private int rect_color_one;
private int rect_color_two;
private int rect_color_three;
private int rect_color_four;
private int rect_color_five;
private Paint rect_one_paint;
private Paint rect_two_paint;
private Paint rect_three_paint;
private Paint rect_four_paint;
private Paint rect_five_paint;
private int mWidth;
private int mHeight;
private float[] mHeightRate = {1/16.0f,1/10.0f,1/8.0f};
private int HORIZONTAL_OFFSET = 5;
private int bg_default_color;
private ValueAnimator va;
public MyLoadingView(Context context) {
super(context);
}
public MyLoadingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
initPaint();
initAnima();
}
public MyLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
initPaint();
initAnima();
}
//设置 属性动画
private void initAnima() {
va = ValueAnimator.ofInt(0, 4);
va.setDuration(3000);
va.setRepeatCount(ValueAnimator.INFINITE);
va.setRepeatMode(ValueAnimator.RESTART);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
setRectColorByNum(value);
}
});
postDelayed(new Runnable() {
@Override
public void run() {
va.start();
}
},500);
}
private void initAttrs(Context context, AttributeSet attrs) {
//获取用户传来的五种颜色
TypedArray ty = context.obtainStyledAttributes(attrs, R.styleable.MyLoadingView);
rect_color_one = ty.getColor(R.styleable.MyLoadingView_loading_color_one, Color.parseColor(“#325AB4”));
rect_color_two = ty.getColor(R.styleable.MyLoadingView_loading_color_two, Color.parseColor(“#3C8CFF”));
rect_color_three = ty.getColor(R.styleable.MyLoadingView_loading_color_three, Color.parseColor(“#888888”));
rect_color_four = ty.getColor(R.styleable.MyLoadingView_loading_color_four, Color.parseColor(“#00C8D2”));
rect_color_five = ty.getColor(R.styleable.MyLoadingView_loading_color_five, Color.parseColor(“#78E6DC”));
//获取背景色
try {
ColorDrawable bg = (ColorDrawable) getBackground();
bg_default_color = bg.getColor();
}catch (Exception e){
bg_default_color = Color.WHITE;
}
ty.recycle();
}
//初始化画笔
private void initPaint() {
rect_one_paint = getPaintByColor(rect_color_one);
rect_two_paint = getPaintByColor(rect_color_two);
rect_three_paint = getPaintByColor(rect_color_three);
rect_four_paint = getPaintByColor(rect_color_four);
rect_five_paint = getPaintByColor(rect_color_five);
}
private Paint getPaintByColor(int Color) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
//让其为正方形,并且宽高不能小于40
mWidth = mHeight = Math.max(Math.min(mWidth, mHeight),dp2px(100));
setMeasuredDimension(mWidth, mHeight);
}
private int dp2px(int value) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,getResources().getDisplayMetrics());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制矩形
drawRect(canvas);
}
private void drawRect(Canvas canvas) {
int centerX = mWidth/2;
int centerY = mHeight/2;
RectF rectOne = new RectF(centerX-HORIZONTAL_OFFSET,
centerY-mWidth*mHeightRate[0],
centerX+HORIZONTAL_OFFSET,
centerY+mWidth*mHeightRate[0]);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
0066)]
[外链图片转存中…(img-B7PS4go3-1715843320067)]
[外链图片转存中…(img-S1ECn6n3-1715843320069)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!