2013-12-03 2 views
1

У меня есть два вида деятельности: FirstActivity и SecondActivityAndroid активность анимация

Я хочу анимацию щелчка кнопки в этой деятельности:

Я использую следующие файлы:

FirstActivity.java

package com.example.vanimation; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class FirstActivity extends Activity 
    { 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_first); 
    ((Button)findViewById(R.id.NextButton)).setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      //startActivity(new Intent(FirstActivity.this, SecondActivity.class)); 
      Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
      startActivityForResult(intent, 0); 
      overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left); 
     } 
    }); 
} 
} 

SecondActivity.java

package com.example.vanimation; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.KeyEvent; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class SecondActivity extends Activity 
    { 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 
    ((Button)findViewById(R.id.BackButton)).setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      goBack(); 
     } 
    }); 
} 

private void goBack() 
{ 
    finish(); 
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); 
} 

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) 
{ 
    if(keyCode == KeyEvent.KEYCODE_BACK) 
    { 
     goBack(); 
     return true; 
    } 
    return super.onKeyUp(keyCode, event); 
} 
} 

activity_first.xml

<?xml version="1.0"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" android:gravity="center" 
android:background="#ccc"> 
<TextView android:text="Page 1" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<Button android:layout_height="wrap_content" android:text="Next" 
    android:layout_width="wrap_content" android:id="@+id/NextButton" /> 
</LinearLayout> 

activity_second.xml

<?xml version="1.0"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" android:gravity="center" 
android:background="#aaa"> 
<TextView android:text="Page 2" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<Button android:layout_height="wrap_content" android:text="Back" 
    android:layout_width="wrap_content" android:id="@+id/BackButton" /> 

Anim/slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="@android:integer/config_longAnimTime" 
android:fromXDelta="100%p" 
android:toXDelta="0%p" /> 

Anim/slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="@android:integer/config_longAnimTime" 
android:fromXDelta="0" 
android:toXDelta="-100%p" /> 

manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.vanimation" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.vanimation.FirstActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.example.vanimation.SecondActivity" 
     android:label="@string/title_activity_second" > 
    </activity> 
</application> 

Но все-таки анимация не работает. При нажатии кнопки действия изменяются без анимации.

Любая помощь для получения анимации для нее будет полезна для меня.

ответ

0

Используйте ниже код для анимации эффекта

public class CrossfadeActivity extends Activity implements AnimationListener { 

TextView txtMessage1, txtMessage2; 
Button btnStart; 

// Animation 
Animation animFadeIn, animFadeOut; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_crossfade); 

    txtMessage1 = (TextView) findViewById(R.id.txtMessage1); 
    txtMessage2 = (TextView) findViewById(R.id.txtMessage2); 
    btnStart = (Button) findViewById(R.id.btnStart); 

    // load animations 
    animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.fade_in); 
    animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.fade_out); 

    // set animation listeners 
    animFadeIn.setAnimationListener(this); 
    animFadeOut.setAnimationListener(this); 

    // button click event 
    btnStart.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // make fade in element visible 
      txtMessage2.setVisibility(View.VISIBLE); 
      // start fade in animation 
      txtMessage2.startAnimation(animFadeIn); 

      // start fade out animation 
      txtMessage1.startAnimation(animFadeOut); 
     } 
    }); 

} 

@Override 
public void onAnimationEnd(Animation animation) { 
    // Take any action after completing the animation 

    // if animation is fade out hide them after completing animation 
    if (animation == animFadeOut) { 

     // hide faded out element 
     txtMessage1.setVisibility(View.GONE); 
    } 

    if(animation == animFadeIn){ 
     // do something after fade in completed 

     // set visibility of fade in element 
     txtMessage2.setVisibility(View.VISIBLE); 
    } 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAnimationStart(Animation animation) { 
    // TODO Auto-generated method stub 

} 
} 

Вместо TextView или все, что вы можете использовать следующий код активности внутри него. и вместо fadein.xml и fadeout.xml вы можете использовать свой файл slide_in_left и другой анимационный XML-файл.

+0

Я использовал следующий код для деятельности для анимации активности: Intent intent = new Intent (FirstActivity.this, SecondActivity.class); startActivityForResult (цель, 0); overridePendingTransition (R.anim.slide_in_left, R.anim.slide_out_left); Но это не сработало. – dipendra

0

Проверить это, если вы ищете сверху вниз и снизу вверх анимации:

Android Animation Example

Вы можете изменить значение fromDelta и toDelta, если вы хотите, слева направо и справа-слева анимации ,

Надеюсь, это поможет.

0
If you want your animation working in back press then overrride the onBackPress Method 

@Override 
public void onBackPressed() { 
// super.onBackPressed(); 

    Intent desiredIntent = new Intent(this 
      , DesiredActivity.class); 
    desiredIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    startActivity(desiredIntent); 
    overridePendingTransition(0,0); 

} 


Use the below line after startActivity if you want no animation 

    overridePendingTransition(0,0); 

This will override the default animation and do no animation. You can also give some custom animation if you like 



    overridePendingTransition(R.anim.animation1,R.anim.animation2); 
Смежные вопросы