美文网首页
使用java代码动态设置渐变的背景图

使用java代码动态设置渐变的背景图

作者: 爱在记忆消失前 | 来源:发表于2018-08-14 09:53 被阅读203次

    package com.njsoft.ace.shoppingCart.adapter;

    import android.annotation.SuppressLint;
    import android.graphics.drawable.GradientDrawable;
    import android.support.v4.content.ContextCompat;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import com.chad.library.adapter.base.BaseQuickAdapter;
    import com.chad.library.adapter.base.BaseViewHolder;
    import com.njsoft.ace.R;

    import java.util.List;
    import java.util.Random;

    public class PaymentSelectAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private int selectPosition = -1;

    public PaymentSelectAdapter(int layoutResId, List<String> list) {
        super(layoutResId, list);
    }
    
    /**
     * 选择
     *
     * @param selectPosition 当前位置
     */
    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }
    
    /**
     * 删除某位置的数据
     */
    public void deletePosition(int deletePosition) {
        getData().remove(deletePosition);
        notifyItemRemoved(deletePosition);
    }
    
    @Override
    protected void convert(BaseViewHolder helper, String item) {
        ImageView imageView = helper.getView(R.id.Card_Img);
        RelativeLayout cardLayout = helper.getView(R.id.Card_Layout);
        if (selectPosition == helper.getAdapterPosition()) {
            imageView.setImageResource(R.mipmap.radiobutton);
        } else {
            imageView.setImageResource(R.mipmap.radiobutton_default);
        }
        //获取随机颜色
        int colors[] = { getColor(), getColor()};
        /**
        *渐变的方向,颜色
        **/
        GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
       //设置圆角
        gd.setCornerRadius(20);
      //设置背景
        cardLayout.setBackground(gd);
    }
    

    //获取随机颜色
    private int getColor() {
    Random random = new Random();
    return 0xff000000 | random.nextInt(0x00ffffff);
    }

    }

    相关文章

      网友评论

          本文标题:使用java代码动态设置渐变的背景图

          本文链接:https://www.haomeiwen.com/subject/tpjmbftx.html