Skip to content

Commit

Permalink
Toggles: resize bitmap in UserToggle
Browse files Browse the repository at this point in the history
This will resize the bitmap of the user to a reasonable size.

Before the bitmap would use almost 1mb in memory on my phone and
after the change it is now at 250kb.

Change-Id: I6b06cc97b1c94edeb307777a9479e3abc119777e
Signed-off-by: Dirk Rettschlag <dirk.rettschlag@gmail.com>
  • Loading branch information
MarcLandis authored and Gerrit Code Review committed May 14, 2013
1 parent 1dbb813 commit 9181001
Showing 1 changed file with 11 additions and 0 deletions.
Expand Up @@ -19,7 +19,9 @@
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Profile;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.WindowManager;
import android.view.View;
import android.view.WindowManagerGlobal;
import android.widget.ImageView;
Expand All @@ -33,6 +35,7 @@ public class UserToggle extends BaseToggle {
private AsyncTask<Void, Void, Pair<String, Drawable>> mUserInfoTask;

private static Drawable sAvatarDrawable = null;
private static int sAvatarBaseSize = 125;

@Override
public void init(Context c, int style) {
Expand Down Expand Up @@ -105,6 +108,14 @@ protected Pair<String, Drawable> doInBackground(Void... params) {
Drawable avatar = null;
Bitmap rawAvatar = um.getUserIcon(userId);
if (rawAvatar != null) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
int desiredSize = (int) (sAvatarBaseSize * dm.density);
int width = rawAvatar.getWidth();
if (width > desiredSize) {
rawAvatar = Bitmap.createScaledBitmap(rawAvatar, desiredSize, desiredSize, false);
}
avatar = new BitmapDrawable(mContext.getResources(), rawAvatar);
} else {
avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
Expand Down

0 comments on commit 9181001

Please sign in to comment.