Android自定义控件CustomView1

public class MyView extends View {

    public MyView(Context context) {

        super(context);

        init();

    }

    public MyView(Context context, AttributeSet attrs) {

        super(context,attrs);

        init();

    }

    protected void onDraw(Canvas canvas) {        //    画布

        super.onDraw();

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

        paint.setColor(Color.BLUE);        //    画笔颜色    Color.TRANSPARENT:透明色

        paint.setTextSize(20);            //    字体大小

        paint.setStrokeWidth(30);

        paint.setAntiAlias(true);

        paint.setStyle(Paint.Style.STROKE);        //空心    FILL:填充

 

        int width = getWidth();

        int height = getHeight();

        canvas.drawLine(0, 0, width, height, paint);

        canvas.drawCircle(width/2, height/2, 50, paint);

        canvas.drawRect(0, 0, 200, 100, paint);

        Rect rect = new Rect(0, 0, 200, 100);

        canvas.drawRect(rect, paint);

        RectF rectF = new RectF(100, 100, 200, 100);

        canvas.drawArc(rectF, 0, 270, true, paint);

        canvas.drawRoundRect(0, 0, 200, 100, 20, 30, paint);

        Bitmap bitmap = BitmapFactory.decodeResource(getResource(), R.mimap.xxx);

        canvas.drawBitmap(bitmap, width/2, 0, paint);

        canvas.drawText("文本", 0, 20, paint);

    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if(widthMode == MeasureSpec.EXACTLY        //    match_parent或固定值

           && heightMode == MeasureSpec.AT_MOST) {        //    wrap_content    }

        int width = MeasureSpec.getSize(widthMeasureSpec);

        if(widthMode == MeasureSpec.AT_MOST) {    width = 100;    }

        if(heightMode == MeasureSpec.AT_MOST) {    height = 100;    }

        setMeasureDimension(width, height);

    }

    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();

        if(action == MotionEvent.ACTION_DOWN) {

 

        } else if(action == MotionEvent.ACTION_MOVE) {

 

        } else if(action == MotionEvent.ACTION_UP) {

  

        }

    }

}

invalidate();        //更新控件的绘制

postinvalidate();        //子线程调用

只有继承自View,onDraw()才会被调用

可以通过在自定义控件的布局文件中使用 `<layout>` 标签来启用 DataBinding,然后在代码中使用 DataBindingUtil 类来绑定数据。 例如,假设我们有一个自定义控件 MyCustomView,它的布局文件为 custom_view.xml,我们想要绑定一个名为 `text` 的字符串属性。我们可以这样做: 1. 在 custom_view.xml 中使用 `<layout>` 标签包裹布局文件的根布局: ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 自定义控件的布局 --> </LinearLayout> </layout> ``` 2. 在 MyCustomView 的构造函数中使用 DataBindingUtil.inflate 方法来获取绑定对象,并将它与自定义控件的根布局绑定: ```java public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); // 获取绑定对象 CustomViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_view, this, true); // 绑定数据 binding.setText("Hello, world!"); } ``` 3. 在 MyCustomView 中添加一个 `text` 属性,并在 custom_view.xml 中使用 `@{}` 语法来绑定该属性: ```java public class MyCustomView extends LinearLayout { private String text; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); CustomViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_view, this, true); binding.setCustomView(this); } public String getText() { return text; } public void setText(String text) { this.text = text; } } ``` ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="customView" type="com.example.MyCustomView" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{customView.text}" /> </LinearLayout> </layout> ``` 这样,当 MyCustomView 的 `text` 属性发生变化时,custom_view.xml 中的 TextView 的文本也会自动更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值