Good morning guys I was having problems with Androids out of memory error because my app uses a lot of images. So i found code on the web that creates a class that stores the image as a variable for each activity and then replaces the image stored in the last activity with the image of the new activity. The image being replaced is the background of each activity and the problem I am having is as i click the button to start the new activity the last activities background disappears like 2 seconds before the last activity screen disappears and the new activity starts so it looks terrible and not smooth to the user. I want the background and the activity to disappear at the same time before the new activity starts so it looks smooth. Here is the code for the class that changes the background.
public class MyApp extends Application {
private RelativeLayout bgimg; // layout of the activity
private Bitmap background; // background in the Bitmap format
private BitmapDrawable bg; // background in the Drawable format
public void loadBackground(int id) {
background = BitmapFactory.decodeStream(getResources().openRawResource(id));
bg = new BitmapDrawable(background);
bgimg.setBackgroundDrawable(bg);
}
public void unloadBackground() {
if (bgimg != null)
bgimg.setBackgroundDrawable(null);
if (bg!= null) {
background.recycle();
}
bg = null;
}
public void setBackground(RelativeLayout i, int sourceid) {
unloadBackground();
bgimg = i;
loadBackground(sourceid);
}
}
here is the code for the first activity
public class MainActivity extends Activity implements OnClickListener {
private MyApp app;
private int bgid = R.drawable.main; // id of the background drawable
private int layoutid = R.id.mainmain; // id of the activity layout
private RelativeLayout layout; // the layout of the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton=(Button)findViewById(R.id.button1);
startButton.setOnClickListener(this);
app = (MyApp)getApplication();
layout = (RelativeLayout) findViewById(R.id.mainmain);
app.setBackground(layout, bgid); // free last background, and store new one
}
@Override
protected void onResume() {
super.onResume();
layout = (RelativeLayout) findViewById(layoutid);
app.setBackground(layout, bgid);
}
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent myintent = new Intent(this,Second.class);
startActivity(myintent);
}
}
and here is the code for the second activity
public class Second extends Activity implements OnClickListener {
private MyApp app;
private int bgid = R.drawable.two; // id of the background drawable
private int layoutid = R.id.seconds; // id of the activity layout
private RelativeLayout layout; // the layout of the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button startButton=(Button)findViewById(R.id.button2);
startButton.setOnClickListener(this);
app = (MyApp)getApplication();
layout = (RelativeLayout) findViewById(R.id.seconds);
app.setBackground(layout, bgid); // free last background, and store new one
}
@Override
protected void onResume() {
super.onResume();
layout = (RelativeLayout) findViewById(layoutid);
app.setBackground(layout, bgid);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent(this,Third.class);
startActivity(myintent);
}
}
So can someone help me alter my code so that my main activities background does not disappear before my main activities content when I click the button to start a new activity, so making both background and activity content disappear at the same time before the next activity starts?
errors
12-02 11:13:57.588: E/AndroidRuntime(1861): FATAL EXCEPTION: main 12-02 11:13:57.588: E/AndroidRuntime(1861): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4063cd38 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1012) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.graphics.Canvas.drawBitmap(Canvas.java:1116) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:335) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.View.draw(View.java:9264) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.drawChild(ViewGroup.java:2582) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.disp atchDraw(ViewGroup.java:2189) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.drawChild(ViewGroup.java:2582) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.View.draw(View.java:9282) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.widget.FrameLayout.draw(FrameLayout.java:419) 12-02 11:13:57.588: E/AndroidRuntime(1861): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1924) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewRoot.draw(ViewRoot.java:1666) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewRoot.performTraversals(ViewRoot.java:1381) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.os.Handler.dispatchMessage(Handler.java:99) 12-02 11:13:57.588: E/AndroidRuntime(1861): a t android.os.Looper.loop(Looper.java:132) 12-02 11:13:57.588: E/AndroidRuntime(1861): at android.app.ActivityThread.main(ActivityThread.java:4025) 12-02 11:13:57.588: E/AndroidRuntime(1861): at java.lang.reflect.Method.invokeNative(Native Method) 12-02 11:13:57.588: E/AndroidRuntime(1861): at java.lang.reflect.Method.invoke(Method.java:491) 12-02 11:13:57.588: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 12-02 11:13:57.588: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-02 11:13:57.588: E/AndroidRuntime(1861): at dalvik.system.NativeStart.main(Native Method)
First: I don't recommend using background images, for the sake of performance and supporting the endless screen resolutions.
Second: you can just set the background of each activity root layout container to your image like this:
android:background="@drawable/your_bg"
sometimes if the background image resolution is too high, the system does not render it. so you'll need to use multiple versions of your image to support the different screen densities or use a 9-patch to scale with different resolutions.
Try following:
Just have following code in onCreate() only, remove it from onResume as this will be called after onCreate and will lead to unloading and loading.
layout = (RelativeLayout) findViewById(layoutid); app.setBackground(layout, bgid);
Instead of set the drawable to null in unloading method and then loading new image in loading combine the two steps, ie instead of setting background to null directly replace it will new one...and ofcourse set the original bitmap of previous activity to null and also call the recycle
I hope this steps help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.