MainActivity.java
package com.example.crystalball; import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private static final String Tag = "MainActivity"; Button mButton;
TextView mTextView;
ImageView mImageView;
Animation mAnimation;
Animation mFadein;
SoundPool mSoundPool;
int click; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main); Log.v(Tag, "onCreate()"); mButton = (Button) findViewById(R.id.button1);
mTextView = (TextView) findViewById(R.id.textView1);
mImageView = (ImageView) findViewById(R.id.imageView1);
mAnimation = AnimationUtils.loadAnimation(this, R.anim.zoomin);
mFadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
click = mSoundPool.load(this, R.raw.click, 1); mButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = "Hello World";
mTextView.setText(answer);
mTextView.startAnimation(mFadein);
mImageView.startAnimation(mAnimation); playSound();
toast("跳转成功");
startAnotherActivity();
}
}); } //通过intent跳转到另一个activity
private void startAnotherActivity() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
} private void showDialog() {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setTitle("提示");
mBuilder.setMessage("确认关闭?");
mBuilder.setPositiveButton("确认", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
MainActivity.this.finish();
}
}); mBuilder.setNegativeButton("取消", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}); mBuilder.create().show();
} @Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(Tag, "onStart()");
} @Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.v(Tag, "onRestart()");
} @Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.v(Tag, "onResume()");
} @Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(Tag, "onPause()");
} @Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(Tag, "onStop()");
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(Tag, "onDestroy()");
} //按下退出键时
@Override
public void onBackPressed() {
showDialog();
} //播放声音
private void playSound() { mSoundPool.play(click, 1, 1, 0, 0, 1);
} //toast提示
private void toast(String content) {
Toast mToast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.show(); }
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
} }
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- alpha: 渐变透明度动画效果 -->
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>
zoomin.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- scale: 渐变尺寸伸缩动画效果 -->
<scale
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
xmlns:android="http://schemas.android.com/apk/res/android"> </scale>