mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
8 years ago
15 changed files with 129 additions and 242 deletions
@ -0,0 +1,57 @@ |
|||||
|
package gr.thmmy.mthmmy.utils; |
||||
|
/* |
||||
|
* Copyright 2014 Julian Shen |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
import android.graphics.Bitmap; |
||||
|
import android.graphics.BitmapShader; |
||||
|
import android.graphics.Canvas; |
||||
|
import android.graphics.Paint; |
||||
|
|
||||
|
import com.squareup.picasso.Transformation; |
||||
|
|
||||
|
public class CircleTransform implements Transformation { |
||||
|
@Override |
||||
|
public Bitmap transform(Bitmap source) { |
||||
|
int size = Math.min(source.getWidth(), source.getHeight()); |
||||
|
|
||||
|
int x = (source.getWidth() - size) / 2; |
||||
|
int y = (source.getHeight() - size) / 2; |
||||
|
|
||||
|
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); |
||||
|
if (squaredBitmap != source) { |
||||
|
source.recycle(); |
||||
|
} |
||||
|
|
||||
|
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); |
||||
|
|
||||
|
Canvas canvas = new Canvas(bitmap); |
||||
|
Paint paint = new Paint(); |
||||
|
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); |
||||
|
paint.setShader(shader); |
||||
|
paint.setAntiAlias(true); |
||||
|
|
||||
|
float r = size / 2f; |
||||
|
canvas.drawCircle(r, r, r, paint); |
||||
|
|
||||
|
squaredBitmap.recycle(); |
||||
|
return bitmap; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String key() { |
||||
|
return "circle"; |
||||
|
} |
||||
|
} |
@ -1,72 +0,0 @@ |
|||||
package gr.thmmy.mthmmy.utils; |
|
||||
|
|
||||
import android.content.Context; |
|
||||
import android.graphics.Bitmap; |
|
||||
import android.graphics.Bitmap.Config; |
|
||||
import android.graphics.Canvas; |
|
||||
import android.graphics.Paint; |
|
||||
import android.graphics.PorterDuff.Mode; |
|
||||
import android.graphics.PorterDuffXfermode; |
|
||||
import android.graphics.Rect; |
|
||||
import android.graphics.RectF; |
|
||||
import android.graphics.drawable.BitmapDrawable; |
|
||||
import android.util.AttributeSet; |
|
||||
|
|
||||
import com.android.volley.toolbox.NetworkImageView; |
|
||||
|
|
||||
public class CircularNetworkImageView extends NetworkImageView { |
|
||||
private static final int THUMBNAIL_SIZE = 80; |
|
||||
private Context mContext; |
|
||||
|
|
||||
public CircularNetworkImageView(Context context) { |
|
||||
super(context); |
|
||||
mContext = context; |
|
||||
} |
|
||||
|
|
||||
public CircularNetworkImageView(Context context, AttributeSet attrs) { |
|
||||
this(context, attrs, 0); |
|
||||
mContext = context; |
|
||||
} |
|
||||
|
|
||||
public CircularNetworkImageView(Context context, AttributeSet attrs, |
|
||||
int defStyle) { |
|
||||
super(context, attrs, defStyle); |
|
||||
mContext = context; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void setImageBitmap(Bitmap bm) { |
|
||||
if (bm == null) return; |
|
||||
setImageDrawable(new BitmapDrawable(mContext.getResources(), |
|
||||
getCircularBitmap(bm))); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Creates a circular bitmap and uses whichever dimension is smaller to determine the width |
|
||||
* <br/>Also constrains the circle to the leftmost part of the image |
|
||||
*/ |
|
||||
private Bitmap getCircularBitmap(Bitmap bitmap) { |
|
||||
bitmap = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false); |
|
||||
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), |
|
||||
bitmap.getHeight(), Config.ARGB_8888); |
|
||||
Canvas canvas = new Canvas(output); |
|
||||
int size = bitmap.getWidth(); |
|
||||
if (bitmap.getWidth() > bitmap.getHeight()) |
|
||||
size = bitmap.getHeight(); |
|
||||
final int color = 0xff424242; |
|
||||
final Paint paint = new Paint(); |
|
||||
final Rect rect = new Rect(0, 0, size, size); |
|
||||
final RectF rectF = new RectF(rect); |
|
||||
final float roundPx = size / 2; |
|
||||
|
|
||||
paint.setAntiAlias(true); |
|
||||
canvas.drawARGB(0, 0, 0, 0); |
|
||||
paint.setColor(color); |
|
||||
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); |
|
||||
|
|
||||
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); |
|
||||
canvas.drawBitmap(bitmap, rect, rect, paint); |
|
||||
|
|
||||
return output; |
|
||||
} |
|
||||
} |
|
@ -1,66 +0,0 @@ |
|||||
package gr.thmmy.mthmmy.utils; |
|
||||
|
|
||||
import android.app.Application; |
|
||||
import android.text.TextUtils; |
|
||||
|
|
||||
import com.android.volley.Request; |
|
||||
import com.android.volley.RequestQueue; |
|
||||
import com.android.volley.toolbox.ImageLoader; |
|
||||
import com.android.volley.toolbox.Volley; |
|
||||
|
|
||||
public class ImageController extends Application { |
|
||||
|
|
||||
private static final String TAG = ImageController.class.getSimpleName(); |
|
||||
private static ImageController mInstance; |
|
||||
private RequestQueue mRequestQueue; |
|
||||
private ImageLoader mImageLoader; |
|
||||
|
|
||||
public static synchronized ImageController getInstance() { |
|
||||
return mInstance; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void onCreate() { |
|
||||
super.onCreate(); |
|
||||
mInstance = this; |
|
||||
} |
|
||||
|
|
||||
private RequestQueue getRequestQueue() { |
|
||||
if (mRequestQueue == null) { |
|
||||
mRequestQueue = Volley.newRequestQueue(getApplicationContext()); |
|
||||
} |
|
||||
|
|
||||
return mRequestQueue; |
|
||||
} |
|
||||
|
|
||||
public ImageLoader getImageLoader() { |
|
||||
getRequestQueue(); |
|
||||
if (mImageLoader == null) { |
|
||||
mImageLoader = new ImageLoader(this.mRequestQueue, |
|
||||
new LruBitmapCache()); |
|
||||
} |
|
||||
return this.mImageLoader; |
|
||||
} |
|
||||
|
|
||||
public <T> void addToRequestQueue(Request<T> req, String tag) { |
|
||||
// set the default tag if tag is empty
|
|
||||
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); |
|
||||
getRequestQueue().add(req); |
|
||||
} |
|
||||
|
|
||||
public <T> void addToRequestQueue(Request<T> req) { |
|
||||
req.setTag(TAG); |
|
||||
getRequestQueue().add(req); |
|
||||
} |
|
||||
|
|
||||
public void cancelPendingRequests() { |
|
||||
mRequestQueue.cancelAll(new RequestQueue.RequestFilter() { |
|
||||
@Override |
|
||||
public boolean apply(Request<?> request) { |
|
||||
return true; |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
|
@ -1,41 +0,0 @@ |
|||||
package gr.thmmy.mthmmy.utils; |
|
||||
|
|
||||
import android.graphics.Bitmap; |
|
||||
import android.support.v4.util.LruCache; |
|
||||
|
|
||||
import com.android.volley.toolbox.ImageLoader.ImageCache; |
|
||||
|
|
||||
class LruBitmapCache extends LruCache<String, Bitmap> implements |
|
||||
ImageCache { |
|
||||
private static final int CACHE_SIZE_DIVIDER = 8; |
|
||||
|
|
||||
LruBitmapCache() { |
|
||||
this(getDefaultLruCacheSize()); |
|
||||
} |
|
||||
|
|
||||
private LruBitmapCache(int sizeInKiloBytes) { |
|
||||
super(sizeInKiloBytes); |
|
||||
} |
|
||||
|
|
||||
private static int getDefaultLruCacheSize() { |
|
||||
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); |
|
||||
final int cacheSize = maxMemory / CACHE_SIZE_DIVIDER; |
|
||||
|
|
||||
return cacheSize; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int sizeOf(String key, Bitmap value) { |
|
||||
return value.getRowBytes() * value.getHeight() / 1024; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public Bitmap getBitmap(String url) { |
|
||||
return get(url); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void putBitmap(String url, Bitmap bitmap) { |
|
||||
put(url, bitmap); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue