2011-07-16 103 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

我正在开发一个android应用程序,我必须使用这个淡出动画。我使用Eclipse作为IDE。 – Junaid

+0

你有淡出的动画文件吗? – 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

相关问题