赞
踩
目录
自定义View的实现方式有以下几种: 组合控件,继承控件,自绘控件
1.首先先创建一个.xml文件,编写布局
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:background="#B8DCF8"
- android:id="@+id/rl">
-
-
- <ImageView
- android:id="@+id/iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- app:srcCompat="@drawable/ic_baseline_arrow_back_24"
- android:layout_centerVertical="true"
- android:layout_marginStart="10dp"
- />
-
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="安卓"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- />
-
-
-
- </RelativeLayout>
2.创建一个java文件,继承与上方.xml的最外层的布局,然后实现他的构造方法,初始化UI
- public class ac extends RelativeLayout {
-
-
- public ac(Context context) {
- super(context);
- }
-
- public ac(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.a,this,true);
- }
-
- public ac(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
- public ac(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- }
-
3.在布局中引用该控件
- <com.hopu.day18_viewcustom.ac
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
使用步骤
- public class color extends androidx.appcompat.widget.AppCompatTextView {
-
- public color(@NonNull Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Paint paint=new Paint();
- paint.setColor(Color.RED);
- paint.setStrokeWidth(5);
- int width=getWidth();
- int height=getBaseline();
- canvas.drawLine(0,height,width,height,paint);
- }
- }
2.在布局文件中调用
使用步骤
代码案例
设计背景色的案例
1.继承ViewGroup类系统控件
- public class beijingse extends LinearLayout {
-
- public beijingse(Context context) {
- super(context);
- }
-
- public beijingse(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- protected void dispatchDraw(Canvas canvas) {
- super.dispatchDraw(canvas);
- canvas.drawColor(Color.parseColor("#50ff0000"));
- }
-
- public beijingse(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
- public beijingse(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- }
2. 在布局文件中调用
我这里是把最外层的布局给引用了上面的继承viewGroup
- <com.hopu.day18_viewcustom.beijingse xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
-
- <com.hopu.day18_viewcustom.ac
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
-
- <com.hopu.day18_viewcustom.color
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="你好"/>
-
- </com.hopu.day18_viewcustom.beijingse>
1.创建一个drwaViewjava文件,继承与View,实现他的构造方法,以及创建一个方法
init();来对画笔进行设置
- public class drawView extends View {
- private Paint paint;
- public drawView(Context context) {
- super(context);
- init();
- }
-
- public drawView(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- init();
- }
-
- public drawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
-
- public drawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- init();
- }
-
- private void init(){
- paint=new Paint();
- paint.setColor(getResources().getColor(R.color.black));//画笔颜色
- paint.setAntiAlias(true);//抗锯齿
- paint.setTextSize(36);//绘制文字大小,单位px
- paint.setStrokeWidth(5);//画笔粗细
- paint.setStyle(Paint.Style.FILL);//画笔风格
-
- }
2.重写onDraw方法,在该方法里绘图
- //在该地方画图
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawColor(getResources().getColor(R.color.teal_200));//绘制画布背景颜色
-
- canvas.drawCircle(getWidth()/2,getHeight()/2,200,paint);//绘制圆形
-
- canvas.drawRect(200,100,800,400,paint);//绘制矩形
-
- //绘制Bitmap,就是图片
- canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.heihei),200,200,paint);
-
- //绘制圆角矩形
- canvas.drawRoundRect(new RectF(10,10,210,110),20,20,paint);
-
- //绘制形状,至少要绘制三个点才能组成一个图形
- Path path=new Path();
- path.moveTo(10,10);
- path.lineTo(200,30);
- path.lineTo(300,10);
- path.close();
- canvas.drawPath(path,paint);
-
- //绘制一个波浪形的一个数字形状
- Path path1=new Path();
- path1.moveTo(10,10);
- path1.lineTo(0,100);
- path1.lineTo(100,0);
- path1.lineTo(200,200);
- path1.lineTo(300,200);
- path1.close();
- canvas.drawTextOnPath("ABCDEFGHIJKLMNOPQRSTUVWXYZ",path1,10,10,paint);
-
- //绘制文字
- Paint textPaint=new Paint();
- textPaint.setColor(getResources().getColor(R.color.white));
- textPaint.setStrokeWidth(5);
- textPaint.setTextSize(36);
- canvas.drawText("15",getWidth()/2,getHeight()/2,textPaint);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。