2017-06-13 63 views
-1

我是一名混合應用程序開發人員,也是android的新手。我正在嘗試爲我的混合應用修復Android相關問題。Android:如何動態創建一個裏面帶有imagview的對話框容器

我對我的活動有以下兩種方法:onStart和onPause。

當應用程序啓動時,我需要它像平常一樣開始。當應用程序暫停時,我需要顯示圖像(我的公司Logo)。如果我使用setContentView(R.layout.activity_base)設置它,則應用程序再次啓動時會顯示圖像。有沒有辦法動態創建圖像,並在應用程序暫停時顯示它?另外,如何在啓動後刪除圖像?由於我在Android上沒有動手,所以我不知道該如何去做。 我想我需要創建某種對話框並在其中設置圖像視圖。 此外,我不確定,如何刪除此圖像,當應用程序再次啓動。

我的代碼片段:

public void onStart() { 
    Toast.makeText(getApplicationContext(), "App is in foreground", Toast.LENGTH_LONG).show(); 
} 

public void onPause() { 
    Toast.makeText(getApplicationContext(), "App is in background", Toast.LENGTH_LONG).show(); 
    setContentView(R.layout.activity_base); 
} 

謝謝!

+0

的onPause當你的應用程序進入後臺調用,所以你有什麼要做的心不是真的有可能。你爲什麼需要這個? –

+0

當我的應用進入後臺時,我不希望它顯示當前的屏幕。它應該顯示徽標。當應用程序恢復時,標誌會顯示幾秒鐘,之後應用程序將啓動。一種隱私的東西 –

+0

我認爲你正在尋找閃屏,不是嗎? [如何製作閃屏](http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/) –

回答

0

onResume被稱爲從cme到屏幕的最小化。 試試這個

在此處輸入代碼

@Override 
    protected void onResume() { 
     super.onResume(); 
     setContentView(R.layout.activity_main); 
    } 
+0

我需要在應用程序處於後臺時顯示圖像。不在恢復時。 –

+0

背景圖像可以顯示他們使用onpause方法顯示圖像。 –

+0

這個onresume被用來刪除圖像,然後使用這種方法。 –

0
This code is im run it. i think u need this. 

inside ur activity 


ImageView img_view; 
    TextView txt_show; 
    int images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_player); 

     img_view = (ImageView) findViewById(R.id.img_show); 
     txt_show = (TextView) findViewById(R.id.txt_my); 
    } 

    private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) { 
     int fadeInDuration = 1000; 
     int timeBetween = 5000; 
     int fadeOutDuration = 1000; 

     imageView.setVisibility(View.INVISIBLE); 
     imageView.setImageResource(images[imageIndex]); 

     Animation fadeIn = new AlphaAnimation(0, 1); 
     fadeIn.setInterpolator(new DecelerateInterpolator()); 
     fadeIn.setDuration(fadeInDuration); 

     Animation fadeOut = new AlphaAnimation(1, 0); 
     fadeOut.setInterpolator(new AccelerateInterpolator()); 
     fadeOut.setStartOffset(fadeInDuration + timeBetween); 
     fadeOut.setDuration(fadeOutDuration); 

     AnimationSet animation = new AnimationSet(false); 
     animation.addAnimation(fadeIn); 
     animation.addAnimation(fadeOut); 
     animation.setRepeatCount(1); 
     imageView.setAnimation(animation); 

     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      public void onAnimationEnd(Animation animation) { 
       if (images.length - 1 > imageIndex) { 
        animate(imageView, images, imageIndex + 1, forever); 
       } else { 
        if (forever) { 
         animate(imageView, images, 0, forever); 
        } 
       } 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 

     }); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     txt_show.setVisibility(View.GONE); 
     img_view.setVisibility(View.VISIBLE); 
     animate(img_view, images, 0, false); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     img_view.setVisibility(View.GONE); 
     txt_show.setVisibility(View.VISIBLE); 
    } 

inside ur xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <TextView 
     android:id="@+id/txt_my" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="buvan" 
     android:textColor="@color/colorPrimary" /> 


    <ImageView 
     android:id="@+id/img_show" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


</LinearLayout> 
相關問題