Joe Onorato | 5979117 | 2009-07-31 16:21:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.RectF; |
| 27 | import android.graphics.Rect; |
| 28 | import android.graphics.Region; |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.view.MotionEvent; |
| 32 | import android.view.VelocityTracker; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewConfiguration; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.view.ViewParent; |
| 37 | import android.widget.Scroller; |
| 38 | import android.widget.TextView; |
| 39 | import android.os.Parcelable; |
| 40 | import android.os.Parcel; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | |
| 44 | /** |
| 45 | * Wallpaper view shows the wallpaper bitmap, which is far layer in the parallax. |
| 46 | */ |
| 47 | public class WallpaperView extends View { |
| 48 | |
| 49 | private int mScreenCount; |
| 50 | |
| 51 | private Paint mPaint; |
| 52 | private Bitmap mWallpaper; |
| 53 | |
| 54 | private int mWallpaperWidth; |
| 55 | private int mWallpaperHeight; |
| 56 | private float mWallpaperOffset; |
| 57 | private boolean mWallpaperLoaded; |
| 58 | |
| 59 | /** |
| 60 | * Used to inflate the Workspace from XML. |
| 61 | * |
| 62 | * @param context The application's context. |
| 63 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 64 | */ |
| 65 | public WallpaperView(Context context, AttributeSet attrs) { |
| 66 | this(context, attrs, 0); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Used to inflate the Workspace from XML. |
| 71 | * |
| 72 | * @param context The application's context. |
| 73 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 74 | * @param defStyle Unused. |
| 75 | */ |
| 76 | public WallpaperView(Context context, AttributeSet attrs, int defStyle) { |
| 77 | super(context, attrs, defStyle); |
| 78 | |
| 79 | initWorkspace(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Initializes various states for this workspace. |
| 84 | */ |
| 85 | private void initWorkspace() { |
| 86 | mPaint = new Paint(); |
| 87 | mPaint.setDither(false); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set the background's wallpaper. |
| 92 | */ |
| 93 | void loadWallpaper(Bitmap bitmap) { |
| 94 | mWallpaper = bitmap; |
| 95 | mWallpaperLoaded = true; |
| 96 | requestLayout(); |
| 97 | invalidate(); |
| 98 | } |
| 99 | |
| 100 | void setScreenCount(int count) { |
| 101 | mScreenCount = count; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean isOpaque() { |
| 106 | return !mWallpaper.hasAlpha(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onDraw(Canvas canvas) { |
| 111 | boolean restore = false; |
| 112 | |
| 113 | float x = mScrollX * mWallpaperOffset; |
| 114 | if (x + mWallpaperWidth < mRight - mLeft) { |
| 115 | x = mRight - mLeft - mWallpaperWidth; |
| 116 | } |
| 117 | |
| 118 | canvas.drawBitmap(mWallpaper, x, (mBottom - mTop - mWallpaperHeight) / 2, mPaint); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void onSizeChanged(int width, int height, int oldw, int oldh) { |
| 123 | |
| 124 | if (mWallpaperLoaded) { |
| 125 | mWallpaperLoaded = false; |
| 126 | mWallpaper = Utilities.centerToFit(mWallpaper, width, height, getContext()); |
| 127 | mWallpaperWidth = mWallpaper.getWidth(); |
| 128 | mWallpaperHeight = mWallpaper.getHeight(); |
| 129 | } |
| 130 | |
| 131 | final int wallpaperWidth = mWallpaperWidth; |
| 132 | mWallpaperOffset = wallpaperWidth > width ? (mScreenCount * width - wallpaperWidth) / |
| 133 | ((mScreenCount - 1) * (float) width) : 1.0f; |
| 134 | } |
| 135 | } |
| 136 | |