Android简明开发教程十六:Button 画刷示例
将RadioButton 换成Button ,类似的在res/layout 中新建brush.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:background=”@drawable/white”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<com.pstreets.graphics2d.GuidebeeGraphics2DView
android:id=”@+id/graphics2dview”
android:layout_weight=”1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”/>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:orientation=”horizontal”
>
<Button android:text=”Pattern”
android:id=”@+id/btnPattern”
android:layout_width=”wrap_content”
android:textColor=”@color/black”
android:checked=”true”
android:layout_height=”wrap_content”>
</Button>
<Button android:text=”Gradients”
android:id=”@+id/btnGradients”
android:layout_width=”wrap_content”
android:textColor=”@color/black”
android:layout_height=”wrap_content”>
</Button>
</LinearLayout>
</LinearLayout>
修改Brushes.java ,完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
public class Brushes extends Graphics2DActivity
implements OnClickListener {
private Button btnPattern;
private Button btnGradients;
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.brush);
graphic2dView = (GuidebeeGraphics2DView)
findViewById(R.id.graphics2dview);
btnPattern = (Button) findViewById(R.id.btnPattern);
btnGradients = (Button) findViewById(R.id.btnGradients);
btnPattern.setOnClickListener( this );
btnGradients.setOnClickListener( this );
}
@Override
protected void drawImage() {
drawPatterns();
}
@Override
public void onClick(View view) {
if (view == btnPattern) {
drawPatterns();
} else {
drawGradient();
}
graphic2dView.refreshCanvas();
}
private void drawPatterns() {
TextureBrush brush1;
TextureBrush brush2;
TextureBrush brush3;
AffineTransform matrix1 = new AffineTransform();
AffineTransform matrix2 = new AffineTransform();
Bitmap bitmap
= BitmapFactory.decodeResource(getResources(),
R.drawable.brick);
int [] rgbData = new int [bitmap.getHeight()
* bitmap.getWidth()];
bitmap.getPixels(rgbData, 0 , bitmap.getWidth(), 0 , 0 ,
bitmap.getWidth(), bitmap.getHeight());
brush1 = new TextureBrush(rgbData, bitmap.getWidth(),
bitmap.getHeight());
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bird);
rgbData = new int [bitmap.getHeight() * bitmap.getWidth()];
bitmap.getPixels(rgbData, 0 , bitmap.getWidth(), 0 , 0 ,
bitmap.getWidth(), bitmap.getHeight());
brush2 = new TextureBrush(rgbData, bitmap.getWidth(),
bitmap.getHeight());
brush3 = new TextureBrush(rgbData, bitmap.getWidth(),
bitmap.getHeight(), 127 );
matrix2.translate( 50 , 50 );
// Clear the canvas with white color.
graphics2D.clear(Color.WHITE);
graphics2D.setAffineTransform(matrix1);
graphics2D.fillRectangle(brush1,
new Rectangle( 20 , 50 , 100 , 100 ));
graphics2D.fillOval(brush2, 10 , 10 , 80 , 80 );
graphics2D.setAffineTransform(matrix2);
graphics2D.fillOval(brush3, 10 , 10 , 80 , 80 );
}
private void drawGradient() {
/* The linear gradient color */
LinearGradientBrush brush1;
/* The radial gradient color */
RadialGradientBrush brush2;
/* The second radial gradient color */
RadialGradientBrush brush3;
char [] engText = "Brush" .toCharArray();
FontEx font = FontEx.getSystemFont();
int fontSize = 44 ;
int X = 15 ;
int Y = 50 ;
int [] fractions = new int [] { 13 , 242 };
Color[] colors = new Color[] { new Color( 0xffff6600 ),
new Color( 0xffffff66 ) };
brush1 = new LinearGradientBrush( 50 , 50 , 150 , 125 ,
fractions, colors,
Brush.NO_CYCLE);
fractions = new int [] { 13 , 128 , 255 };
colors = new Color[] { new Color( 0xffff6600 ),
new Color( 0xffffff66 ),
new Color( 0xffff6600 ) };
brush2 = new RadialGradientBrush( 90 , 100 , 50 ,
fractions, colors);
fractions = new int [] { 0 , 255 };
colors = new Color[] { new Color( 0xFFFFFF00 ),
new Color( 0xFF000000 ) };
brush3 = new RadialGradientBrush( 50 , 50 , 100 ,
fractions, colors);
// Clear the canvas with white color.
graphics2D.clear(Color.white);
graphics2D.fillRectangle(brush1,
new Rectangle( 10 , 75 , 120 , 80 ));
Pen pen = new Pen(brush2, 8 );
graphics2D.drawOval(pen, 20 , 60 , 100 , 50 );
graphics2D.setDefaultBrush(brush3);
pen = new Pen(brush2, 2 );
graphics2D.setDefaultPen(pen);
graphics2D.drawChars(font, fontSize, engText, 0 ,
engText.length, X, Y);
}
}
|
介绍了RadioButton和Button 后,这时应该对使用Android提供的控件的用法有了基本的认识。 控件提供了onClick(),onLongClick(),onFocusChange(),onKey(),onTouch(),onCreateContextMenu()等多种事件以相应用户。用多种方法来处理用户事件。一种是示例代码同过Activity实现OnClickListener接口,再有是采用如下代码为Button支持事件处理方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// Create an anonymous implementation of OnClickListenerprivate
OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
|
在创建自定义控件时,也可以重载onKeyDown(int, KeyEvent),onKeyUp(int, KeyEvent) ,onTouchEvent(MotionEvent)等来处理用户事件。
Android简明开发教程十六:Button 画刷示例相关推荐
- Android简明开发教程二十一:访问Internet 绘制在线地图
在例子Android简明开发教程十七:Dialog 显示图像 中我们留了一个例子DrawMap()没有实现,这个例子显示在线地图,目前大部分地图服务器都是将地图以图片存储以提高响应速度. 一般大小为2 ...
- Luat 功能开发教程(十六) LittleVGL
目录 LittleVGL 简介 控件API说明 实现流程 示例 image控件 Canvas控件 button控件 Arc控件(加载器) page控件 label控件 Slider控件 switch控 ...
- Android UI开发第二十五篇——分享一篇自定义的 Action Bar
Action Bar是android3.0以后才引入的,主要是替代3.0以前的menu和tittle bar.在3.0之前是不能使用Action Bar功能的.这里引入了自定义的Action Bar, ...
- OpenGl文章
Android OpenGL ES 简明开发教程
Android OpenGL ES 简明开发教程 分类:android学习笔记2011-12-14 15:04375人阅读评论(0)收藏举报 ApiDemos 的Graphics示例中含有OpenGL ...
- 【Visual C++】游戏开发四十八 浅墨DirectX教程十六 三维地形系统的实现
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 本系列文 ...
- 【Visual C++】游戏开发四十八 浅墨DirectX教程十六 三维地形系统的实现
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/zhmxy555/article/details/8685546 作者:毛星云(浅墨) ...
- EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换
目录 1. 数据转换概念 2. 数据智能感知 - DataSense 3. 简单数据转换组件 3.1 Object to JSON 3.2 JSON to XML 3.3 JSON to Object ...
- S3C2416裸机开发系列十六_sd卡驱动实现
S3C2416裸机开发系列十六 sd卡驱动实现 象棋小子 1048272975 SD卡(Secure Digital Memory Card)具有体积小.容量大.数据传输快.可插拔.安全性好等优 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler
原文地址为: Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler 上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程( ...
- android项目开发教程,Android项目开发教程
Android项目开发教程 编辑 锁定 讨论 上传视频 Android项目开发教程 书 名 Android项目开发教程 作 者 车金庆.何征天.李琳.严正宇.周凌翱 类 别 图书&g ...
最新文章
- 如何让Table显示滚动条
- MariaDB的二进制包安装方法
- JVM - 一个案例反推不同JDK版本的intern机制以及intern C++源码解析
- Jvm原理剖析与调优之内存结构
- Excel2010--在指定的范围内添加随机数
- mac netbeans java_在macOS上不安装JDK的情况下安装Netbeans
- PTA 数据结构与算法题目集 6-1
- Qt学习 QVariant类(转)
- 12. PHP 函数
- 串口调试助手fx2n_串口调试助手发送控制台达PLC命令
- 苹果备忘录怎么使用计算机,原来苹果备忘录这么神奇!你还不会用?网友:万把块白花了...
- 读书笔记 摘自:《亲密关系:通往灵魂的桥梁(张德芬译)》的笔记(作者: 【加】克里斯多福·孟)
- 浅谈标签概念及应用场景
- opencv 数据库裁剪图片
- Just Do It!
- java class 加密_如何给class文件加密
- 《当咖啡与甘蓝汁竞争》:产品是负熵,帮助客户更好进化
- [Python从零到壹] 五十八.图像增强及运算篇之图像锐化Sobel、Laplacian算子实现边缘检测
- repo sync 更新源码 android-12.0.0_r34, fatal: 不能重置索引文件至版本 ‘v2.27^0‘。
- Codeforces Round #781 CF1665 CDE
热门文章
- (转)以C++为核心语言的高频交易系统的讨论?
- 常用内存数据库介绍(一)
- 毕设题目:Matlab图像配准
- 【元胞自动机】基于matlab元胞自动机双边教室疏散【含Matlab源码 1208期】
- 【人民币识别】基于matlab GUI人民币序列号识别【含Matlab源码 908期】
- android mat教程,OpenCV for Android - Access elements of Mat
- 无人驾驶 ai算法_质疑AI是否具有意图以及这对无人驾驶汽车意味着什么
- 机器学习 决策树 监督_监督机器学习-决策树分类器简介
- 学术会议查询 边缘计算_我设计了可以预测边缘性的“学术不诚实”的AI系统(SMART课堂)...
- C++中指针运算符(*)和数组索引符([])的优先级。指针数组及数组指针