自定义view,实现梯形报表.
[Java]代码
001 |
package com.test; |
002 |
|
003 |
import android.content.Context; |
004 |
import android.graphics.Bitmap; |
005 |
import android.graphics.BitmapFactory; |
006 |
import android.graphics.Canvas; |
007 |
import android.graphics.Color; |
008 |
import android.graphics.Paint; |
009 |
import android.graphics.Path; |
010 |
import android.graphics.Rect; |
011 |
import android.view.View; |
012 |
import android.view.ViewGroup.LayoutParams; |
013 |
|
014 |
public class Ladder extends View { |
015 |
private Context context= null ; |
016 |
private final static String TAG = "Ladder" ; |
017 |
private final static int SCREEN_WIDTH = 300 ; // screen width |
018 |
private final static int SCREEN_HEIGHT = 300 ; // screen height |
019 |
private final static int CONTAINER_BOTTOM_WIDTH = 50 ; // ladder bottom width |
020 |
private final static int CONTAINER_TOP_WIDTH = 200 ; // ladder top width |
021 |
private final static int CONTAINER_BOTTOM_LEFT = (SCREEN_WIDTH - CONTAINER_BOTTOM_WIDTH) / 2 ; // the |
022 |
// distance |
023 |
// of |
024 |
// bottom-left |
025 |
private final static int CONTAINER_BOTTOM_RIGHT = (SCREEN_WIDTH + CONTAINER_BOTTOM_WIDTH) / 2 ; // the |
026 |
// distance |
027 |
// of |
028 |
// bottom-right |
029 |
private final static int CONTAINER_TOP_LEFT = (SCREEN_WIDTH - CONTAINER_TOP_WIDTH) / 2 ; // the |
030 |
// distance |
031 |
// of |
032 |
// top-left |
033 |
private final static int CONTAINER_TOP_RIGHT = (SCREEN_WIDTH + CONTAINER_TOP_WIDTH) / 2 ; // the |
034 |
// distance |
035 |
// of |
036 |
// top-left |
037 |
private float first_height = 0 ; // the first appoach |
038 |
private float second_height = 0 ; // interview |
039 |
private float third_height = 0 ; // advise |
040 |
private Path path1; |
041 |
|
042 |
private Paint paint1; |
043 |
|
044 |
private float total; |
045 |
private float a; |
046 |
private float b; |
047 |
private float c; |
048 |
private float d; |
049 |
|
050 |
public Ladder(Context context, int a, int b, int c, int d) { |
051 |
super (context); |
052 |
this .context=context; |
053 |
setLayoutParams( new LayoutParams( 300 , 300 )); |
054 |
setBackgroundColor(Color.TRANSPARENT); |
055 |
init(a, b, c, d); |
056 |
paint1 = new Paint(); |
057 |
paint1.setAntiAlias( true ); |
058 |
paint1.setStrokeWidth( 0 ); |
059 |
|
060 |
} |
061 |
|
062 |
private void init( int a, int b, int c, int d) { |
063 |
total = a + b + c + d; |
064 |
this .a = a; |
065 |
this .b = b; |
066 |
this .c = c; |
067 |
this .d = d; |
068 |
first_height = (a / total * SCREEN_HEIGHT); |
069 |
second_height = (b / total * SCREEN_HEIGHT); |
070 |
third_height = (c / total * SCREEN_HEIGHT); |
071 |
} |
072 |
|
073 |
private void draw_Container(Canvas canvas) { |
074 |
paint1.setColor(Color.BLACK); |
075 |
paint1.setStyle(Paint.Style.STROKE); |
076 |
path1 = new Path(); |
077 |
path1.moveTo(CONTAINER_BOTTOM_LEFT, SCREEN_HEIGHT); |
078 |
path1.lineTo(CONTAINER_BOTTOM_RIGHT, SCREEN_HEIGHT); |
079 |
path1.lineTo(CONTAINER_TOP_RIGHT, SCREEN_HEIGHT - SCREEN_HEIGHT); |
080 |
path1.lineTo(CONTAINER_TOP_LEFT, SCREEN_HEIGHT - SCREEN_HEIGHT); |
081 |
path1.close(); |
082 |
canvas.drawPath(path1, paint1); |
083 |
} |
084 |
|
085 |
private void draw_1(Canvas canvas) { |
086 |
double first_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
087 |
/ 2 * a / total * 2 + CONTAINER_BOTTOM_WIDTH; |
088 |
|
089 |
paint1.setColor(Color.YELLOW); |
090 |
paint1.setStyle(Paint.Style.FILL_AND_STROKE); |
091 |
path1 = new Path(); |
092 |
path1.moveTo(CONTAINER_BOTTOM_LEFT, SCREEN_HEIGHT); |
093 |
path1.lineTo(CONTAINER_BOTTOM_RIGHT, SCREEN_HEIGHT); |
094 |
|
095 |
path1.lineTo(( float ) ((SCREEN_WIDTH + first_bottom_width) / 2 ), |
096 |
SCREEN_HEIGHT - first_height); |
097 |
path1.lineTo(( float ) ((SCREEN_WIDTH - first_bottom_width) / 2 ), |
098 |
SCREEN_HEIGHT - first_height); |
099 |
path1.close(); |
100 |
canvas.drawPath(path1, paint1); |
101 |
|
102 |
} |
103 |
|
104 |
private void draw_2(Canvas canvas) { |
105 |
double first_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
106 |
/ 2 * a / total * 2 + CONTAINER_BOTTOM_WIDTH; |
107 |
double second_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
108 |
/ 2 * (a + b) / total * 2 + CONTAINER_BOTTOM_WIDTH; |
109 |
paint1.setColor(Color.parseColor( "#EA7500" )); |
110 |
paint1.setStyle(Paint.Style.FILL_AND_STROKE); |
111 |
path1 = new Path(); |
112 |
path1.moveTo(( float ) ((SCREEN_WIDTH - first_bottom_width) / 2 ), |
113 |
SCREEN_HEIGHT - first_height); |
114 |
path1.lineTo(( float ) ((SCREEN_WIDTH + first_bottom_width) / 2 ), |
115 |
SCREEN_HEIGHT - first_height); |
116 |
|
117 |
path1.lineTo(( float ) ((SCREEN_WIDTH + second_bottom_width) / 2 ), |
118 |
SCREEN_HEIGHT - (first_height + second_height)); |
119 |
path1.lineTo(( float ) ((SCREEN_WIDTH - second_bottom_width) / 2 ), |
120 |
SCREEN_HEIGHT - (first_height + second_height)); |
121 |
path1.close(); |
122 |
canvas.drawPath(path1, paint1); |
123 |
} |
124 |
|
125 |
private void draw_3(Canvas canvas) { |
126 |
double first_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
127 |
/ 2 * (a + b) / total * 2 + CONTAINER_BOTTOM_WIDTH; |
128 |
double second_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
129 |
/ 2 * (a + b + c) / total * 2 + CONTAINER_BOTTOM_WIDTH; |
130 |
paint1.setColor(Color.parseColor( "#A8FF24" )); |
131 |
paint1.setStyle(Paint.Style.FILL_AND_STROKE); |
132 |
path1 = new Path(); |
133 |
path1.moveTo(( float ) ((SCREEN_WIDTH - first_bottom_width) / 2 ), |
134 |
SCREEN_HEIGHT - (first_height + second_height)); |
135 |
path1.lineTo(( float ) ((SCREEN_WIDTH + first_bottom_width) / 2 ), |
136 |
SCREEN_HEIGHT - (first_height + second_height)); |
137 |
|
138 |
path1.lineTo(( float ) ((SCREEN_WIDTH + second_bottom_width) / 2 ), |
139 |
SCREEN_HEIGHT - (first_height + second_height + third_height)); |
140 |
path1.lineTo(( float ) ((SCREEN_WIDTH - second_bottom_width) / 2 ), |
141 |
SCREEN_HEIGHT - (first_height + second_height + third_height)); |
142 |
path1.close(); |
143 |
canvas.drawPath(path1, paint1); |
144 |
} |
145 |
|
146 |
private void draw_4(Canvas canvas) { |
147 |
double first_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
148 |
/ 2 * (a + b + c) / total * 2 + CONTAINER_BOTTOM_WIDTH; |
149 |
double second_bottom_width = (CONTAINER_TOP_WIDTH - CONTAINER_BOTTOM_WIDTH) |
150 |
/ 2 * (a + b + c + d) / total * 2 + CONTAINER_BOTTOM_WIDTH; |
151 |
paint1.setColor(Color.parseColor( "#A4A4FF" )); |
152 |
paint1.setStyle(Paint.Style.FILL_AND_STROKE); |
153 |
path1 = new Path(); |
154 |
path1.moveTo(( float ) ((SCREEN_WIDTH - first_bottom_width) / 2 ), |
155 |
SCREEN_HEIGHT - (first_height + second_height + third_height)); |
156 |
path1.lineTo(( float ) ((SCREEN_WIDTH + first_bottom_width) / 2 ), |
157 |
SCREEN_HEIGHT - (first_height + second_height + third_height)); |
158 |
|
159 |
path1.lineTo(( float ) ((SCREEN_WIDTH + second_bottom_width) / 2 ), 0 ); |
160 |
path1.lineTo(( float ) ((SCREEN_WIDTH - second_bottom_width) / 2 ), 0 ); |
161 |
path1.close(); |
162 |
canvas.drawPath(path1, paint1); |
163 |
} |
164 |
|
165 |
private void draw_msg(Canvas canvas) { |
166 |
Rect rect = new Rect( 10 , SCREEN_HEIGHT- 100 , 20 , SCREEN_HEIGHT - 90 ); |
167 |
paint1.setColor(Color.BLACK); |
168 |
canvas.drawText( "接近\r" + (( int ) (Math.round(d / total * 100 ))) + "%" , 20 , |
169 |
SCREEN_HEIGHT- 90 , paint1); |
170 |
|
171 |
paint1.setColor(Color.parseColor( "#A4A4FF" )); |
172 |
canvas.drawRect(rect, paint1); |
173 |
|
174 |
|
175 |
rect = new Rect( 10 , SCREEN_HEIGHT- 80 , 20 , SCREEN_HEIGHT - 70 ); |
176 |
paint1.setColor(Color.BLACK); |
177 |
canvas.drawText( "面谈\r" + (( int ) (Math.round(c / total * 100 ))) + "%" , 20 , |
178 |
SCREEN_HEIGHT- 70 , paint1); |
179 |
|
180 |
paint1.setColor(Color.parseColor( "#A8FF24" )); |
181 |
canvas.drawRect(rect, paint1); |
182 |
|
183 |
|
184 |
rect = new Rect( 10 , SCREEN_HEIGHT- 60 , 20 , SCREEN_HEIGHT - 50 ); |
185 |
paint1.setColor(Color.BLACK); |
186 |
canvas.drawText( "说明/建议书" + (( int )(Math.round(b / total * 100 )))+ "%" , 20 , |
187 |
SCREEN_HEIGHT- 50 , paint1); |
188 |
|
189 |
paint1.setColor(Color.parseColor( "#EA7500" )); |
190 |
canvas.drawRect(rect, paint1); |
191 |
|
192 |
|
193 |
rect = new Rect( 10 , SCREEN_HEIGHT- 40 , 20 , SCREEN_HEIGHT - 30 ); |
194 |
paint1.setColor(Color.BLACK); |
195 |
canvas.drawText( "缔约\r" + (( int ) (Math.round(a / total * 100 ))) + "%" , 20 , |
196 |
SCREEN_HEIGHT- 30 , paint1); |
197 |
|
198 |
paint1.setColor(Color.YELLOW); |
199 |
canvas.drawRect(rect, paint1); |
200 |
} |
201 |
|
202 |
@Override |
203 |
protected void onDraw(Canvas canvas) { |
204 |
super .onDraw(canvas); |
205 |
// canvas.drawColor(Color.WHITE); |
206 |
// canvas.drawBitmap(context.getResources().getDrawable(R.id.wkworkspace_background_wood), null); |
207 |
// Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.wkworkspace_background_wood); |
208 |
|
209 |
// canvas.drawBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.wkworkspace_background_wood),0,0,null); |
210 |
draw_Container(canvas); |
211 |
draw_1(canvas); //缔约 |
212 |
draw_2(canvas); //说明/建议书 |
213 |
draw_3(canvas); //面谈 |
214 |
draw_4(canvas); //接近 |
215 |
draw_msg(canvas); |
216 |
|
217 |
} |
218 |
|
219 |
@Override |
220 |
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { |
221 |
super .onMeasure(widthMeasureSpec, heightMeasureSpec); |
222 |
|
223 |
} |
224 |
|
225 |
} |