blob: f512cefa111191da8ee5740e549186eafaa6f531 [file] [log] [blame]
Michael Kolbc7485ae2010-09-03 10:10:58 -07001
2/*
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package com.android.browser;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Rect;
22import android.graphics.drawable.Drawable;
Michael Kolbb7b115e2010-09-25 16:59:37 -070023import android.os.Handler;
24import android.os.Message;
Michael Kolbc7485ae2010-09-03 10:10:58 -070025import android.util.AttributeSet;
26import android.widget.ImageView;
27
28/**
29 *
30 */
31public class PageProgressView extends ImageView {
32
Michael Kolbb7b115e2010-09-25 16:59:37 -070033 public static final int MAX_PROGRESS = 10000;
34 private static final int MSG_UPDATE = 42;
35 private static final int STEPS = 10;
36 private static final int DELAY = 40;
37
38 private int mCurrentProgress;
39 private int mTargetProgress;
40 private int mIncrement;
Michael Kolbc7485ae2010-09-03 10:10:58 -070041 private Rect mBounds;
Michael Kolbb7b115e2010-09-25 16:59:37 -070042 private Handler mHandler;
Michael Kolbc7485ae2010-09-03 10:10:58 -070043
44 /**
45 * @param context
46 * @param attrs
47 * @param defStyle
48 */
49 public PageProgressView(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
51 init(context);
52 }
53
54 /**
55 * @param context
56 * @param attrs
57 */
58 public PageProgressView(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 init(context);
61 }
62
63 /**
64 * @param context
65 */
66 public PageProgressView(Context context) {
67 super(context);
68 init(context);
69 }
70
71 private void init(Context ctx) {
Michael Kolbc7485ae2010-09-03 10:10:58 -070072 mBounds = new Rect(0,0,0,0);
Michael Kolbb7b115e2010-09-25 16:59:37 -070073 mCurrentProgress = 0;
74 mTargetProgress = 0;
75 mHandler = new Handler() {
76
77 @Override
78 public void handleMessage(Message msg) {
79 if (msg.what == MSG_UPDATE) {
80 mCurrentProgress = Math.min(mTargetProgress,
81 mCurrentProgress + mIncrement);
82 mBounds.right = getWidth() * mCurrentProgress / MAX_PROGRESS;
83 invalidate();
84 if (mCurrentProgress < mTargetProgress) {
85 sendMessageDelayed(mHandler.obtainMessage(MSG_UPDATE), DELAY);
86 }
87 }
88 }
89
90 };
Michael Kolbc7485ae2010-09-03 10:10:58 -070091 }
92
93 @Override
94 public void onLayout(boolean f, int l, int t, int r, int b) {
95 mBounds.left = 0;
Michael Kolbb7b115e2010-09-25 16:59:37 -070096 mBounds.right = (r - l) * mCurrentProgress / MAX_PROGRESS;
Michael Kolbc7485ae2010-09-03 10:10:58 -070097 mBounds.top = 0;
98 mBounds.bottom = b-t;
99 }
100
Michael Kolbc7485ae2010-09-03 10:10:58 -0700101 void setProgress(int progress) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700102 mCurrentProgress = mTargetProgress;
103 mTargetProgress = progress;
104 mIncrement = (mTargetProgress - mCurrentProgress) / STEPS;
105 mHandler.removeMessages(MSG_UPDATE);
106 mHandler.sendEmptyMessage(MSG_UPDATE);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700107 }
108
109 @Override
110 public void onDraw(Canvas canvas) {
111// super.onDraw(canvas);
112 Drawable d = getDrawable();
113 d.setBounds(mBounds);
114 d.draw(canvas);
115 }
116
117}