80,350
社区成员
发帖
与我相关
我的任务
分享
//当前的状态
private int type = TYPE_WIFI_DISCONNECT;//默认情况下是WIFI没有连接
//关闭或者启动动画
private boolean animationStatus = true;//默认启动
//定义最小需要的大小
private int width = 400;
private int height = 400;
//paint的着色器
private Shader wifiTypeShader;
private ObjectAnimator animator;
public CustomSignView(Context context) {
super(context);
}
public CustomSignView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//在这里引用自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomSignView);
type = typedArray.getInteger(R.styleable.CustomSignView_sign_status,TYPE_WIFI_DISCONNECT);
animationStatus = typedArray.getBoolean(R.styleable.CustomSignView_animation_status,true);
typedArray.recycle();
}
public CustomSignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
//如果是wifi没有连接,绘制一个从中心向外面辐射的红色圆形,表示wifi没有连接
switch (type) {
case TYPE_WIFI_DISCONNECT:
drawDisconnectWifi(paint, canvas);
break;
case TYPE_WIFI_ERROR:
drawErrorWifi(paint, canvas);
break;
case TYPE_WIFI_RIGHT:
drawRightWifi(paint,canvas);
break;
case TYPE_SIGN_SUCCESS:
drawSignSuccess(paint,canvas);
break;
}
}
//绘制WIFI没有连接的图案
private void drawDisconnectWifi(Paint paint, Canvas canvas) {
wifiTypeShader = new RadialGradient(width / 2, height / 2, width / 4, Color.WHITE, Color.RED, Shader.TileMode.CLAMP);
paint.setShader(wifiTypeShader);
canvas.drawCircle(width / 2, height / 2, width / 2, paint);
//绘制文字
drawWifiText("wifi未连接", "无法签到", canvas);
tipAnimation();
}
//启动动画
private void tipAnimation() {
if(animationStatus) {
animator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);
animator.setDuration(5000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setInterpolator(new LinearInterpolator());//匀速运动
animator.start();
}
}
//停止动画
public void stopAnimation(){
if(animator != null && !animator.isStarted()){
animator.cancel();
animator.end();
}
}
//定义最小需要的大小
private int width = 400;
private int height = 400;
//paint的着色器
private Shader wifiTypeShader;
private ObjectAnimator animator;
//当前的状态
private int type = TYPE_WIFI_DISCONNECT;//默认情况下是WIFI没有连接
//关闭或者启动动画
private boolean animationStatus = true;//默认启动
public CustomSignView(Context context) {
super(context);
}
public CustomSignView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//在这里引用自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomSignView);
type = typedArray.getInteger(R.styleable.CustomSignView_sign_status,TYPE_WIFI_DISCONNECT);
animationStatus = typedArray.getBoolean(R.styleable.CustomSignView_animation_status,true);
typedArray.recycle();
}
public CustomSignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
//如果是wifi没有连接,绘制一个从中心向外面辐射的红色圆形,表示wifi没有连接
switch (type) {
case TYPE_WIFI_DISCONNECT:
drawDisconnectWifi(paint, canvas);
break;
case TYPE_WIFI_ERROR:
drawErrorWifi(paint, canvas);
break;
case TYPE_WIFI_RIGHT:
drawRightWifi(paint,canvas);
break;
case TYPE_SIGN_SUCCESS:
drawSignSuccess(paint,canvas);
break;
}
}
//绘制WIFI没有连接的图案
private void drawDisconnectWifi(Paint paint, Canvas canvas) {
wifiTypeShader = new RadialGradient(width / 2, height / 2, width / 4, Color.WHITE, Color.RED, Shader.TileMode.CLAMP);
paint.setShader(wifiTypeShader);
canvas.drawCircle(width / 2, height / 2, width / 2, paint);
//绘制文字
drawWifiText("wifi未连接", "无法签到", canvas);
tipAnimation();
}
//启动动画
private void tipAnimation() {
if(animationStatus) {
animator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);
animator.setDuration(5000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setInterpolator(new LinearInterpolator());//匀速运动
animator.start();
}
}
//停止动画
public void stopAnimation(){
if(animator != null && !animator.isStarted()){
animator.cancel();
animator.end();
}
}
}
上面是View,这个View我在fargment中引用
private CustomSignView signView;
@Override
protected void initView() {
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_sign,null);
signView = view.findViewById(R.id.sv_sign_view);
}
@Override
protected void initListener() {
super.initListener();
signView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
super.onClick(v);
switch(v.getId()){
case R.id.sv_sign_view:
signView.stopAnimation();
break;
}
}
这个在fragment中调用