Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2013 Square, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | package com.squareup.picasso; |
michael@0 | 17 | |
michael@0 | 18 | import android.content.Context; |
michael@0 | 19 | import android.content.res.Resources; |
michael@0 | 20 | import android.graphics.Bitmap; |
michael@0 | 21 | import android.graphics.Canvas; |
michael@0 | 22 | import android.graphics.ColorFilter; |
michael@0 | 23 | import android.graphics.Paint; |
michael@0 | 24 | import android.graphics.Path; |
michael@0 | 25 | import android.graphics.Point; |
michael@0 | 26 | import android.graphics.Rect; |
michael@0 | 27 | import android.graphics.drawable.AnimationDrawable; |
michael@0 | 28 | import android.graphics.drawable.BitmapDrawable; |
michael@0 | 29 | import android.graphics.drawable.Drawable; |
michael@0 | 30 | import android.os.SystemClock; |
michael@0 | 31 | import android.widget.ImageView; |
michael@0 | 32 | |
michael@0 | 33 | import static android.graphics.Color.WHITE; |
michael@0 | 34 | import static com.squareup.picasso.Picasso.LoadedFrom.MEMORY; |
michael@0 | 35 | |
michael@0 | 36 | final class PicassoDrawable extends Drawable { |
michael@0 | 37 | // Only accessed from main thread. |
michael@0 | 38 | private static final Paint DEBUG_PAINT = new Paint(); |
michael@0 | 39 | |
michael@0 | 40 | private static final float FADE_DURATION = 200f; //ms |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Create or update the drawable on the target {@link ImageView} to display the supplied bitmap |
michael@0 | 44 | * image. |
michael@0 | 45 | */ |
michael@0 | 46 | static void setBitmap(ImageView target, Context context, Bitmap bitmap, |
michael@0 | 47 | Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) { |
michael@0 | 48 | Drawable placeholder = target.getDrawable(); |
michael@0 | 49 | if (placeholder instanceof AnimationDrawable) { |
michael@0 | 50 | ((AnimationDrawable) placeholder).stop(); |
michael@0 | 51 | } |
michael@0 | 52 | PicassoDrawable drawable = |
michael@0 | 53 | new PicassoDrawable(context, placeholder, bitmap, loadedFrom, noFade, debugging); |
michael@0 | 54 | target.setImageDrawable(drawable); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Create or update the drawable on the target {@link ImageView} to display the supplied |
michael@0 | 59 | * placeholder image. |
michael@0 | 60 | */ |
michael@0 | 61 | static void setPlaceholder(ImageView target, int placeholderResId, Drawable placeholderDrawable) { |
michael@0 | 62 | if (placeholderResId != 0) { |
michael@0 | 63 | target.setImageResource(placeholderResId); |
michael@0 | 64 | } else { |
michael@0 | 65 | target.setImageDrawable(placeholderDrawable); |
michael@0 | 66 | } |
michael@0 | 67 | if (target.getDrawable() instanceof AnimationDrawable) { |
michael@0 | 68 | ((AnimationDrawable) target.getDrawable()).start(); |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | private final boolean debugging; |
michael@0 | 73 | private final float density; |
michael@0 | 74 | private final Picasso.LoadedFrom loadedFrom; |
michael@0 | 75 | final BitmapDrawable image; |
michael@0 | 76 | |
michael@0 | 77 | Drawable placeholder; |
michael@0 | 78 | |
michael@0 | 79 | long startTimeMillis; |
michael@0 | 80 | boolean animating; |
michael@0 | 81 | int alpha = 0xFF; |
michael@0 | 82 | |
michael@0 | 83 | PicassoDrawable(Context context, Drawable placeholder, Bitmap bitmap, |
michael@0 | 84 | Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) { |
michael@0 | 85 | Resources res = context.getResources(); |
michael@0 | 86 | |
michael@0 | 87 | this.debugging = debugging; |
michael@0 | 88 | this.density = res.getDisplayMetrics().density; |
michael@0 | 89 | |
michael@0 | 90 | this.loadedFrom = loadedFrom; |
michael@0 | 91 | |
michael@0 | 92 | this.image = new BitmapDrawable(res, bitmap); |
michael@0 | 93 | |
michael@0 | 94 | boolean fade = loadedFrom != MEMORY && !noFade; |
michael@0 | 95 | if (fade) { |
michael@0 | 96 | this.placeholder = placeholder; |
michael@0 | 97 | animating = true; |
michael@0 | 98 | startTimeMillis = SystemClock.uptimeMillis(); |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | @Override public void draw(Canvas canvas) { |
michael@0 | 103 | if (!animating) { |
michael@0 | 104 | image.draw(canvas); |
michael@0 | 105 | } else { |
michael@0 | 106 | float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION; |
michael@0 | 107 | if (normalized >= 1f) { |
michael@0 | 108 | animating = false; |
michael@0 | 109 | placeholder = null; |
michael@0 | 110 | image.draw(canvas); |
michael@0 | 111 | } else { |
michael@0 | 112 | if (placeholder != null) { |
michael@0 | 113 | placeholder.draw(canvas); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | int partialAlpha = (int) (alpha * normalized); |
michael@0 | 117 | image.setAlpha(partialAlpha); |
michael@0 | 118 | image.draw(canvas); |
michael@0 | 119 | image.setAlpha(alpha); |
michael@0 | 120 | invalidateSelf(); |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | if (debugging) { |
michael@0 | 125 | drawDebugIndicator(canvas); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | @Override public int getIntrinsicWidth() { |
michael@0 | 130 | return image.getIntrinsicWidth(); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | @Override public int getIntrinsicHeight() { |
michael@0 | 134 | return image.getIntrinsicHeight(); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | @Override public void setAlpha(int alpha) { |
michael@0 | 138 | this.alpha = alpha; |
michael@0 | 139 | if (placeholder != null) { |
michael@0 | 140 | placeholder.setAlpha(alpha); |
michael@0 | 141 | } |
michael@0 | 142 | image.setAlpha(alpha); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | @Override public void setColorFilter(ColorFilter cf) { |
michael@0 | 146 | if (placeholder != null) { |
michael@0 | 147 | placeholder.setColorFilter(cf); |
michael@0 | 148 | } |
michael@0 | 149 | image.setColorFilter(cf); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | @Override public int getOpacity() { |
michael@0 | 153 | return image.getOpacity(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | @Override protected void onBoundsChange(Rect bounds) { |
michael@0 | 157 | super.onBoundsChange(bounds); |
michael@0 | 158 | |
michael@0 | 159 | image.setBounds(bounds); |
michael@0 | 160 | if (placeholder != null) { |
michael@0 | 161 | placeholder.setBounds(bounds); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | private void drawDebugIndicator(Canvas canvas) { |
michael@0 | 166 | DEBUG_PAINT.setColor(WHITE); |
michael@0 | 167 | Path path = getTrianglePath(new Point(0, 0), (int) (16 * density)); |
michael@0 | 168 | canvas.drawPath(path, DEBUG_PAINT); |
michael@0 | 169 | |
michael@0 | 170 | DEBUG_PAINT.setColor(loadedFrom.debugColor); |
michael@0 | 171 | path = getTrianglePath(new Point(0, 0), (int) (15 * density)); |
michael@0 | 172 | canvas.drawPath(path, DEBUG_PAINT); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | private static Path getTrianglePath(Point p1, int width) { |
michael@0 | 176 | Point p2 = new Point(p1.x + width, p1.y); |
michael@0 | 177 | Point p3 = new Point(p1.x, p1.y + width); |
michael@0 | 178 | |
michael@0 | 179 | Path path = new Path(); |
michael@0 | 180 | path.moveTo(p1.x, p1.y); |
michael@0 | 181 | path.lineTo(p2.x, p2.y); |
michael@0 | 182 | path.lineTo(p3.x, p3.y); |
michael@0 | 183 | |
michael@0 | 184 | return path; |
michael@0 | 185 | } |
michael@0 | 186 | } |