reduce memory footprint of qr code in share tab

This commit is contained in:
Vincent Breitmoser 2014-10-09 15:57:44 +02:00
parent f6d0347c79
commit a54a0c34fe
2 changed files with 16 additions and 17 deletions

View File

@ -21,11 +21,6 @@ import android.annotation.TargetApi;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
@ -37,6 +32,7 @@ import android.support.v4.content.Loader;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -404,21 +400,25 @@ public class ViewKeyShareFragment extends LoaderFragment implements
new AsyncTask<Void, Void, Bitmap>() { new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... unused) { protected Bitmap doInBackground(Void... unused) {
String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
return QrCodeUtils.getQRCodeBitmap(qrCodeContent, QR_CODE_SIZE); // render with minimal size
return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0);
} }
protected void onPostExecute(Bitmap qrCode) { protected void onPostExecute(Bitmap qrCode) {
// only change view, if fragment is attached to activity // only change view, if fragment is attached to activity
if (ViewKeyShareFragment.this.isAdded()) { if (ViewKeyShareFragment.this.isAdded()) {
// Transition drawable with a transparent drawable and the final bitmap
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
new ColorDrawable(Color.TRANSPARENT),
new BitmapDrawable(getResources(), qrCode)
});
mFingerprintQrCode.setImageDrawable(td); // scale the image up to our actual size. we do this in code rather
td.startTransition(200); // than let the ImageView do this because we don't require filtering.
Bitmap scaled = Bitmap.createScaledBitmap(qrCode,
mFingerprintQrCode.getHeight(), mFingerprintQrCode.getHeight(),
false);
mFingerprintQrCode.setImageBitmap(scaled);
// simple fade-in animation
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(200);
mFingerprintQrCode.startAnimation(anim);
} }
} }
}; };

View File

@ -37,7 +37,6 @@ import java.util.Hashtable;
* Copied from Bitcoin Wallet * Copied from Bitcoin Wallet
*/ */
public class QrCodeUtils { public class QrCodeUtils {
public static final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
/** /**
* Generate Bitmap with QR Code based on input. * Generate Bitmap with QR Code based on input.
@ -50,7 +49,7 @@ public class QrCodeUtils {
try { try {
final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, final BitMatrix result = new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, size,
size, hints); size, hints);
final int width = result.getWidth(); final int width = result.getWidth();
@ -60,7 +59,7 @@ public class QrCodeUtils {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
final int offset = y * width; final int offset = y * width;
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT; pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
} }
} }