自定义View为view添加选中边框
2016-10-31 18:11
357 查看
有这样一个需求,需要再itemView选中或者点击之后,给它添加一个选中效果,当然,现在开发框架那么多,尤其是tv开发框架,用MainUpView就可以实现,但是我偏要用自定义的view实现呢?很简单,一个方法几句话就可以实现。
1、效果图:
2、分析代码:
需要自定义一个View,代码是ElementView.java:
可以看到
ok,这样就能实现如上效果图的选中的白色框和动画了。
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,这样就能实现如上效果图的选中的白色框和动画了。
相关文章推荐
- UITableView 自定义cell添加UIView作为分割线,选中消失的问题
- 自定义TextView设置边框与背景颜色添加点击事件,点击更改随机数与背景
- 自定义TextView之 给你的TextView添加边框
- ImageView添加选中边框
- Android-如何给View添加边框,边框颜色和线的粗细可以自定义
- 自定义View之自定义按键圆角边框背景添加点击效果
- UITableViewCell添加自定义view,在cell选中高亮时,view消失,取消高亮时,view又出现
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
- Android使用自定义ImageView为图片添加边框
- 给UIImageview 添加边框 或者设置成圆角-ios5貌似不能用?
- Android--为TextView添加边框
- ViewFlipper添加自定义View失去焦点问题解决
- Android--为TextView添加边框
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- 在iPhone应用的table cell中添加自定义布局view
- 给UIImageview 添加边框 或者设置成圆角
- 给UIImageview 添加边框 或者设置成圆角
- iOS创建不带nib文件的工程之四--在自定义view上添加按钮