Back to project page memBitmapTest.
The source code is released under:
GNU General Public License
If you think the Android project memBitmapTest listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.membmptest; /* www. ja v a 2 s .c o m*/ import java.lang.ref.WeakReference; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; public abstract class BaseMemoryActivity extends Activity { private ArrayList<WeakReference<Drawable>> weakDrawables; protected abstract Class<?> getNewClass(); protected abstract ArrayList<Integer> getImageResourceIds(); // Static final variables. public static final String EXTRA_SET_DRAWABLES_AFTER = "EXTRA_SET_DRAWABLES_AFTER"; private static final String EXTRA_INDEX = "INDEX"; // Object declarations. protected int currentActvityIndex = 0; protected boolean setDrawablesAfter = true; @Override public void onDetachedFromWindow() { unbindDrawables(getWindow().getDecorView()); super.onDetachedFromWindow(); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TestLog.recLifeCycle(this); Bundle b = getIntent().getExtras(); if (b != null) { currentActvityIndex = b.getInt(EXTRA_INDEX, currentActvityIndex); setDrawablesAfter = b.getBoolean(EXTRA_SET_DRAWABLES_AFTER, setDrawablesAfter); } if (setDrawablesAfter) { setContentView(R.layout.activity_main_without_drawables); } } @SuppressLint("NewApi") @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); TextView tvMainActivityCount = (TextView) findViewById(R.id.tvMainActivityCount); tvMainActivityCount.setText(getString(R.string.activtyIndexStr) + " : " + currentActvityIndex); Button btnSameActivity = (Button) findViewById(R.id.btnSameActivity); Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity); final CheckBox chkStartAfterFinish = (CheckBox) findViewById(R.id.chkStartAfterFinish); final CheckBox chkLoadResourceAfterCreate = (CheckBox) findViewById(R.id.chkLoadResourceAfterCreate); chkLoadResourceAfterCreate.setChecked(setDrawablesAfter); btnSameActivity.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { start(chkStartAfterFinish.isChecked(), false, chkLoadResourceAfterCreate.isChecked()); } }); btnNewActivity.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { start(chkStartAfterFinish.isChecked(), true, chkLoadResourceAfterCreate.isChecked()); } }); try { if (setDrawablesAfter) { boolean useResourcesClass = true; ArrayList<Integer> ids = getImageResourceIds(); if (!useResourcesClass) { ArrayList<Bitmap> bmps = new ArrayList<Bitmap>(); bmps.add(BitmapFactory.decodeResource(getResources(), ids.get(0))); bmps.add(BitmapFactory.decodeResource(getResources(), ids.get(1))); BitmapDrawable bmdrawable2 = new BitmapDrawable( getResources(), bmps.get(1)); BitmapDrawable bmdrawable1 = new BitmapDrawable( getResources(), bmps.get(0)); findViewById(R.id.llWithoutBackground2).setBackground( bmdrawable2); findViewById(R.id.llWithoutBackground1).setBackground( bmdrawable1); } else { weakDrawables = new ArrayList<WeakReference<Drawable>>(); Drawable dr1 = getResources().getDrawable(ids.get(0)); Drawable dr2 = getResources().getDrawable(ids.get(1)); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); // Drawable dr1 = // getResources().getDrawableForDensity(ids.get(0), // metrics.densityDpi); // Drawable dr2 = // getResources().getDrawableForDensity(ids.get(1), // metrics.densityDpi); // Drawable dr1 = // getResources().getDrawableForDensity(ids.get(0), // DisplayMetrics.DENSITY_LOW); // Drawable dr2 = // getResources().getDrawableForDensity(ids.get(1), // DisplayMetrics.DENSITY_LOW); weakDrawables.add(new WeakReference<Drawable>(dr1)); weakDrawables.add(new WeakReference<Drawable>(dr2)); findViewById(R.id.llWithoutBackground1).setBackground( weakDrawables.get(0).get()); findViewById(R.id.llWithoutBackground2).setBackground( weakDrawables.get(1).get()); } } } catch (Exception ex) { TestLog.exceptionLog(ex); } } @Override protected void onDestroy() { super.onDestroy(); TestLog.recLifeCycle(this); try { unbindDrawables(getWindow().getDecorView()); } catch (Exception ex) { TestLog.exceptionLog(ex); } if (weakDrawables != null) { weakDrawables.clear(); } } /** * * All {@link View} and {@link ViewGroup} backgrounds are removed.. * Neccessary for memory management.. Should be called when fragment is * removed.. * * @param view */ private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup && !(view instanceof AdapterView)) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } private void start(boolean callFinish, boolean startNewTypeClass, boolean loadAfterCreate) { Intent i; if (startNewTypeClass) { i = new Intent(this, getNewClass()); } else { i = new Intent(this, this.getClass()); } int newIndex = currentActvityIndex + 1; i.putExtra(EXTRA_INDEX, newIndex); i.putExtra(EXTRA_SET_DRAWABLES_AFTER, loadAfterCreate); try { if (callFinish) { finish(); } startActivity(i); } catch (Exception ex) { Toast.makeText(this, "Error on activity creation", Toast.LENGTH_SHORT).show(); } } }