zoukankan      html  css  js  c++  java
  • 自定义View的学习(一) 自绘制控件

    一、自绘控件

     就是自己绘制的控件,通过onDraw()方法将控件绘制出来  自定义一个可点击的View  这个View可以记住用户点击的次数

    public class CounterView extends View implements OnClickListener {
    
        private Paint mPaint;
        
        private Rect mBounds;
    
        private int mCount;
        
        public CounterView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mBounds = new Rect();
            setOnClickListener(this);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mPaint.setColor(Color.BLUE);
            canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
            mPaint.setColor(Color.YELLOW);
            mPaint.setTextSize(30);
            String text = String.valueOf(mCount);
            mPaint.getTextBounds(text, 0, text.length(), mBounds);
            float textWidth = mBounds.width();
            float textHeight = mBounds.height();
            canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2
                    + textHeight / 2, mPaint);
        }
    
        @Override
        public void onClick(View v) {
            mCount++;
            invalidate();
        }
    
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <com.example.customview.CounterView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true" />
    
    </RelativeLayout>

  • 相关阅读:
    认识目标文件的内容
    PetaLinux工程更新HDF文件的脚本
    [海思] 中断申请和重启问题
    [海思] HI3531D串口调试
    【C语言】结构体初始化
    [ZCU106] Petalinux 2018.3 与启动环境搭建记录
    旧版本源码usbip的移植
    jar包下载地址
    Django框架
    前端内容
  • 原文地址:https://www.cnblogs.com/bimingcong/p/5807313.html
Copyright © 2011-2022 走看看