1 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 */ 17 package com.android.browser; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Rect; 22 import android.graphics.drawable.Drawable; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.util.AttributeSet; 26 import android.widget.ImageView; 27 28 /** 29 * 30 */ 31 public class PageProgressView extends ImageView { 32 33 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; 41 private Rect mBounds; 42 private Handler mHandler; 43 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) { 72 mBounds = new Rect(0,0,0,0); 73 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 }; 91 } 92 93 @Override 94 public void onLayout(boolean f, int l, int t, int r, int b) { 95 mBounds.left = 0; 96 mBounds.right = (r - l) * mCurrentProgress / MAX_PROGRESS; 97 mBounds.top = 0; 98 mBounds.bottom = b-t; 99 } 100 101 void setProgress(int progress) { 102 mCurrentProgress = mTargetProgress; 103 mTargetProgress = progress; 104 mIncrement = (mTargetProgress - mCurrentProgress) / STEPS; 105 mHandler.removeMessages(MSG_UPDATE); 106 mHandler.sendEmptyMessage(MSG_UPDATE); 107 } 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 } 118