自定义View为view添加选中边框

有这样一个需求,需要再itemView选中或者点击之后,给它添加一个选中效果,当然,现在开发框架那么多,尤其是tv开发框架,用MainUpView就可以实现,但是我偏要用自定义的view实现呢?很简单,一个方法几句话就可以实现。

1、效果图:

这里写图片描述

这里写图片描述

2、分析代码:

需要自定义一个View,代码是ElementView.java:

/**
 * ElementView 2016-10-28
 */
public class ElementView extends ImageView {

    /****** 画笔工具 ******/
    private Paint mPaint;

    /**
     * @param context
     */
    public ElementView(Context context) {
        super(context);
        mPaint = new Paint();

        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     */
    public ElementView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        // TODO Auto-generated constructor stub
    }

    /**
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public ElementView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        // TODO Auto-generated constructor stub
    }

    @SuppressLint({ "DrawAllocation", "NewApi" })
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        // view获取焦点后执行
        if (hasFocus()) {

            // 设置画笔抗锯齿
            mPaint.setAntiAlias(true);
            // 画笔颜色,就是边框颜色,白色不透明(可以设置其他自己喜欢的边框颜色),0xffffffff为不透明,0x00ffffff为透明
            mPaint.setColor(0xffffffff);
            // 设置画笔的风格,stroke为边框模式,fill为填充内部区域模式,fill_and_stroke是填充加边框,全覆盖
            mPaint.setStyle(Style.STROKE);
            // 设置画笔边框的宽度
            mPaint.setStrokeWidth(4f);
            // 设置画笔的透明色
            // mPaint.setAlpha(200);

            Rect rect = new Rect();
            // 获取当前view显示的区域座标
            getGlobalVisibleRect(rect);
            // getLocalVisibleRect(rect);
            int left = 2;
            int top = 2;
            int right = rect.right - rect.left - 2;
            int bottom = rect.bottom - rect.top - 2;
            RectF rectf = new RectF(left, top, right, bottom);

            // 用画笔画一个带圆角的矩形框,3.0f为圆角的弧度
            canvas.drawRoundRect(rectf, 3.0f, 3.0f, mPaint);
            // canvas.drawRect(left, top, right, bottom, mPaint);
            Log.e("=====onDraw=====", "left: " + left + ",top: " + top
                    + ",right: " + right + ",bottom: " + bottom);
        } else {
        }
    }
}

可以看到hasFocus()判断了一下,是否获取到了焦点,如果获取焦点执行画框部分代码,然后动画放大效果的代码如下:

if (hasFocus) {
//view获取焦点后执行放大动画
// 放大,视图动画
    Animation animation = AnimationUtils.loadAnimation(
                        getActivity(), R.anim.scale);
    view.startAnimation(animation);
    view.bringToFront();

    //这里是属性动画的透明度设置
    // ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha",
    // 1.0f, 0.6f, 1.0f).setDuration(1200);
    // alpha.setRepeatMode(ValueAnimator.RESTART);
    // alpha.setRepeatCount(ValueAnimator.INFINITE);
    // alpha.start();

} else {

    // 缩小
    Animation animation = AnimationUtils.loadAnimation(
                        getActivity(), R.anim.smallscale);
    view.startAnimation(animation);
}

ok,这样就能实现如上效果图的选中的白色框和动画了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章