1 public class PagePoint extends View {
2
3 private String TAG = "PagePoint ";
4 Paint p;
5 int totalPage; // the total number of pages.
6 int currentPage;
7 Bitmap bmp, bmp 2;
8 int screenW;
9 int screenH;
10 boolean flag = false;
11
12 int ws; // The Image Width
13 int dw = 10; // distance between points
14 int totalWidth;
15 int startX;
16
17 public PagePoint (Context context, AttributeSet attrs, int defStyle ){
18 super (context, attrs, defStyle );
19 p = new Paint ();
20 p. setFlags (Paint. ANTI_ALIAS_FLAG );
21
22 DisplayMetrics displayMetrics = getResources (). getDisplayMetrics ();
23 screenW = displayMetrics. widthPixels;
24 screenH = displayMetrics. heightPixels;
25
26 bmp = BitmapFactory. decodeResource (getResources (), R. drawable. click); // read-only, cannot be drawn directly on bmp
27 bmp 2 = BitmapFactory. decodeResource (getResources (), R. drawable. clicked); // read-only, cannot be drawn directly on bmp
28
29 ws = bmp. getWidth ();
30 dw = 10;
31
32}
33
34 public PagePoint (Context context, AttributeSet attrs ){
35 this (context, attrs, 0 );
36
37}
38
39 public PagePoint (Context context ){
40 this (context, null );
41
42}
43
44 public void addPagePoint (int number ){
45
46 totalPage = number;
47 currentPage = 0;
48
49}
50
51 public void toPagePoint (int currentPage, int nextOrPre ){
52
53 flag = true;
54 this. currentPage = nextOrPre;
55 totalWidth = ws * totalPage + dw * (totalPage-1 );
56 startX = (screenW-totalWidth)/2;
59 invalidate (startX, screenH-213-ws, startX + (ws + dw) * totalPage, screenH );
60 // invalidate ();
61}
62
63 @ Override
64 public boolean onTouchEvent (MotionEvent event ){
65 Log. I (TAG, "---------- onTouchEvent ----------");
66
67 return super. onTouchEvent (event );
68}
69
70 @ Override
71 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
72 Log. I (TAG, "---------- onMeasure ----------");
73
74 super. onMeasure (widthMeasureSpec, heightMeasureSpec );
75}
76
77 @ Override
78 protected void onDraw (Canvas canvas ){
79 super. onDraw (canvas );
80 Log. I (TAG, "---------- onDraw ----------");
81
82 totalWidth = ws * totalPage + dw * (totalPage-1 );
83 startX = (screenW-totalWidth)/2; // start position of the vertex
84
85 for (int I = 0; I <totalPage; I ++ ){
86 Log. I (TAG, "startX" + (startX + (ws + dw) * I) + "---------- onDraw ----------");
89 canvas. drawBitmap (currentPage = I? Bmp 2: bmp, startX + (ws + dw) * I, screenH-213 + ws/2, p );
90}
91
92}
93}
The following is the use of pagepoint:
1 public class TestPoint extends Activity {
2
3 Handler handler = new Handler ();
4 PagePoint pagePoint;
5
6
7/** Called when the activity is first created .*/
8 @ Override
9 public void onCreate (Bundle savedInstanceState ){
10 super. onCreate (savedInstanceState );
11
12
13 pagePoint = (PagePoint) findViewById (R. id. test );
14 pagePoint. addPagePoint (2 );
15 // when page turning is simulated
16 handler. postDelayed (new Runnable (){
17
18 @ Override
19 public void run (){
20 for (int I = 1; I <5; I ++ ){
21 pagePoint. toPagePoint (0, I );
22 Log. I ("", "---------- onDraw invalidate ----------");
23
24 break;
25}
26
27}
28}, 2000 );
29
30}
31}
Main. xml content:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3 android: layout_width = "fill_parent"
4 android: layout_height = "fill_parent"
5 android: background = "@ drawable/background"
6 android: orientation = "vertical">
7 <com. hyl. app. PagePoint
8 android: id = "@ + id/test"
9 android: layout_width = "fill_parent"
10 android: layout_height = "wrap_content"/>
11 </FrameLayout>
:
Author YunLin