当我开始一项新活动时,为什么我的背景会消失?

时间:2013-12-02 09:01:54

标签: java android android-layout android-intent

早上好,我因为我的应用程序使用大量图像而遇到安卓内存出现内存错误的问题。所以我在网上找到了一个代码,它创建了一个类,它将图像存储为每个活动的变量,然后将最后一个活动中存储的图像替换为新活动的图像。被替换的图像是每个活动的背景,我遇到的问题是当我点击按钮开始新活动时,最后一个活动背景消失,就像最后一个活动屏幕消失前2秒,新活动开始,所以它看起来很糟糕而且对用户来说并不顺利。我希望背景和活动在新活动开始之前同时消失,因此它看起来很平滑。以下是更改背景的类的代码。

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);
            }


}

这是第一个活动的代码

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);

}

    }

,这是第二个活动的代码

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);



}

}

因此,当我点击按钮开始新活动时,有人可以帮助我改变我的代码,以便我的主要活动背景不会在我的主要活动内容之前消失,因此同时使背景和活动内容在同一时间消失下一个活动开始了吗?

错误

12-02 11:13:57.588:E / AndroidRuntime(1861):致命异常:主要 12-02 11:13:57.588:E / AndroidRuntime(1861):java.lang.RuntimeException:Canvas:尝试使用循环位图android.graphics.Bitmap@4063cd38 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.graphics.Canvas.throwIfRecycled(Canvas.java:1012) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.graphics.Canvas.drawBitmap(Canvas.java:1116) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:335) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.View.draw(View.java:9264) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.drawChild(ViewGroup.java:2584) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.drawChild(ViewGroup.java:2582) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.drawChild(ViewGroup.java:2582) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.View.draw(View.java:9282) 12-02 11:13:57.588:E / AndroidRuntime(1861):在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):在android.view.ViewRoot.draw(ViewRoot.java:1666) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewRoot.performTraversals(ViewRoot.java:1381) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.view.ViewRoot.handleMessage(ViewRoot.java:2003) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.os.Handler.dispatchMessage(Handler.java:99) 12-02 11:13:57.588:E / AndroidRuntime(1861):在android.os.Looper.loop(Looper.java:132) 12-02 11:13:57.588:E / AndroidRuntime(1861):在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)

2 个答案:

答案 0 :(得分:1)

首先:我不建议使用背景图像,以提高性能并支持无穷无尽的屏幕分辨率。

第二:您可以将每个活动根布局容器的背景设置为您的图像,如下所示:

android:background="@drawable/your_bg"

有时如果背景图像分辨率太高,系统不会渲染它。 所以你需要使用你的图像的多个版本来支持不同的屏幕密度或使用9补丁来扩展不同的分辨率。

答案 1 :(得分:0)

请尝试以下操作:

  1. 只在onCreate()中使用以下代码,将其从onResume中删除,因为这将在onCreate之后调用,并将导致卸载和加载。

    layout =(RelativeLayout)findViewById(layoutid);       app.setBackground(layout,bgid);

  2. 不是在卸载方法中将drawable设置为null,然后在加载中加载新图像,而是将两个步骤结合起来,即不将background设置为null直接替换它将是新的...并且当然设置原始位图之前的活动为null并且还调用了回收

  3. 我希望这些步骤有帮助