[英]Why doesn't my SharedPreferences retrieve the right data when I start a new Activity?
[英]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色塊來縮放不同的分辨率。
請嘗試以下操作:
僅在onCreate()中包含以下代碼,將其從onResume中刪除,因為它將在onCreate之后被調用,並導致卸載和加載。
布局=(RelativeLayout)findViewById(layoutid); app.setBackground(layout,bgid);
而不是在卸載方法中將drawable設置為null,然后在加載時加載新圖像,將這兩個步驟結合起來,即,不是將background設置為null而是直接將其替換為new,並且將上一個活動的原始位圖設置為null,然后也叫回收
希望此步驟對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.