简体   繁体   English

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

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

Good morning guys I was having problems with Androids out of memory error because my app uses a lot of images. 早上好,我遇到了Android内存不足错误的问题,因为我的应用使用了大量图像。 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. 替换的图像是每个活动的背景,而我遇到的问题是,当我单击按钮以开始新活动时,最后一个活动背景消失了,例如在最后一个活动屏幕消失之前2秒钟,新活动开始了,所以看起来很糟糕对用户而言并不顺畅。 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 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): 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 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(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) 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(本机方法)

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. 因此您需要使用图像的多个版本来支持不同的屏幕密度,或者使用9色块来缩放不同的分辨率。

Try following: 请尝试以下操作:

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

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

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

I hope this steps help 希望此步骤对您有所帮助

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

相关问题 为什么启动新的Activity时,我的SharedPreferences无法检索正确的数据? - Why doesn't my SharedPreferences retrieve the right data when I start a new Activity? 为什么开始新活动时我的android应用程序崩溃? - Why does my android application crash when starting a new activity? 为什么我不能在这里开始一个新的活动? - Why can I not start a new Activity here? 为什么当我创建一个新活动时,“setContentView”无法识别我的布局? - Why when I create a new activity, “setContentView” not recognize my layout? 当向其添加actionListener时,为什么我的按钮从我的borderlayout中消失了? - Why does my button disappear from my borderlayout when I add an actionListener to it? 新活动未启动 - New Activity does not start 尝试获取电话号码时,为什么我的活动崩溃了? - Why does my activity crash when I try to get a number? 不使用按钮时,为什么新活动不会从此 OnClick 方法启动? - Why does a new activity not start from this OnClick method when not using a button? 在Java中隐藏JButton边框时,为什么我的文本消失了? - Why does my text disappear when I hide JButton borders in Java? 为什么我开始执行此操作时画面会冻结[DOWNLOADING] - Why does my frame freeze when I start this action[DOWNLOADING]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM