繁体   English   中英

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

[英]why does my background disappear when i start a new activity?

早上好,我遇到了Android内存不足错误的问题,因为我的应用使用了大量图像。 因此,我在网上找到了创建一个类的代码,该类将图像存储为每个活动的变量,然后用新活动的图像替换上一个活动中存储的图像。 替换的图像是每个活动的背景,而我遇到的问题是,当我单击按钮以开始新活动时,最后一个活动背景消失了,例如在最后一个活动屏幕消失之前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:画布:尝试使用回收的位图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):位于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):at 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.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):位于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):at 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):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(本机方法)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):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)12-02 11:13:57.588:E / AndroidRuntime(1861):位于达尔维克。 system.NativeStart.main(本机方法)

第一:为了提高性能并支持无尽的屏幕分辨率,我不建议使用背景图像。

第二:您可以像这样将每个活动根布局容器的背景设置为图像:

android:background="@drawable/your_bg"

有时,如果背景图像分辨率太高,系统将不会渲染它。 因此您需要使用图像的多个版本来支持不同的屏幕密度,或者使用9色块来缩放不同的分辨率。

请尝试以下操作:

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

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

  2. 而不是在卸载方法中将drawable设置为null,然后在加载时加载新图像,将这两个步骤结合起来,即,不是将background设置为null而是直接将其替换为new,并且将上一个活动的原始位图设置为null,然后也叫回收

希望此步骤对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM