Android 中View.onDraw(Canvas canvas)的使用方法
作者:zhangqijie001 时间:2022-11-16 16:40:48
Android 中View.onDraw(Canvas canvas)的使用方法
View通过View.onDraw(Canvas canvas)来Draw.
我们可以定义自己的继承于View的TestView,然后重载View.onDraw(Canvas canvas).
对于自定义的TestView如何与Activity关联?有以下两种方式:
直接在setContentView(View view)里面加进去自定义的View:setContentView(new TestView(this)).
另外,可以在layout文件里面可以使用自定义的View(如何自定义的View为内部类,就会失效),
如:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.android.test.TestView
android:id="@+id/testview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
以下为使用onDraw(Canvas canvas)画矩形区域,及在其上画文本的实例(通过使用内部类使程序显得更加简洁,紧凑):
package com.android.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TestView(this));
}
public class TestView extends View {
private Paint mPaint = new Paint();
public TestView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
String text = "Android - 机器人";
mPaint.setColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.RED);
String familyName = "宋体";
Typeface font = Typeface.create(familyName,Typeface.BOLD);
paint.setTypeface(font);
paint.setTextSize(22);
canvas.drawRect(new Rect(0, 0, 320, 240), mPaint);
canvas.drawText(text, 0, 100, paint);
}
}
}
运行效果如下图:
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/zhangqijie001/article/details/5827012
标签:Android,Canvas
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot实现分布式锁的自动释放的示例代码
2023-10-17 11:06:24
Java web的读取Excel简单实例代码
2023-05-29 08:25:37
![](https://img.aspxhome.com/file/2023/6/78946_0s.png)
Java中ReentrantLock4种常见的坑
2021-09-26 10:51:46
![](https://img.aspxhome.com/file/2023/3/61133_0s.webp)
java的NIO管道用法代码分享
2022-05-01 23:52:53
![](https://img.aspxhome.com/file/2023/7/105917_0s.jpg)
详解Android的.aar文件生成方法以及使用技巧
2023-08-06 06:43:20
![](https://img.aspxhome.com/file/2023/9/85459_0s.jpg)
一步步教你把SpringBoot项目打包成Docker镜像
2022-01-10 12:21:02
![](https://img.aspxhome.com/file/2023/9/85439_0s.png)
利用java制作简单的音乐播放器
2022-01-21 11:39:32
C#实现根据图片的链接地址获取图片的后缀名
2022-08-04 04:30:58
SpringBoot实战之高效使用枚举参数(原理篇)案例详解
2022-02-10 23:54:23
![](https://img.aspxhome.com/file/2023/5/77055_0s.jpg)
Java StringUtils字符串分割转数组的实现
2023-07-19 12:43:37
软件开发基础之设计模式概述
2023-05-14 04:53:07
![](https://img.aspxhome.com/file/2023/4/67434_0s.png)
老生常谈C/C++内存管理
2022-05-07 02:17:10
详解maven的setting配置文件中mirror和repository的区别
2022-03-19 11:56:42
![](https://img.aspxhome.com/file/2023/2/83112_0s.jpg)
IntelliJ IDEA 15款超级牛逼插件推荐(自用,超级牛逼)
2023-10-10 05:51:22
![](https://img.aspxhome.com/file/2023/6/71166_0s.png)
eclipse的web项目实现Javaweb购物车的方法
2023-07-28 15:55:53
![](https://img.aspxhome.com/file/2023/2/64152_0s.png)
android中Fragment+RadioButton实现底部导航栏
2022-11-22 00:52:20
![](https://img.aspxhome.com/file/2023/3/139633_0s.png)
Android仿iphone自定义滚动选择器
2023-06-24 12:47:59
![](https://img.aspxhome.com/file/2023/9/125359_0s.gif)
mybatis教程之resultmap_动力节点Java学院整理
2022-09-05 22:38:37
Android基于广播事件机制实现简单定时提醒功能代码
2023-07-10 09:57:55
基于Lucene的Java搜索服务器Elasticsearch安装使用教程
2022-05-31 05:05:10
![](https://img.aspxhome.com/file/2023/7/93017_0s.png)