美文网首页
自定义view

自定义view

作者: 水固态中 | 来源:发表于2017-12-02 16:21 被阅读0次

xml引用自定义view 包名+控件名

<com.mycompany.myapphuizhi.NewButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

新建类,继承view,重写ondraw方法

public class NewButton extends View {

  Paint paint = new Paint();//画笔

        //注意构造函数,最起码要实现这个attrs属性从xml传入

  public NewButton(Context context,  AttributeSet attrs) {

  super(context, attrs);

  }

  @Override

  public void draw(Canvas canvas) {

  //canvas画布

  super.draw(canvas);

        paint.setColor(Color.YELLOW);

  canvas.drawCircle(300, 300, 200, paint);

  }

}

canvas画布

paint画笔

Paint.setStyle(Style style) 设置绘制模式

Paint.setColor(int color) 设置颜色

Paint.setStrokeWidth(float width) 设置线条宽度

Paint.setTextSize(float textSize) 设置文字大小

Paint.setAntiAlias(boolean aa) 设置抗锯齿

默认黑色

自定义view

相关文章

网友评论

      本文标题:自定义view

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