rounded ImageView instead of Bitmap, crop-select

// FREEBIE
This commit is contained in:
Jake McGinty
2014-12-29 17:31:41 -08:00
parent a0599c1639
commit 62816ee51a
19 changed files with 81 additions and 154 deletions

View File

@@ -217,41 +217,6 @@ public class BitmapUtil {
return new Pair<>(options.outWidth, options.outHeight);
}
public static Bitmap getCircleCroppedBitmap(Bitmap bitmap) {
if (bitmap == null) return null;
final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
return getScaledCircleCroppedBitmap(bitmap, srcSize);
}
public static Bitmap getScaledCircleCroppedBitmap(Context context, MasterSecret masterSecret, Uri uri, int destSize)
throws IOException, BitmapDecodingException
{
Bitmap bitmap = createScaledBitmap(context, masterSecret, uri, destSize, destSize);
return getScaledCircleCroppedBitmap(bitmap, destSize);
}
public static Bitmap getScaledCircleCroppedBitmap(Bitmap bitmap, int destSize) {
if (bitmap == null) return null;
Bitmap output = Bitmap.createBitmap(destSize, destSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
final int srcX = (bitmap.getWidth() - srcSize) / 2;
final int srcY = (bitmap.getHeight() - srcSize) / 2;
final Rect srcRect = new Rect(srcX, srcY, srcX + srcSize, srcY + srcSize);
final Rect destRect = new Rect(0, 0, destSize, destSize);
final int color = 0xff424242;
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(destSize / 2, destSize / 2, destSize / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, srcRect, destRect, paint);
return output;
}
public static InputStream toCompressedJpeg(Bitmap bitmap) {
ByteArrayOutputStream thumbnailBytes = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 85, thumbnailBytes);

View File

@@ -25,6 +25,8 @@ import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;
import com.makeramen.RoundedDrawable;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
@@ -48,7 +50,7 @@ public class BitmapWorkerRunnable implements Runnable {
public final String number;
public BitmapWorkerRunnable(Context context, ImageView imageView, Bitmap defaultPhoto, String number, int size) {
this.imageViewReference = new WeakReference<ImageView>(imageView);
this.imageViewReference = new WeakReference<>(imageView);
this.context = context;
this.defaultPhoto = defaultPhoto;
this.size = size;
@@ -57,42 +59,36 @@ public class BitmapWorkerRunnable implements Runnable {
@Override
public void run() {
final Bitmap bitmap;
try {
final Recipient recipient = RecipientFactory.getRecipientsFromString(context, number, false).getPrimaryRecipient();
final Bitmap contactPhoto = recipient.getContactPhoto();
if (defaultPhoto == contactPhoto) {
return;
}
if (recipient.getContactPhoto() != null) {
final ImageView imageView = imageViewReference.get();
final TaggedFutureTask<?> bitmapWorkerTask = AsyncDrawable.getBitmapWorkerTask(imageView);
bitmap = BitmapUtil.getScaledCircleCroppedBitmap(contactPhoto, size);
if (bitmapWorkerTask.getTag().equals(number) && imageView != null) {
final BitmapDrawable drawable = new BitmapDrawable(context.getResources(), recipient.getContactPhoto());
imageView.post(new Runnable() {
@Override
public void run() {
imageView.setImageDrawable(drawable);
}
});
}
}
} catch (RecipientFormattingException rfe) {
Log.w(TAG, "Couldn't get recipient from string", rfe);
return;
}
if (bitmap != null) {
final ImageView imageView = imageViewReference.get();
final TaggedFutureTask<?> bitmapWorkerTask = AsyncDrawable.getBitmapWorkerTask(imageView);
if (bitmapWorkerTask.getTag().equals(number) && imageView != null) {
final BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
imageView.post(new Runnable() {
@Override
public void run() {
imageView.setImageDrawable(drawable);
}
});
}
}
}
public static class AsyncDrawable extends BitmapDrawable {
public static class AsyncDrawable extends RoundedDrawable {
private final WeakReference<TaggedFutureTask<?>> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap,
TaggedFutureTask<?> bitmapWorkerTask) {
super(res, bitmap);
public AsyncDrawable(Bitmap bitmap, TaggedFutureTask<?> bitmapWorkerTask) {
super(bitmap);
bitmapWorkerTaskReference =
new WeakReference<TaggedFutureTask<?>>(bitmapWorkerTask);
}
@@ -112,5 +108,4 @@ public class BitmapWorkerRunnable implements Runnable {
return null;
}
}
}