当前位置:操作系统 > 安卓/Android >>

android屏幕分页,页标

 

1 public class PagePoint extends View {

 2

 3     private String TAG = "PagePoint";

 4     Paint p;

 5     int totalPage;//总页面数

 6     int currentPage;

 7     Bitmap bmp, bmp2;

 8     int screenW;

 9     int screenH;

10     boolean flag = false;

11

12     int ws;//图片的宽

13     int dw = 10;//点之间的间距

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);// 只读,不能直接在bmp上画

27         bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.clicked);// 只读,不能直接在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;//点的起始位置

84

85         for (int i = 0; i < totalPage; i++) {

86             Log.i(TAG, "startX" + (startX + (ws + dw) * i) + "----------onDraw----------");

89             canvas.drawBitmap(currentPage == i ? bmp2 : bmp, startX + (ws + dw) * i, screenH - 213 + ws / 2, p);

90         }

91

92     }

93 }

下面是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     //模拟翻页的时候切换页标

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内容:

 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"补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,