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

Java 当我开始一项新的活动时,为什么我的背景消失了?,java,android,android-layout,android-intent,Java,Android,Android Layout,Android Intent,早上好,伙计们,我遇到了Androids内存不足错误的问题,因为我的应用程序使用了很多图像。因此,我在web上找到了创建一个类的代码,该类将图像存储为每个活动的变量,然后用新活动的图像替换上一个活动中存储的图像。被替换的图像是每个活动的背景,我遇到的问题是,当我单击按钮开始新活动时,最后一个活动背景消失,大约在最后一个活动屏幕消失前2秒,新活动开始,因此对用户来说,它看起来很糟糕且不平滑。我希望背景和活动在新活动开始之前同时消失,使其看起来平滑。下面是更改背景的类的代码 public class

早上好,伙计们,我遇到了Androids内存不足错误的问题,因为我的应用程序使用了很多图像。因此,我在web上找到了创建一个类的代码,该类将图像存储为每个活动的变量,然后用新活动的图像替换上一个活动中存储的图像。被替换的图像是每个活动的背景,我遇到的问题是,当我单击按钮开始新活动时,最后一个活动背景消失,大约在最后一个活动屏幕消失前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):致命异常:main 12-02 11:13:57.588:E/AndroidRuntime(1861):java.lang.RuntimeException:画布:尝试使用回收的位图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):位于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):位于java.lang.reflect.Method.Invokenactive(本机方法) 12-02 11:13:57.588:E/AndroidRuntime(1861):位于java.lang.reflect.Method.invoke(Method.java:491) 12-02 11:13:57.588:E/AndroidRuntime(1861):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 12-02 11:13:57.588:E/AndroidRuntime(1861):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-02 11:13:57.588:E/AndroidRuntime(1861):在dalvik.system.NativeStart.main(本机方法)

尝试以下内容:

  • 只需在onCreate()中包含以下代码,将其从onResume中删除,因为这将在onCreate之后调用,并将导致卸载和加载

    布局=(RelativeLayout)findViewById(layoutid); 应用背景(布局,bgid)

  • 而不是在卸载方法中将drawable设置为null,然后在加载中加载新图像,而是将这两个步骤结合起来,即不是将背景设置为null,而是直接将其替换为新的…当然,还可以将以前活动的原始位图设置为null,并调用循环

  • 我希望这些步骤有帮助

    请尝试以下步骤:

  • 只需在onCreate()中包含以下代码,将其从onResume中删除,因为这将在onCreate之后调用,并将导致卸载和加载

    布局=(RelativeLayout)findViewById(layoutid); 应用背景(布局,bgid)

  • 而不是在卸载方法中将drawable设置为null,然后在加载中加载新图像,而是将这两个步骤结合起来,即不是将背景设置为null,而是直接将其替换为新的…当然,还可以将以前活动的原始位图设置为null,并调用循环


  • 首先,我希望这些步骤能有所帮助:为了性能和支持无休止的屏幕分辨率,我不建议使用背景图像

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

    android:background="@drawable/your_bg"
    
    有时,如果背景图像分辨率太高,系统不会渲染它。
    因此,您需要使用多个版本的图像来支持不同的屏幕密度,或者使用9-patch以不同的分辨率进行缩放。

    首先:为了性能和支持无休止的屏幕分辨率,我不建议使用背景图像

    第二:您只需将每个活动根布局容器的背景设置为图像l