1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/PicassoDrawable.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,186 @@ 1.4 +/* 1.5 + * Copyright (C) 2013 Square, Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 +package com.squareup.picasso; 1.20 + 1.21 +import android.content.Context; 1.22 +import android.content.res.Resources; 1.23 +import android.graphics.Bitmap; 1.24 +import android.graphics.Canvas; 1.25 +import android.graphics.ColorFilter; 1.26 +import android.graphics.Paint; 1.27 +import android.graphics.Path; 1.28 +import android.graphics.Point; 1.29 +import android.graphics.Rect; 1.30 +import android.graphics.drawable.AnimationDrawable; 1.31 +import android.graphics.drawable.BitmapDrawable; 1.32 +import android.graphics.drawable.Drawable; 1.33 +import android.os.SystemClock; 1.34 +import android.widget.ImageView; 1.35 + 1.36 +import static android.graphics.Color.WHITE; 1.37 +import static com.squareup.picasso.Picasso.LoadedFrom.MEMORY; 1.38 + 1.39 +final class PicassoDrawable extends Drawable { 1.40 + // Only accessed from main thread. 1.41 + private static final Paint DEBUG_PAINT = new Paint(); 1.42 + 1.43 + private static final float FADE_DURATION = 200f; //ms 1.44 + 1.45 + /** 1.46 + * Create or update the drawable on the target {@link ImageView} to display the supplied bitmap 1.47 + * image. 1.48 + */ 1.49 + static void setBitmap(ImageView target, Context context, Bitmap bitmap, 1.50 + Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) { 1.51 + Drawable placeholder = target.getDrawable(); 1.52 + if (placeholder instanceof AnimationDrawable) { 1.53 + ((AnimationDrawable) placeholder).stop(); 1.54 + } 1.55 + PicassoDrawable drawable = 1.56 + new PicassoDrawable(context, placeholder, bitmap, loadedFrom, noFade, debugging); 1.57 + target.setImageDrawable(drawable); 1.58 + } 1.59 + 1.60 + /** 1.61 + * Create or update the drawable on the target {@link ImageView} to display the supplied 1.62 + * placeholder image. 1.63 + */ 1.64 + static void setPlaceholder(ImageView target, int placeholderResId, Drawable placeholderDrawable) { 1.65 + if (placeholderResId != 0) { 1.66 + target.setImageResource(placeholderResId); 1.67 + } else { 1.68 + target.setImageDrawable(placeholderDrawable); 1.69 + } 1.70 + if (target.getDrawable() instanceof AnimationDrawable) { 1.71 + ((AnimationDrawable) target.getDrawable()).start(); 1.72 + } 1.73 + } 1.74 + 1.75 + private final boolean debugging; 1.76 + private final float density; 1.77 + private final Picasso.LoadedFrom loadedFrom; 1.78 + final BitmapDrawable image; 1.79 + 1.80 + Drawable placeholder; 1.81 + 1.82 + long startTimeMillis; 1.83 + boolean animating; 1.84 + int alpha = 0xFF; 1.85 + 1.86 + PicassoDrawable(Context context, Drawable placeholder, Bitmap bitmap, 1.87 + Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) { 1.88 + Resources res = context.getResources(); 1.89 + 1.90 + this.debugging = debugging; 1.91 + this.density = res.getDisplayMetrics().density; 1.92 + 1.93 + this.loadedFrom = loadedFrom; 1.94 + 1.95 + this.image = new BitmapDrawable(res, bitmap); 1.96 + 1.97 + boolean fade = loadedFrom != MEMORY && !noFade; 1.98 + if (fade) { 1.99 + this.placeholder = placeholder; 1.100 + animating = true; 1.101 + startTimeMillis = SystemClock.uptimeMillis(); 1.102 + } 1.103 + } 1.104 + 1.105 + @Override public void draw(Canvas canvas) { 1.106 + if (!animating) { 1.107 + image.draw(canvas); 1.108 + } else { 1.109 + float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION; 1.110 + if (normalized >= 1f) { 1.111 + animating = false; 1.112 + placeholder = null; 1.113 + image.draw(canvas); 1.114 + } else { 1.115 + if (placeholder != null) { 1.116 + placeholder.draw(canvas); 1.117 + } 1.118 + 1.119 + int partialAlpha = (int) (alpha * normalized); 1.120 + image.setAlpha(partialAlpha); 1.121 + image.draw(canvas); 1.122 + image.setAlpha(alpha); 1.123 + invalidateSelf(); 1.124 + } 1.125 + } 1.126 + 1.127 + if (debugging) { 1.128 + drawDebugIndicator(canvas); 1.129 + } 1.130 + } 1.131 + 1.132 + @Override public int getIntrinsicWidth() { 1.133 + return image.getIntrinsicWidth(); 1.134 + } 1.135 + 1.136 + @Override public int getIntrinsicHeight() { 1.137 + return image.getIntrinsicHeight(); 1.138 + } 1.139 + 1.140 + @Override public void setAlpha(int alpha) { 1.141 + this.alpha = alpha; 1.142 + if (placeholder != null) { 1.143 + placeholder.setAlpha(alpha); 1.144 + } 1.145 + image.setAlpha(alpha); 1.146 + } 1.147 + 1.148 + @Override public void setColorFilter(ColorFilter cf) { 1.149 + if (placeholder != null) { 1.150 + placeholder.setColorFilter(cf); 1.151 + } 1.152 + image.setColorFilter(cf); 1.153 + } 1.154 + 1.155 + @Override public int getOpacity() { 1.156 + return image.getOpacity(); 1.157 + } 1.158 + 1.159 + @Override protected void onBoundsChange(Rect bounds) { 1.160 + super.onBoundsChange(bounds); 1.161 + 1.162 + image.setBounds(bounds); 1.163 + if (placeholder != null) { 1.164 + placeholder.setBounds(bounds); 1.165 + } 1.166 + } 1.167 + 1.168 + private void drawDebugIndicator(Canvas canvas) { 1.169 + DEBUG_PAINT.setColor(WHITE); 1.170 + Path path = getTrianglePath(new Point(0, 0), (int) (16 * density)); 1.171 + canvas.drawPath(path, DEBUG_PAINT); 1.172 + 1.173 + DEBUG_PAINT.setColor(loadedFrom.debugColor); 1.174 + path = getTrianglePath(new Point(0, 0), (int) (15 * density)); 1.175 + canvas.drawPath(path, DEBUG_PAINT); 1.176 + } 1.177 + 1.178 + private static Path getTrianglePath(Point p1, int width) { 1.179 + Point p2 = new Point(p1.x + width, p1.y); 1.180 + Point p3 = new Point(p1.x, p1.y + width); 1.181 + 1.182 + Path path = new Path(); 1.183 + path.moveTo(p1.x, p1.y); 1.184 + path.lineTo(p2.x, p2.y); 1.185 + path.lineTo(p3.x, p3.y); 1.186 + 1.187 + return path; 1.188 + } 1.189 +}