转自:http://mzh3344258.blog.51cto.com/1823534/1215749
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
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:background
=
"@drawable/background"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
".MainActivity"
>
<
com.xiaoma.shadedemo.TextViewBorder
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
android:layout_marginLeft
=
"22dp"
android:layout_marginTop
=
"20dp"
android:text
=
" JAVA实现带四条边框"
android:textColor
=
"@android:color/white"
android:textSize
=
"25sp"
/>
<
com.xiaoma.shadedemo.TextViewBorderLeftRight
android:id
=
"@+id/TextViewBorder01"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"@android:color/white"
android:layout_alignLeft
=
"@+id/textView1"
android:layout_below
=
"@+id/textView1"
android:layout_marginTop
=
"20dp"
android:text
=
"JAVA实现带左右边框"
android:textSize
=
"25sp"
/>
<
com.xiaoma.shadedemo.TextViewBorderUnder
android:id
=
"@+id/TextViewBorder02"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/TextViewBorder01"
android:textColor
=
"@android:color/white"
android:layout_below
=
"@+id/TextViewBorder01"
android:layout_marginTop
=
"33dp"
android:text
=
"JAVA代码实现下边框"
android:textSize
=
"25sp"
/>
<
TextView
android:id
=
"@+id/TextViewBorderUnder01"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/button1"
android:layout_below
=
"@+id/TextViewBorder02"
android:layout_marginTop
=
"20dp"
android:text
=
"Shape XML实现边框"
android:background
=
"@drawable/shape_test"
android:textColor
=
"@android:color/white"
android:textSize
=
"25sp"
/>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/TextViewBorder02"
android:layout_below
=
"@+id/TextViewBorderUnder01"
android:layout_marginTop
=
"29dp"
android:background
=
"@drawable/shape_selector"
android:text
=
"Selector与Shape混用按钮"
android:textColor
=
"@android:color/black"
/>
</
RelativeLayout
>
|
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
|
package
com.xiaoma.shadedemo;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.util.AttributeSet;
import
android.widget.TextView;
public
class
TextViewBorder
extends
TextView
{
/**
* 下面两个方法在构造时需注意: 一:如果是XML文件加载的方式使用自定义控件到布局中是用以下方式, 二:如果是用纯代码的方式加载自定义的控制到而已中时用第二种方式
*/
// 方式一:
public
TextViewBorder(Context context, AttributeSet attrs)
{
super
(context, attrs);
}
// 方式二:
/*
* public TextViewBorder(Context context) { // TODO Auto-generated constructor stub super(context); }
*/
/**
* 1. Rect对象 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域, 即是说使用 Rect.contains(left, top)为true,
* Rect.contains(right, bottom)为false 2. drawLine方法 drawLine(float startX, float startY, float stopX, float stopY,
* Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1 3. drawRect(Rect r, Paint paint) 当绘制空心矩形时,绘制的是一个左闭右闭的区域
* 验证方法:下面就是可以验证左闭右开的区间方法,现在知道为什么要-1 了
*/
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);// 绘制上边框
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); // 绘制左边框
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); // 绘制右边框
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);// 绘制下边框
}
/*
* 1. Rect对象
*
* 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域,即是说使用 Rect.contains(left, top)为true, Rect.contains(right,
* bottom)为false
*
* 2.drawLine方法
*
* drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1
*
* 验证方法:
*
* Canvas c = canvas; paint.setColor(Color.RED); c.drawLine(x, y, x+c.getWidth()-1, y, paint); c.drawLine(x,
* y+height-1, x+c.getWidth(), y+height-1, paint); paint.setColor(Color.BLUE); c.drawPoint(x+c.getWidth()-1, y,
* paint); 说明drawLine是没有绘制到右边最后一个点的
*
* 3.drawRect(Rect r, Paint paint)
*
* 当绘制空心矩形时,绘制的是一个左闭右闭的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); c.drawRect(rect,
* paint); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x, y+height, x+width,
* y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width, y+height, paint);
* 当绘制实心矩形时,绘制的是一个左闭右开的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x,
* y+height, x+width, y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width,
* y+height, paint); paint.setStyle(Style.FILL); paint.setColor(Color.BLUE); c.drawRect(rect, paint);
* 这个规则跟j2me也是一样的,在j2me里,drawRect长宽会多画出1px。SDK的说明是:
*
* The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width
* or height is less than zero, nothing is drawn.
*
* 例如drawRect(10,10,100,1)绘制,结果是一个2px高的矩形,用fillRect(10,10,100,1),结果是一个1px高的矩形
*
* 以上就是对Android绘图的具体介绍。
*/
}
/**
* 在布局文件中引用 这样引用就行了..吼吼 <com.xiaoma.shadedemo.TextViewBorder android:id="@+id/a02_txtKSSJ" android:textColor="#000000"
* android:layout_marginLeft="10dip" android:layout_width="100dip" android:layout_height="wrap_content" />
*/
|
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
|
package
com.xiaoma.shadedemo;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.util.AttributeSet;
import
android.widget.TextView;
public
class
TextViewBorderLeftRight
extends
TextView
{
/**
* 下面两个方法在构造时需注意: 一:如果是XML文件加载的方式使用自定义控件到布局中是用以下方式, 二:如果是用纯代码的方式加载自定义的控制到而已中时用第二种方式
*/
// 方式一:
public
TextViewBorderLeftRight(Context context, AttributeSet attrs)
{
super
(context, attrs);
}
// 方式二:
/*
* public TextViewBorder(Context context) { // TODO Auto-generated constructor stub super(context); }
*/
/**
* 1. Rect对象 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域, 即是说使用 Rect.contains(left, top)为true,
* Rect.contains(right, bottom)为false 2. drawLine方法 drawLine(float startX, float startY, float stopX, float stopY,
* Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1 3. drawRect(Rect r, Paint paint) 当绘制空心矩形时,绘制的是一个左闭右闭的区域
* 验证方法:下面就是可以验证左闭右开的区间方法,现在知道为什么要-1 了
*/
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
canvas.drawLine(0, 0, 0, getHeight(), paint);
// canvas.drawLine(getWidth(), 0, getWidth() - 1, getHeight() - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
// canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
// canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
// canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
}
/*
* 1. Rect对象
*
* 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域,即是说使用 Rect.contains(left, top)为true, Rect.contains(right,
* bottom)为false
*
* 2.drawLine方法
*
* drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1
*
* 验证方法:
*
* Canvas c = canvas; paint.setColor(Color.RED); c.drawLine(x, y, x+c.getWidth()-1, y, paint); c.drawLine(x,
* y+height-1, x+c.getWidth(), y+height-1, paint); paint.setColor(Color.BLUE); c.drawPoint(x+c.getWidth()-1, y,
* paint); 说明drawLine是没有绘制到右边最后一个点的
*
* 3.drawRect(Rect r, Paint paint)
*
* 当绘制空心矩形时,绘制的是一个左闭右闭的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); c.drawRect(rect,
* paint); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x, y+height, x+width,
* y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width, y+height, paint);
* 当绘制实心矩形时,绘制的是一个左闭右开的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x,
* y+height, x+width, y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width,
* y+height, paint); paint.setStyle(Style.FILL); paint.setColor(Color.BLUE); c.drawRect(rect, paint);
* 这个规则跟j2me也是一样的,在j2me里,drawRect长宽会多画出1px。SDK的说明是:
*
* The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width
* or height is less than zero, nothing is drawn.
*
* 例如drawRect(10,10,100,1)绘制,结果是一个2px高的矩形,用fillRect(10,10,100,1),结果是一个1px高的矩形
*
* 以上就是对Android绘图的具体介绍。
*/
}
/**
* 在布局文件中引用 这样引用就行了..吼吼 <com.xiaoma.shadedemo.TextViewBorder android:id="@+id/a02_txtKSSJ" android:textColor="#000000"
* android:layout_marginLeft="10dip" android:layout_width="100dip" android:layout_height="wrap_content" />
*/
|
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
|
package
com.xiaoma.shadedemo;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.util.AttributeSet;
import
android.widget.TextView;
public
class
TextViewBorderUnder
extends
TextView
{
/**
* 下面两个方法在构造时需注意: 一:如果是XML文件加载的方式使用自定义控件到布局中是用以下方式, 二:如果是用纯代码的方式加载自定义的控制到而已中时用第二种方式
*/
// 方式一:
public
TextViewBorderUnder(Context context, AttributeSet attrs)
{
super
(context, attrs);
}
// 方式二:
/*
* public TextViewBorder(Context context) { // TODO Auto-generated constructor stub super(context); }
*/
/**
* 1. Rect对象 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域, 即是说使用 Rect.contains(left, top)为true,
* Rect.contains(right, bottom)为false 2. drawLine方法 drawLine(float startX, float startY, float stopX, float stopY,
* Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1 3. drawRect(Rect r, Paint paint) 当绘制空心矩形时,绘制的是一个左闭右闭的区域
* 验证方法:下面就是可以验证左闭右开的区间方法,现在知道为什么要-1 了
*/
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
// canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
// canvas.drawLine(0, getHeight(), getWidth() - 1, getHeight(), paint);
// canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
canvas.drawLine(0, getHeight() - 1, getWidth() - 1, getHeight() - 1, paint);
}
/*
* 1. Rect对象
*
* 一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域,即是说使用 Rect.contains(left, top)为true, Rect.contains(right,
* bottom)为false
*
* 2.drawLine方法
*
* drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1
*
* 验证方法:
*
* Canvas c = canvas; paint.setColor(Color.RED); c.drawLine(x, y, x+c.getWidth()-1, y, paint); c.drawLine(x,
* y+height-1, x+c.getWidth(), y+height-1, paint); paint.setColor(Color.BLUE); c.drawPoint(x+c.getWidth()-1, y,
* paint); 说明drawLine是没有绘制到右边最后一个点的
*
* 3.drawRect(Rect r, Paint paint)
*
* 当绘制空心矩形时,绘制的是一个左闭右闭的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); c.drawRect(rect,
* paint); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x, y+height, x+width,
* y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width, y+height, paint);
* 当绘制实心矩形时,绘制的是一个左闭右开的区域
*
* 验证方法:
*
* rect.set(x, y, x+width, y+height); paint.setColor(Color.RED); c.drawLine(x, y, x+width, y, paint); c.drawLine(x,
* y+height, x+width, y+height, paint); c.drawLine(x, y, x, y+height, paint); c.drawLine(x+width, y, x+width,
* y+height, paint); paint.setStyle(Style.FILL); paint.setColor(Color.BLUE); c.drawRect(rect, paint);
* 这个规则跟j2me也是一样的,在j2me里,drawRect长宽会多画出1px。SDK的说明是:
*
* The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width
* or height is less than zero, nothing is drawn.
*
* 例如drawRect(10,10,100,1)绘制,结果是一个2px高的矩形,用fillRect(10,10,100,1),结果是一个1px高的矩形
*
* 以上就是对Android绘图的具体介绍。
*/
}
/**
* 在布局文件中引用 这样引用就行了..吼吼 <com.xiaoma.shadedemo.TextViewBorder android:id="@+id/a02_txtKSSJ" android:textColor="#000000"
* android:layout_marginLeft="10dip" android:layout_width="100dip" android:layout_height="wrap_content" />
*/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<!-- <solid android:color="#cceeff"/> 直接写这个的话,可以给控制添加一个整体的背景哦 -->
<
stroke
android:width
=
"0.5dp"
android:color
=
"#22ccff"
/>
<
padding
android:left
=
"5dp"
android:top
=
"5dp"
android:right
=
"5dp"
android:bottom
=
"5dp"
/>
<
size
android:height
=
"0.5dp"
android:width
=
"5dp"
/>
<!-- 目前没有什么用,可删除,留在这个地方防止乱猜 -->
<
corners
android:radius
=
"10dp"
/>
<!-- 这个radius里面的值需要个整型,单位用dp,用其它单位亦无影响 -->
</
shape
>
|
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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<!-- 今天主要讲的是shape渲染,shape主要类型分四种,效果如下,我们常用rectangle,也就是矩形渲染,其它的都太丑了!! -->
<
item
android:state_pressed
=
"true"
>
<!--按钮按下时的渲染效果 -->
<
shape
android:shape
=
"oval"
>
<
corners
android:radius
=
"10dp"
/>
<!-- 四个角的角度 -->
<!--gradient就是梯度渲染,简单说就是颜色渐变,type为渐变方式,总共三种 linear sweep ridial,常用linear-->
<
gradient
android:endColor
=
"#eebbbb"
android:startColor
=
"#9900ee"
android:type
=
"linear"
/>
<!-- padding属性是指定内容与控件边距,这个地方小马专门将距左边距设置较大,方便观察 -->
<
padding
android:bottom
=
"5dp"
android:left
=
"20dp"
android:right
=
"5dp"
android:top
=
"5dp"
/>
<!-- solid填充整个区域,颜色为FFDDFF,如果使用整个区域填充的话,上面的gradient梯度会失效,即:覆盖 -->
<!-- <solid android:color="#FFDDFF"/> -->
<!-- stroke为需要填充的边框,指定颜色及边框宽度 -->
<
stroke
android:width
=
"3dp"
android:color
=
"#000000"
/>
</
shape
>
<!-- <clip android:clipOrientation="vertical" android:gravity="right" android:drawable="@drawable/ic_launcher" /> -->
<!-- 试了下没用 -->
<!-- <color android:color="#223344"/> -->
<!-- <scale android:scaleWidth="15dp" android:scaleHeight=" 5dp" android:scaleGravity="center" /> -->
</
item
>
<
item
>
<!-- 默认 -->
<
shape
android:shape
=
"rectangle"
>
<
corners
android:radius
=
"10dp"
/>
<!-- 四个角的角度 -->
<!--gradient就是梯度渲染,简单说就是颜色渐变,type为渐变方式,总共三种 linear sweep ridial,常用linear-->
<!-- 这个地方一定注意,在使用gradient标签中使用android:type的前提是必须android:gradientRadius="20dp"已经设置,否则会报错 -->
<
gradient
android:endColor
=
"#acefda"
android:startColor
=
"#0099ff"
android:type
=
"linear"
/>
<!-- padding属性是指定内容与控件边距,这个地方小马专门将距左边距设置较大,方便观察 -->
<
padding
android:bottom
=
"5dp"
android:left
=
"20dp"
android:right
=
"5dp"
android:top
=
"5dp"
/>
<!-- solid填充整个区域,颜色为FFDDFF,如果使用整个区域填充的话,上面的gradient梯度会失效,即:覆盖 -->
<!-- <solid android:color="#FFDDFF"/> -->
<!-- stroke为需要填充的边框,指定颜色及边框宽度 -->
<
stroke
android:width
=
"3dp"
android:color
=
"#000000"
/>
</
shape
>
</
item
>
</
selector
>
|