2016-04-16 14 views
1

Ich habe ein ImageView und ich möchte es 2 Sekunden nach dem Ausführen des Codes angezeigt werden. Ich habe diesen Code ausprobiert, aber leider hat es nichts getan.Lassen ImageView innerhalb von 2 Sekunden erscheinen

ImageView aniView = (ImageView) findViewById(R.id.imageView1); 
     float dest = 1; 
     if (aniView.getAlpha() <= 0) { 
      dest = 0; 
      System.out.println("F"); 
     } 
     ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView, 
       "alpha", dest); 
     animation3.setDuration(5000); 
     animation3.start(); 
+0

hilft Haben Sie versucht, 'setStartDelay (2000)'? Hier finden Sie weitere Informationen http://android--code.blogspot.com/2015/07/android-objectanimator-delay.html – GRiMe2D

+0

Warum nicht einfach eine Verzögerung einstellen? –

Antwort

1

Verwendung Es gibt keine Notwendigkeit für eine Handler ist, CountDownTimer oder eine andere direkte Thread-Manipulation.

Android Klassen wie ObjectAnimator oder ViewPropertyAnimator haben alle erforderlichen Methoden in ihrer API.

Mit ObjectAnimator:

private static void showImageViewObjectAnimator(@NonNull final ImageView imageView) { 
    imageView.setAlpha(0f); 
    final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f); 
    objectAnimator.setStartDelay(TimeUnit.SECONDS.toMillis(2)); 
    objectAnimator.setDuration(TimeUnit.SECONDS.toMillis(5)); 
    objectAnimator.start(); 
    } 

ViewPropertyAnimator Verwendung:

private static void showImageViewViewPropertyAnimator(@NonNull final ImageView imageView) { 
    imageView.setAlpha(0f); 
    imageView.animate() 
     .alpha(1f) 
     .setStartDelay(TimeUnit.SECONDS.toMillis(2)) 
     .setDuration(TimeUnit.SECONDS.toMillis(5)); 
    //no need to call start! 
    } 

Ich hoffe, dass es Ihnen

0

Wenn ich verstanden richtig, was wollen Sie dies ...

Erstellen Sie ein neues Layout-Datei und nennen Sie es spritzen (oder was auch immer Sie wollen) und setzen das Bild dort und dann diesen Code hinzu:

public void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.splash); 
//display the image during 2 secondes, 
new CountDownTimer(2000,1000){ 
    @Override 
    public void onTick(long millisUntilFinished){} 

    @Override 
    public void onFinish(){ 
      //set the new Content of your activity 
      YourActivity.this.setContentView(R.layout.main); 
    } 
}.start(); 
//rest of the code goes here... 
+0

Aber das Bild wird abrupt angezeigt, und ich möchte, dass es langsam erscheint. –

+0

AlphaAnimation anim = neue AlphaAnimation (1.0f, 0.0f); anim.setDuration (2000); yourImage.startAnimation (anim); – Chunrand

0

try

aniView.postDelayed(new Runnable() { 
     @Override 
     public void run() 
      { 
       // your code for animation. 
      } 
     }, 2000); //2000 for 2 seconds delay. 
+0

Wird dies abrupt angezeigt? –

+0

Mit dieser Lösung sind Sie in der Verantwortung, diese "Runable" ebenfalls zu löschen, beachten Sie dies – jakubbialkowski

Verwandte Themen