android studio自定义视图

本文介绍了在Android Studio中自定义视图的方法,包括使用canvas.drawColor设置背景颜色,通过Paint对象设定画笔风格、线条宽度及抗锯齿效果,以及颜色的设置。还展示了如何在自定义视图MyRect中使用资源属性构造器处理XML属性,并在布局文件中引用该自定义控件。
摘要由CSDN通过智能技术生成

颜色四种常用的设置方式
canvas.drawColor(Color.RED);//系统颜色(Color.RED)

  canvas.drawColor(Color.rgb(100, 150, 200));//rgb
  canvas.drawColor(Color.argb(100, 100, 150, 200));//argb,其中第一参数为透明度
  canvas.drawColor(Color.parseColor("#44E21D"));//16进制

画板背景
canvas.drawColor(Color.BLUE)
画笔设置
实心或空心
paint.setStyle(Style.FILL);//实心
paint.setStyle(Style.STROKE);// 空心
粗细
paint.setStrokeWidth(10)
抗锯齿
paint.setAntiAlias(true);
颜色设置
paint.setColor(Color.parseColor(“#103864”));

自定义控件的设置

public class MyRect extends View {
// 资源属性的构造器,供资源解析器来访问(只要将xml资源文件放到values即可)
public MyRect(Context context, AttributeSet attrs){
super(context, attrs);
//获取配置属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);

    // 设置默认值
    int color = ta.getColor(R.styleable.MyView_rect_color, 0x00ff00);
    setBackgroundColor(color);

    ta.recycle();
}
public MyRect(Context context){
    super(context);
}

}

在布局文件中引用自定义的视图控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值