java - 为什么当我开始一项新 Activity 时我的背景消失了?

标签 java android android-layout android-intent

早上好,我遇到了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):位于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(本机方法)
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):在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):在dalvik.system.NativeStart.main(本机方法)

最佳答案

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

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

android:background="@drawable/your_bg"


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

关于java - 为什么当我开始一项新 Activity 时我的背景消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20324348/

相关文章:

java - 情境使用 : Run tasks in ForkJoinPool vs. new Thread

java - com.datastax.driver.core.exceptions.InvalidQueryException 使用 Datastax Java 驱动程序

java - AndroidX 任务 :@react-native-community_viewpager:compileDebugJavaWithJavac FAILED 之后的 React-Native

android - 如何在 Parse.com 中验证 deviceToken 的 GCM 发件人 ID

android - LinearLayout 不使用 ScrollView 滚动

java - 来自 Android Studio 的新 Activity 显示错误

android - 在 View 寻呼机中查看上一个 View 和下一个 View 之间的重叠

java - 如何仅在特定配置文件上启用 spring 基本身份验证?

java - 多个集合的并行迭代

android - WebView.enableSlowWholeDocumentDraw 是否存在于当前 Activity 之外?