美文网首页
自定义View实战(一):从零到基本元素

自定义View实战(一):从零到基本元素

作者: 27efec53a72d | 来源:发表于2017-12-04 09:01 被阅读24次

一、最简单的自定义View,什么都不显示,但是有View的特性

com.cctvjiatao.customview.MainActivity

packagecom.cctvjiatao.customview;

importandroid.app.Activity;

importandroid.os.Bundle;

publicclassMainActivityextendsActivity {

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

com.cctvjiatao.customview.v1.CustomView1

packagecom.cctvjiatao.customview.v1;

importandroid.content.Context;

importandroid.util.AttributeSet;

importandroid.view.View;

/**

* @作者: 麦典威

* @包名:com.cctvjiatao.customview.v1

* @文件名:CustomView1.java

* @功能: 最简单的自定义View,什么都不显示,但是有View的特性

*/

publicclassCustomView1extendsView {

publicCustomView1(Context context) {

super(context);

}

publicCustomView1(Context context, AttributeSet attrs) {

super(context, attrs);

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00ff00">

AndroidManifest.xml

package="com.cctvjiatao.customview"

android:versionCode="1"

android:versionName="1.0">

android:minSdkVersion="14"

android:targetSdkVersion="21"/>

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

android:name=".MainActivity"

android:label="@string/app_name">

效果图暂略。

二、自定义View,显示一行文字。注意:drawText的坐标是 “左下” 坐标。

com.cctvjiatao.customview.MainActivity 同一

AndroidManifest.xml 同一

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00ff00"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent">

com.cctvjiatao.customview.v2.CustomView1

packagecom.cctvjiatao.customview.v2;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.Paint;

importandroid.util.AttributeSet;

importandroid.view.View;

/**

* @作者: 麦典威

* @包名:com.cctvjiatao.customview.v2

* @文件名:CustomView2.java

* @功能: 自定义View,显示一行文字。注意:drawText的坐标是 “左下” 坐标。

*/

publicclassCustomView1extendsView {

publicCustomView1(Context context, AttributeSet attrs) {

super(context, attrs);

}

publicCustomView1(Context context) {

super(context);

}

@Override

protectedvoidonDraw(Canvas canvas) {

Paint paint =newPaint();

// canvas.drawText("This is a canvas", 0, 0, paint);//这样写不会显示文字,因为文字的左下坐标是(0,0)

canvas.drawText("This is a canvas,坐标为左下(0,50)",0,50, paint);// (字符,左坐标,下坐标,画笔)

paint.setTextSize(30);//设置画笔大小,即字体大小

canvas.drawText("This is a canvas,坐标为左下(0,30)",0,30, paint);// (字符,左坐标,下坐标,画笔)

}

}

效果图暂略。

三、自定义View,画线、矩形、圆角矩形、圆形

com.cctvjiatao.customview.MainActivity 同一

AndroidManifest.xml 同一

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00ff00"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent">

com.cctvjiatao.customview.v2.CustomView2

packagecom.cctvjiatao.customview.v2;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.Paint;

importandroid.graphics.Rect;

importandroid.graphics.RectF;

importandroid.graphics.Paint.Style;

importandroid.util.AttributeSet;

importandroid.view.View;

/**

* @作者: 麦典威

* @包名:com.cctvjiatao.customview.v2

* @文件名:CustomView2.java

* @功能: 自定义View,画线、矩形、圆角矩形、圆形

*/

publicclassCustomView2extendsView {

publicCustomView2(Context context, AttributeSet attrs) {

super(context, attrs);

}

publicCustomView2(Context context) {

super(context);

}

@Override

protectedvoidonDraw(Canvas canvas) {

Paint paint =newPaint();

paint.setTextSize(10);

paint.setColor(0xffff0000);

// 画直线

canvas.drawLine(0,10,200,10, paint);

// 画斜线

canvas.drawLine(0,10,200,60, paint);

// 画矩形(Rect)

Rect rect =newRect(0,80,100,160);

canvas.drawRect(rect, paint);

// 画矩形(RectF)

RectF rectf =newRectF(150,80,250,160);

canvas.drawRect(rectf, paint);

// 画矩形(坐标)

canvas.drawRect(300,80,400,160, paint);

// 画圆角矩形(RectF)

RectF rectrf =newRectF(10,180,110,250);

canvas.drawRoundRect(rectrf,10,10, paint);

// 画圆角矩形(RectF)

canvas.drawRoundRect(120,180,220,250,10,10, paint);

// 画圆形

canvas.drawCircle(100,350,50, paint);

paint.setStyle(Style.STROKE);

canvas.drawCircle(210,350,50, paint);

paint.setStyle(Style.FILL_AND_STROKE);

canvas.drawCircle(320,350,50, paint);

paint.setStyle(Style.FILL);

canvas.drawCircle(430,350,50, paint);

}

}

效果图暂略。

四、自定义View,画图像

com.cctvjiatao.customview.MainActivity 同一

AndroidManifest.xml 同一

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00ff00"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:visibility="gone">

android:layout_width="match_parent"

android:layout_height="match_parent">

com.cctvjiatao.customview.v2.CustomView3

packagecom.cctvjiatao.customview.v2;

importcom.cctvjiatao.customview.R;

importandroid.content.Context;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

importandroid.graphics.Canvas;

importandroid.graphics.Paint;

importandroid.util.AttributeSet;

importandroid.view.View;

/**

* @作者: 麦典威

* @包名:com.cctvjiatao.customview.v2

* @文件名:CustomView3.java

* @功能: 自定义View,画图像

*/

publicclassCustomView3extendsView {

privateBitmap bitmap;

publicCustomView3(Context context, AttributeSet attrs) {

super(context, attrs);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

}

publicCustomView3(Context context) {

super(context);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

}

@Override

protectedvoidonDraw(Canvas canvas) {

Paint paint =newPaint();

canvas.drawBitmap(bitmap,0,35, paint);

}

}

效果图暂略

相关文章

网友评论

      本文标题:自定义View实战(一):从零到基本元素

      本文链接:https://www.haomeiwen.com/subject/qpstixtx.html