blob: d9fb66a62ff8e3e974377399dbe6ee7531120033 [file] [log] [blame]
Joe Onorato59791172009-07-31 16:21:40 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.ComponentName;
22import android.content.res.TypedArray;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.RectF;
27import android.graphics.Rect;
28import android.graphics.Region;
29import android.graphics.drawable.Drawable;
30import android.util.AttributeSet;
31import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
35import android.view.ViewGroup;
36import android.view.ViewParent;
37import android.widget.Scroller;
38import android.widget.TextView;
39import android.os.Parcelable;
40import android.os.Parcel;
41
42import java.util.ArrayList;
43
44/**
45 * Wallpaper view shows the wallpaper bitmap, which is far layer in the parallax.
46 */
47public 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