自定义view随机数点击事件
2017-10-14 08:24
323 查看
public class MyView extends View implements View.OnClickListener{ int[] colors = new int[]{ Color.RED,Color.YELLOW,Color.GREEN }; int[] colores = new int[]{ Color.BLUE,Color.BLACK,Color.WHITE }; //背景色 private int bgColor; //文字色 private int textColor; //文字大小 private int textSize; private Paint paint; private Rect r; private String str = "4232"; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyViewAttrs,defStyleAttr,0); bgColor = ta.getColor(R.styleable.MyViewAttrs_bgColor, Color.BLACK); textColor = ta.getColor(R.styleable.MyViewAttrs_textColor,Color.WHITE); textSize = ta.getDimensionPixelSize(R.styleable.MyViewAttrs_textSize,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 30,getResources().getDisplayMetrics())); ta.recycle(); setOnClickListener(this); paint = new Paint(); r = new Rect(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if(widthMode == MeasureSpec.EXACTLY){ }else{ widthSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,getResources().getDisplayMetrics()); } if(heightMode == MeasureSpec.EXACTLY){ }else{ heightSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,getResources().getDisplayMetrics()); } setMeasuredDimension(widthSize,heightSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制矩形区域 paint.setColor(bgColor); paint.setStrokeWidth(3); paint.setAntiAlias(true); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint); //获取文字宽高,绘制文字 paint.setTextSize(textSize); paint.setColor(textColor); paint.getTextBounds(str,0,str.length(),r); canvas.drawText(str,getWidth()/2-r.width()/2,getHeight()/2+r.height()/2,paint); } //生成随机字符串 private String changeText(){ Random random = new Random(); String num = ""; for(int i = 0;i < 4;i++){ num = num + random.nextInt(10); } return num; } @Override public void onClick(View view) { str = changeText(); Random random = new Random(); int index = random.nextInt(3-1); bgColor = colors[index]; textColor=colores[index]; invalidate(); }
//自定义属性 <declare-styleable name="MyViewAttrs"> <attr name="bgColor" format="color">#dddddd</attr> <attr name="textColor" format="color">#ff0000</attr> <attr name="textSize" format="dimension">30</attr> </declare-styleable>
相关文章推荐
- 自定义TextView设置边框与背景颜色添加点击事件,点击更改随机数与背景
- 自定义ViewGroup (支持margin,gravity以及水平,垂直排列,滑动和点击事件)
- 自定义view,点击更换背景色与随机数
- Android自动识别<a>标签的自定义TextView(可自定义点击事件)
- 如何自定义RecyclerView的点击事件和绘制分割线-高级UI-2
- 项目——用自定义View在屏幕的左侧写上a~z,并加上点击事件
- Recyclerview的自定义点击事件
- Dialog的自定义view无法获取到点击事件
- Android实现RecyclerView自定义列表、点击事件以及下拉刷新
- Android自定义TextView链接点击和长按事件
- 关于在自定义View中添加Button按钮以及实现点击事件
- Android自定义TextView中的超链接点击事件处理
- UITableView增加自定义Button并绑定点击事件
- Android自动识别<a>标签的自定义TextView(可自定义点击事件)
- Recyclerview自定义点击事件
- AlertDialog中使用自定义View,其中的Button如何设置点击事件监听
- UITableView自定义cell上的按钮点击事件
- Android TextView 给文本中指定片段添加自定义点击事件
- 自定义view中控件的点击事件
- Android自定义布局系列之一:流式布局(含TextView的点击事件)