2011-07-16 46 views
4

ボタンをクリックしたときに自分のアクティビティのビューをフェードアウトさせたい。アニメーションコードをボタンのOnClickListener()内に配置しますが、フェードアウトは起こっていません。どのようにすれば、ボタンをクリックしたときに、現在のアクティビティのビューをフェードアウトすることができますか?あらかじめありがとう.....現在のアクティビティの表示をフェードアウト

私のアプリケーションでは、アクティビティの開始時に、そのアクティビティのビューがフェードインし、アクティビティが終了するとビューがフェードアウトし、次に新しいアクティビティのビューがフェードインします以下、イムは、私のコードを与え、そこに問題を把握する助けを必要としてください.....

public class First extends Activity { 

Animation slide; 
View view; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    view = (View) findViewById(android.R.id.content); 

    slide = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
    view.startAnimation(slide); 

    Button btn = (Button) findViewById(R.id.retour); 
    btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Intent intent = new Intent(v.getContext(),Second.class); 
       slide = AnimationUtils.loadAnimation(First.this, R.anim.fade_out); 
       view.startAnimation(slide); 
       Test.group.replaceContentView("Second", intent, 0); 

      } 
    }); 

} 

} 
+0

(該当する場合)言語とフレームワークを指定してください。 – titaniumdecoy

+0

私はこのフェードアウトアニメーションを使用しなければならないアンドロイドアプリケーションを開発しています。 EclipseをIDEとして使用しています。 – Junaid

+0

あなたはfadeout animファイルを持っていますか? – Nikhil

答えて

8

をあなたのonCreate()メソッドでは、このようなものを追加します。

final Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      final View l = findViewById(R.id.main); 

      Animation a = AnimationUtils.loadAnimation(
        YourActivity.this, android.R.anim.fade_out); 
      a.setDuration(200); 
      a.setAnimationListener(new AnimationListener() { 

       public void onAnimationEnd(Animation animation) { 
         // Do what ever you need, if not remove it. 
       } 

       public void onAnimationRepeat(Animation animation) { 
         // Do what ever you need, if not remove it. 
       } 

       public void onAnimationStart(Animation animation) { 
         // Do what ever you need, if not remove it. 
       } 

      }); 
      l.startAnimation(a); 
     } 
    }); 

あなたのレイアウトxmlは、ID = "@ + id/main"のビューで始まり、i D = "@ + ID /ボタン"

例:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:id="@+id/main" 
    android:drawingCacheQuality="high" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
..... 

    <Button 
     android:id="@+id/button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
        android:text="Fade out"> 
    </Button> 
+0

ありがとう、vsm ....それは今働いています.....あなたの努力のおかげで....... !!! :) – Junaid

1

あなたは、コードの下にしてみてくださいことができます。

Button btn = (Button) findViewById(R.id.retour); 
    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(v.getContext(),Second.class); 
      startActivity(intent); 
      overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); 
     } 
    }); 

が役に立ちます。

+0

あなたの提案のおかげでニック、私は本当にありがたい.... .... – Junaid

関連する問題