Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 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.launcher3; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Paint; |
| 24 | import android.graphics.Point; |
| 25 | import android.graphics.PorterDuff; |
| 26 | import android.graphics.PorterDuffXfermode; |
| 27 | import android.graphics.Rect; |
| 28 | import android.util.AttributeSet; |
| 29 | import android.util.DisplayMetrics; |
| 30 | import android.view.View; |
| 31 | |
| 32 | public class ClearCircleLayout extends View { |
| 33 | |
| 34 | private static final String HOLE_LOCATION_PAGE_INDICATOR = "page_indicator"; |
| 35 | private static final String HOLE_LOCATION_CENTER_SCREEN = "center_screen"; |
| 36 | |
| 37 | private static final int BACKGROUND_COLOR = 0x80000000; |
| 38 | private static float MIGRATION_WORKSPACE_INNER_CIRCLE_RADIUS_DPS = 42; |
| 39 | private static float MIGRATION_WORKSPACE_OUTER_CIRCLE_RADIUS_DPS = 46; |
| 40 | |
| 41 | private final String mHoleLocation; |
| 42 | private final Paint mErasePaint; |
| 43 | private final Paint mBorderPaint; |
| 44 | |
| 45 | private Launcher mLauncher; |
| 46 | private Point mHoleCenter; |
| 47 | private DisplayMetrics mMetrics; |
| 48 | |
| 49 | public ClearCircleLayout(Context context, AttributeSet attrs) { |
| 50 | super(context, attrs); |
| 51 | |
| 52 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ClearCircleLayout); |
| 53 | mHoleLocation = a.getString(R.styleable.ClearCircleLayout_holeLocation); |
| 54 | a.recycle(); |
| 55 | |
| 56 | mErasePaint = new Paint(); |
| 57 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)); |
| 58 | mErasePaint.setColor(0xFFFFFF); |
| 59 | mErasePaint.setAlpha(0); |
| 60 | mErasePaint.setAntiAlias(true); |
| 61 | |
| 62 | mBorderPaint = new Paint(); |
| 63 | mBorderPaint.setColor(0xFFFFFFFF); |
| 64 | mBorderPaint.setAntiAlias(true); |
| 65 | } |
| 66 | |
| 67 | void initHole(Launcher launcher) { |
| 68 | mLauncher = launcher; |
| 69 | mMetrics = new DisplayMetrics(); |
| 70 | launcher.getWindowManager().getDefaultDisplay().getMetrics(mMetrics); |
| 71 | |
| 72 | if (mHoleLocation.endsWith(HOLE_LOCATION_PAGE_INDICATOR)) { |
| 73 | LauncherAppState app = LauncherAppState.getInstance(); |
| 74 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 75 | |
| 76 | Rect indicator = grid.getWorkspacePageIndicatorBounds(new Rect()); |
| 77 | mHoleCenter = new Point(indicator.centerX(), indicator.centerY()); |
| 78 | } else if (mHoleLocation.endsWith(HOLE_LOCATION_CENTER_SCREEN)) { |
| 79 | mHoleCenter = new Point(mMetrics.widthPixels / 2, mMetrics.heightPixels / 2); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected void dispatchDraw(Canvas canvas) { |
| 85 | if (mHoleCenter == null) { |
| 86 | canvas.drawColor(BACKGROUND_COLOR); |
| 87 | } else { |
| 88 | drawHole(canvas); |
| 89 | } |
| 90 | |
| 91 | super.dispatchDraw(canvas); |
| 92 | } |
| 93 | |
| 94 | private void drawHole(Canvas canvas) { |
| 95 | // Initialize the draw buffer (to allow punching through) |
| 96 | Bitmap eraseBg = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 97 | Bitmap.Config.ARGB_8888); |
| 98 | Canvas eraseCanvas = new Canvas(eraseBg); |
| 99 | eraseCanvas.drawColor(BACKGROUND_COLOR); |
| 100 | |
| 101 | Rect insets = mLauncher.getDragLayer().getInsets(); |
| 102 | float x = mHoleCenter.x - insets.left; |
| 103 | float y = mHoleCenter.y - insets.top; |
| 104 | // Draw the outer circle |
| 105 | eraseCanvas.drawCircle(x, y, |
| 106 | DynamicGrid.pxFromDp(MIGRATION_WORKSPACE_OUTER_CIRCLE_RADIUS_DPS, mMetrics), |
| 107 | mBorderPaint); |
| 108 | |
| 109 | // Draw the inner circle |
| 110 | eraseCanvas.drawCircle(x, y, |
| 111 | DynamicGrid.pxFromDp(MIGRATION_WORKSPACE_INNER_CIRCLE_RADIUS_DPS, mMetrics), |
| 112 | mErasePaint); |
| 113 | |
| 114 | canvas.drawBitmap(eraseBg, 0, 0, null); |
| 115 | eraseCanvas.setBitmap(null); |
| 116 | eraseBg.recycle(); |
| 117 | } |
| 118 | } |