自定义View的学习(一) 自绘制控件
2016-08-25 16:20
309 查看
一、自绘控件
就是自己绘制的控件,通过onDraw()方法将控件绘制出来 自定义一个可点击的View 这个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>
相关文章推荐
- Android学习自定义View(三)——自绘控件和组合控件
- Android学习自定义View(二)——View和ViewGroup绘制流程以及invalidate()
- Android学习之自定义控件之图片带文字的View
- android学习9#--自定义View之绘制过程分析
- Android开源中国客户端学习 (自定义View)左右滑动控件ScrollLayout
- Android学习自定义View(三)——自绘控件和组合控件
- Android控件:自定义View学习资源汇总
- Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)
- Android学习摘记——简单的自定义View(自绘控件)
- React Native学习之Modal控件自定义弹出View
- Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)
- qt利用委托:QStyledItemDelegate,实现tableview 的表格项的自定义显示,如插入图片,绘制控件
- 学习:自定义view中的onDraw的绘制
- 自定义View学习-绘制一个简单的圆
- Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- Qt自定义委托在QTableView中绘制控件、图片、文字
- iOS--Quartz2D使用(自定义UIImageView控件、绘制基本图形)