(實現淡入淡出的效果)android.R.anim.fade_in, android.R.anim.fade_out;
(由左向右滑入的效果)android.R.anim.slide_in_left, android.R.anim.slide_out_right
我們更可在res/建立anim/來自行定義動畫
本範例建立5個button分別以不同的anim開啟Animation.java, 後點擊任意處回到Main_Activity.java, 結束一個動畫的展現.
Main_Activity.java
package com.tsots.Animation; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main_Activity extends Activity { Context context = Main_Activity.this; String which_button = "null"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main_activity); Button button_slide = (Button)findViewById(R.id.button_slide); Button button_fade = (Button)findViewById(R.id.button_fade); Button button_left = (Button)findViewById(R.id.button_left); Button button_zoom = (Button)findViewById(R.id.button_zoom); Button button_rotate = (Button)findViewById(R.id.button_rotate); /* * Button OnClickListener事件被觸發時, 用Intent前往另一個Activity * overridePendingTransition()必須放在finish()或是startActivity()之後 */ //上下滑動的效果 button_slide.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("WHICH_BUTTON", "slide"); i.putExtras(bundle); i.setClass(context, Animation.class); startActivity(i); overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_down); } }); //實現淡入淡出的效果 button_fade.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("WHICH_BUTTON", "fade"); i.putExtras(bundle); i.setClass(context, Animation.class); startActivity(i); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } }); //由左向右滑入的效果 button_left.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("WHICH_BUTTON", "left"); i.putExtras(bundle); i.setClass(context, Animation.class); startActivity(i); overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right); } }); //放大縮小的效果 button_zoom.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("WHICH_BUTTON", "zoom"); i.putExtras(bundle); i.setClass(context, Animation.class); startActivity(i); overridePendingTransition(R.anim.zoomin, R.anim.zoomout); } }); //旋轉的效果 button_rotate.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putString("WHICH_BUTTON", "rotate"); i.putExtras(bundle); i.setClass(context, Animation.class); startActivity(i); overridePendingTransition(R.anim.rotate_in, R.anim.rotate_out); } }); } @Override public void overridePendingTransition(int enterAnim, int exitAnim) { super.overridePendingTransition(enterAnim, exitAnim); } }
Animation.java
package com.tsots.Animation; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class Animation extends Activity { ImageView finishView1; Bundle bundle; String which_button; TextView animation_des; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_animation); finishView1 = (ImageView) findViewById(R.id.finishView1); animation_des = (TextView) findViewById (R.id.animation_des); bundle = this.getIntent().getExtras(); which_button = bundle.getString("WHICH_BUTTON"); fun_animation_des(); finishView1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { System.out.println("which button selected? "+ which_button); if(which_button.equals("slide")) { animation_des.setText(getResources().getString(R.string.str_slide_in_up)); finish(); overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_down); } else if(which_button.equals("fade")) { finish(); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } else if(which_button.equals("left")) { finish(); overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right); } else if(which_button.equals("zoom")) { finish(); overridePendingTransition(R.anim.zoomin, R.anim.zoomout); } else if(which_button.equals("rotate")) { finish(); overridePendingTransition(R.anim.rotate_in, R.anim.rotate_out); } else { finish(); overridePendingTransition(R.anim.zoomin, R.anim.zoomout); } } }); } public void fun_animation_des() { if(which_button.equals("slide")) { animation_des.setText(getResources().getString(R.string.str_slide_in_up)); } else if(which_button.equals("fade")) { animation_des.setText(getResources().getString(R.string.str_fade_in)); } else if(which_button.equals("left")) { animation_des.setText(getResources().getString(R.string.str_slide_in_left)); } else if(which_button.equals("zoom")) { animation_des.setText(getResources().getString(R.string.str_zoomin)); } else if(which_button.equals("rotate")) { animation_des.setText(getResources().getString(R.string.str_rotate_in)); } else { } } }
沒有留言:
張貼留言