自定义View(一)——画线、矩形、圆形、图像
来源:互联网 发布:淘宝 真皮皮带 编辑:程序博客网 时间:2024/06/07 02:52
一、最简单的自定义View,什么都不显示,但是有View的特性
com.cctvjiatao.customview.MainActivity
package com.cctvjiatao.customview;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}com.cctvjiatao.customview.v1.CustomView1
package com.cctvjiatao.customview.v1;import android.content.Context;import android.util.AttributeSet;import android.view.View;/** * @作者: jiatao * @修改时间:2016-3-11 上午7:58:10 * @包名:com.cctvjiatao.customview.v1 * @文件名:CustomView1.java * @版权声明:www.cctvjiatao.com * @功能: 最简单的自定义View,什么都不显示,但是有View的特性 */public class CustomView1 extends View {public CustomView1(Context context) {super(context);}public CustomView1(Context context, AttributeSet attrs) {super(context, attrs);}}activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" > </com.cctvjiatao.customview.v1.CustomView1></FrameLayout>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cctvjiatao.customview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>效果图:
二、自定义View,显示一行文字。注意:drawText的坐标是 “左下” 坐标。
com.cctvjiatao.customview.MainActivity 同一
AndroidManifest.xml 同一
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:visibility="gone"> </com.cctvjiatao.customview.v1.CustomView1> <com.cctvjiatao.customview.v2.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent"> </com.cctvjiatao.customview.v2.CustomView1></FrameLayout>com.cctvjiatao.customview.v2.CustomView1
package com.cctvjiatao.customview.v2;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * @作者: jiatao * @修改时间:2016-3-11 上午8:22:56 * @包名:com.cctvjiatao.customview.v2 * @文件名:CustomView2.java * @版权声明:www.cctvjiatao.com * @功能: 自定义View,显示一行文字。注意:drawText的坐标是 “左下” 坐标。 */public class CustomView1 extends View {public CustomView1(Context context, AttributeSet attrs) {super(context, attrs);}public CustomView1(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();// 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);// (字符,左坐标,下坐标,画笔)}}
效果图:
com.cctvjiatao.customview.MainActivity 同一
AndroidManifest.xml 同一
activity_main.xml<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:visibility="gone"> </com.cctvjiatao.customview.v1.CustomView1> <com.cctvjiatao.customview.v2.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView1> <com.cctvjiatao.customview.v2.CustomView2 android:layout_width="match_parent" android:layout_height="match_parent"> </com.cctvjiatao.customview.v2.CustomView2></FrameLayout>com.cctvjiatao.customview.v2.CustomView2
<pre name="code" class="java">package com.cctvjiatao.customview.v2;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.view.View;/** * @作者: jiatao * @修改时间:2016-3-12 上午11:31:51 * @包名:com.cctvjiatao.customview.v2 * @文件名:CustomView2.java * @版权声明:www.cctvjiatao.com * @功能: 自定义View,画线、矩形、圆角矩形、圆形 */public class CustomView2 extends View {public CustomView2(Context context, AttributeSet attrs) {super(context, attrs);}public CustomView2(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();paint.setTextSize(10);paint.setColor(0xffff0000);// 画直线canvas.drawLine(0, 10, 200, 10, paint);// 画斜线canvas.drawLine(0, 10, 200, 60, paint);// 画矩形(Rect)Rect rect = new Rect(0, 80, 100, 160);canvas.drawRect(rect, paint);// 画矩形(RectF)RectF rectf = new RectF(150, 80, 250, 160);canvas.drawRect(rectf, paint);// 画矩形(坐标)canvas.drawRect(300, 80, 400, 160, paint);// 画圆角矩形(RectF)RectF rectrf = new RectF(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<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:visibility="gone"> </com.cctvjiatao.customview.v1.CustomView1> <com.cctvjiatao.customview.v2.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView1> <com.cctvjiatao.customview.v2.CustomView2 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView2> <com.cctvjiatao.customview.v2.CustomView3 android:layout_width="match_parent" android:layout_height="match_parent"> </com.cctvjiatao.customview.v2.CustomView3></FrameLayout>com.cctvjiatao.customview.v2.CustomView3
package com.cctvjiatao.customview.v2;import com.cctvjiatao.customview.R;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * @作者: jiatao * @修改时间:2016-3-12 下午1:16:11 * @包名:com.cctvjiatao.customview.v2 * @文件名:CustomView3.java * @版权声明:www.cctvjiatao.com * @功能: 自定义View,画图像 */public class CustomView3 extends View {private Bitmap bitmap;public CustomView3(Context context, AttributeSet attrs) {super(context, attrs);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);}public CustomView3(Context context) {super(context);bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();canvas.drawBitmap(bitmap, 0, 35, paint);}}效果图:
1 0
- 自定义View(一)——画线、矩形、圆形、图像
- 自定义View——画线、矩形、圆形、图像
- 自定义View2——画线、矩形、圆形、.写字,图像
- Android 绘图基础:Canvas画布——自定义View基础(绘制表盘、矩形、圆形、弧、渐变)
- Android 绘图基础:Canvas画布——自定义View基础(绘制表盘、矩形、圆形、弧、渐变)
- 实现自定义圆形、圆角矩形View
- 自定义圆形、圆角矩形View
- 自定义view圆形进度条,矩形与二维码
- 自定义View学习一(圆形头像)
- 自定义View(使用canvas画圆,线,矩形[钟表,圆形下载,矩形下载,弧形下载])
- Android自定义View(一) 画线段
- 自定义View 之 ImageView(一) 自定义圆形ImageView
- Android自定义View——可设置形状(圆形、圆角矩形、椭圆)的ImageView,抗锯齿
- Android 自定义View——圆形进度
- 自定义View——圆形进度条
- 自定义ImageView——圆角 圆形 矩形 图片控件
- 53.自定义View练习(一)圆形百分比控件
- android自定义view-打造圆形ImageView(一)
- 【概念】协议理解之HTTPS
- docker启动Mysql
- 判断移动端PC端访问网页时跳转到对应的移动端网页
- IEEE的latex模板的单双栏设置方法
- android模拟器无法访问本地的tomcat
- 自定义View(一)——画线、矩形、圆形、图像
- 使用application类定义全局变量传递参数
- Java入门笔记
- fzu1977Pandora adventure【插头dp】
- Scalaz(31)- Free :自由数据结构-算式和算法的关注分离
- 基于.net的C# Windows Media Player控件
- java学习---char和int的类型转换问题
- SQOOP 基础及安装
- 输入一个整数,将其个位数字颠倒顺序后输出