Android RedioButton、RadioGroup底部添加动画位移横线下划线



最近公司做项目用到一个RadioGrop底部添加下划线的需求,索性封装一个工具类得了,
给能用的上的码农及懒得算的童鞋节约一点时间,如果感觉能用麻烦给点个赞不枉我辛苦一场。
不说了直接贴代码,代码我是测过了的,赶时间的直接复制
ImageViewAnimationHelper类。


在RadioGrop切换的时候直接调用startAnimation方法传入下标就行了,非常简单。
注意 : 构造中的moveNum和radioButton数量是对应的,不然距离会有问题
lineWithdp是线的宽度,高度是在布局中指定的。



public class ImageViewAnimationHelper {

private Context context;
private float lineWith;
private float moveNum;
private float oldMoveNum;
private float distance;
private float fromXDelta;
private float toXDelta;
private float oldXDelta;
private ImageView imageView;
private boolean isInit = true;

/**
 * 
 * @param context
 * @param imageView
 * @param moveNum 一共需要移动几次
 * @param lineWithdp 下划线的宽度,单位dp
 */
public ImageViewAnimationHelper(Context context, ImageView imageView, float moveNum, float lineWithdp) {
    this.context = context;
    this.moveNum = moveNum;
    this.imageView = imageView;
    this.lineWith = lineWithdp * context.getResources().getDisplayMetrics().density;
    init();
}

/**
 * 初始化
 */
private void init() {
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageView.getLayoutParams();
    //每段间隔距离
    float surplus = context.getResources().getDisplayMetrics().widthPixels - (lineWith * moveNum);
    distance = surplus / (moveNum * 2);
    params.width = (int) lineWith;
    imageView.setLayoutParams(params);
    //刷新
    imageView.requestLayout();
    startAnimation(0);
}

/***
 * 
 * @param moveNum 移动第几个 从0开始下标、不计间隔
 */
public void startAnimation(int moveNum) {

    if (moveNum > this.moveNum) {
        throw new RuntimeException("IndexOutOf Exception ");
    }

    //计算fromXDelta toXDelta
    if (oldMoveNum < moveNum | (oldMoveNum == 0 & isInit) | moveNum == 0) {//右移、初始化
        isInit = false;
        fromXDelta = oldXDelta;
        toXDelta = distance * (2 * moveNum) + lineWith * moveNum + distance;
        oldXDelta = toXDelta;
    } else {//左移
        fromXDelta = oldXDelta;
        toXDelta = distance * (2 * moveNum) + lineWith * moveNum + distance;
        oldXDelta = toXDelta;
    }

    oldMoveNum = moveNum;
    /**
     float fromXDelta 动画开始的点离当前View X坐标上的差值
     float toXDelta 动画结束的点离当前View X坐标上的差值
     float fromYDelta 动画开始的点离当前View Y坐标上的差值
     float toYDelta 动画开始的点离当前View Y坐标上的差值
     */
    Animation animation = new TranslateAnimation(fromXDelta, toXDelta, 0f, 0f);

    animation.setDuration(300);
    animation.setFillAfter(true);
    imageView.startAnimation(animation);
}

}



这里写图片描述


public class MainActivity extends AppCompatActivity {

ImageView instructionsIv;
RadioGroup collectionRadioGroup;

ImageViewAnimationHelper helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_collection);
    instructionsIv = (ImageView) findViewById(R.id.instructionsIv);
    collectionRadioGroup = (RadioGroup) findViewById(R.id.collectionRadioGroup);

    helper = new ImageViewAnimationHelper(this, instructionsIv, 3, 100);
    collectionRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            switch (i) {
                case R.id.collectionNewCarRb:
                    helper.startAnimation(0);
                    break;
                case R.id.collectionUsedCarRb:
                    helper.startAnimation(1);
                    break;
                case R.id.collectionServiceCarRb:
                    helper.startAnimation(2);
                    break;
            }
        }
    });
}

}


实现后的效果图

话说离丈女娘的距离就是一座房子,苦逼的码农哟!

搞定!轻松加愉快(#^.^#)

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值