RadioButton自定义,实现底部导航消息数目提醒效果
2019-04-25 本文已影响0人
天敦敦
先看效果
3CD2C601-38FF-458A-A257-910A3796AA19.png创建styleable
1、在values文件夹下创建attrs.xml
<declare-styleable name="RemindRadioButton">
<attr name="remindTextSize" format="dimension"/>
<attr name="remindNumber" format="integer"/>
<attr name="remindTextColor" format="color"/>
<attr name="remindTextBackground" format="reference"/>
<attr name="remindTextBgColor" format="color"/>
<attr name="remindTextWidth" format="dimension"/>
<attr name="remindTextHeight" format="dimension"/>
<attr name="remindMarginRight" format="dimension"/>
</declare-styleable>
2、自定义RadioButton
package com.ww7h.purchasing.main.view;
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.util.AttributeSet;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/24 15:46
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class RemindRadioButton extends android.support.v7.widget.AppCompatRadioButton {
private float remindWidth;
private float remindHeight;
private float remindTextSize;
private int remindResourceId;
private int remindTextColor;
private int remindTextBgColor;
private int remindNumber = 0;
private float remindMarginRight;
public RemindRadioButton(Context context) {
super(context);
}
public RemindRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, com.ww7h.ww.common.R.styleable.RemindRadioButton, 0, 0);
remindMarginRight = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindMarginRight, 0);
remindWidth = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextWidth, 0);
remindHeight = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextHeight, 0);
remindTextSize = a.getDimension(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextSize, 0);
remindResourceId = a.getResourceId(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextBackground, 0);
remindTextColor = a.getColor(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextColor, Color.WHITE);
remindTextBgColor = a.getColor(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindTextBgColor, Color.RED);
remindNumber = a.getInt(com.ww7h.ww.common.R.styleable.RemindRadioButton_remindNumber, 0);
}
public RemindRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (remindNumber > 0) {
Paint paint = new Paint();
if (remindResourceId == 0) {
paint.setColor(remindTextBgColor);
}
Path path = new Path();
float minX = getWidth() - remindWidth - remindHeight / 2 - remindMarginRight;
float maxX = getWidth() - remindHeight / 2 - remindMarginRight;
// 绘制左侧圆形
path.addCircle(maxX,
remindHeight / 2, remindHeight / 2, Path.Direction.CW);
path.close();
canvas.drawPath(path, paint);
int count = 1;
// 当提醒消息数超过一位数时
if (remindNumber > 9) {
count = remindNumber > 99 ? 3 : 2;
// 绘制右侧半圆
path.addCircle(minX,
remindHeight / 2, remindHeight / 2, Path.Direction.CW);
path.close();
canvas.drawPath(path, paint);
path = new Path();
// 绘制提醒消息显示位置矩形
path.moveTo(minX, remindHeight);
path.lineTo(minX, 0);
path.lineTo(maxX, 0);
path.lineTo(maxX, remindHeight);
path.close();
canvas.drawPath(path, paint);
} else {
count = 1;
minX = getWidth() - remindHeight - remindMarginRight;
maxX = getWidth() - remindMarginRight;
path = new Path();
// 绘制消息显示矩形
path.moveTo(minX, remindHeight);
path.lineTo(minX, 0);
path.lineTo(maxX, 0);
path.lineTo(maxX, remindHeight);
path.close();
paint.setColor(Color.TRANSPARENT);
canvas.drawPath(path, paint);
}
paint.setStyle(Paint.Style.STROKE);
paint.setColor(remindTextColor);
paint.setTextSize(remindTextSize);
canvas.drawTextOnPath(String.valueOf(remindNumber), path,
remindHeight + Math.abs((remindWidth - remindTextSize / 2 * count )/2),
remindTextSize, paint);
}
}
public void setRemindNumber(int number) {
this.remindNumber = number;
invalidate();
}
public void setRemindMarginRight(float remindMarginRight) {
this.remindMarginRight = remindMarginRight;
invalidate();
}
public void setRemindWidth(float remindWidth) {
this.remindWidth = remindWidth;
invalidate();
}
public void setRemindHeight(float remindHeight) {
this.remindHeight = remindHeight;
invalidate();
}
}
3、布局中使用
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="number1"
type="Integer"/>
<variable
name="number2"
type="Integer"/>
<variable
name="number3"
type="Integer"/>
<variable
name="number4"
type="Integer"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/jump_next_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/b_nav_group"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_54"
android:orientation="horizontal">
<com.ww7h.purchasing.main.view.RemindRadioButton
android:id="@+id/b_nav_1"
android:checked="true"
android:textSize="@dimen/sp_12"
android:text="清单"
app:remindMarginRight="@dimen/dp_30"
app:remindNumber="@{number1}"
app:remindTextWidth="10dp"
app:remindTextHeight="10dp"
app:remindTextSize="8sp"
app:remindTextColor="@color/color_6f"
app:remindTextBgColor="@color/color_2f230"
android:textColor="@color/bottom_nav_text_color"
android:gravity="center"
android:drawableTop="@drawable/bottom_nav_check_icon1"
android:button="@null"
android:layout_weight="1"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content" />
<com.ww7h.purchasing.main.view.RemindRadioButton
android:id="@+id/b_nav_2"
android:textSize="@dimen/sp_12"
android:text="日程"
app:remindNumber="@{number2}"
app:remindTextWidth="5dp"
app:remindTextHeight="10dp"
app:remindTextSize="8sp"
app:remindTextColor="@color/color_6f"
app:remindTextBgColor="@color/color_2f230"
app:remindMarginRight="@dimen/dp_30"
android:textColor="@color/bottom_nav_text_color"
android:drawableTop="@drawable/bottom_nav_check_icon2"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content" />
<com.ww7h.purchasing.main.view.RemindRadioButton
android:id="@+id/b_nav_3"
android:textSize="@dimen/sp_12"
android:text="购买人"
app:remindNumber="@{number3}"
app:remindMarginRight="@dimen/dp_30"
app:remindTextWidth="10dp"
app:remindTextHeight="10dp"
app:remindTextSize="8sp"
app:remindTextColor="@color/color_6f"
app:remindTextBgColor="@color/color_2f230"
android:textColor="@color/bottom_nav_text_color"
android:drawableTop="@drawable/bottom_nav_check_icon3"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content" />
<com.ww7h.purchasing.main.view.RemindRadioButton
android:id="@+id/b_nav_4"
android:textSize="@dimen/sp_12"
android:text="邮寄"
app:remindNumber="@{number4}"
app:remindMarginRight="@dimen/dp_30"
app:remindTextWidth="10dp"
app:remindTextHeight="10dp"
app:remindTextSize="8sp"
app:remindTextColor="@color/color_6f"
app:remindTextBgColor="@color/color_2f230"
android:textColor="@color/bottom_nav_text_color"
android:drawableTop="@drawable/bottom_nav_check_icon4"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content" />
</RadioGroup>
</RelativeLayout>
</layout>
4、Activity调用
package com.ww7h.purchasing.main.view;
import android.content.Intent;
import android.view.View;
import com.ww7h.common.mvvm.v.BaseVActivity;
import com.ww7h.purchasing.R;
import com.ww7h.purchasing.databinding.ActivityMainDetailBinding;
import com.ww7h.ww.common.utils.DensityUtil;
import com.ww7h.ww.common.utils.LogUtil;
import com.ww7h.ww.common.utils.ScreenUtil;
/**
* ================================================
* 描述:
* 来源: Android Studio.
* 项目名: PurchasingTreasure
* 包名: com.ww7h.purchasing.main.view
* 创建时间: 2019/4/23 20:53
*
* @author ww Github地址:https://github.com/ww7hcom
* ================================================
*/
public class MainDetailActivity extends BaseVActivity<MainDetailActivity, ActivityMainDetailBinding> {
@Override
protected int getContentView() {
return R.layout.activity_main_detail;
}
@Override
protected void initAction() {
vdBinding.jumpNextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void initView() {
float width = ScreenUtil.Companion.getScreenWidth(activity) / 8f;
vdBinding.setNumber1(2);
vdBinding.bNav1.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav1.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.setNumber2(23);
vdBinding.bNav2.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav2.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 20));
vdBinding.setNumber3(999);
vdBinding.bNav3.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 15));
vdBinding.bNav3.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav3.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 25));
vdBinding.setNumber4(0);
vdBinding.bNav4.setRemindWidth(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindHeight(DensityUtil.INSTANCE.sp2px(activity, 10));
vdBinding.bNav4.setRemindMarginRight(width - DensityUtil.INSTANCE.sp2px(activity, 20));
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtil.e(TAG, "-------");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "-------");
}
}