Android之十三图片2D翻转
java代码
package com.example.fanzhuan;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Animatable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageA;
private ImageView imageB;
private ScaleAnimation sato=new ScaleAnimation(1,
0, 1, 1,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT, 0.5f);
private ScaleAnimation sato1=new ScaleAnimation(0,
1, 1, 1,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT, 0.5f);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
findViewById(R.id.root).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
if(imageA.getVisibility()==View.VISIBLE){
imageA.startAnimation(sato);
Toast.makeText(MainActivity.this, "数值11111", Toast.LENGTH_LONG);
}else
imageB.startAnimation(sato);
Toast.makeText(MainActivity.this, "数值22222", Toast.LENGTH_LONG);
}
});
}
@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;
}
private void showImageA(){
imageA.setVisibility(View.VISIBLE);
imageB.setVisibility(View.INVISIBLE);
}
private void showImageB(){
imageA.setVisibility(View.INVISIBLE);
imageB.setVisibility(View.VISIBLE);
}
private void initview(){
imageA=(ImageView) findViewById(R.id.ivA);
imageB=(ImageView)findViewById(R.id.ivB);
showImageA();
sato.setDuration(500);//动画持续时间
sato1.setDuration(500);
sato.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if(imageA.getVisibility()==View.VISIBLE){
imageA.setAnimation(null);
showImageB();
imageB.startAnimation(sato1);
Toast.makeText(MainActivity.this, "数值333333", Toast.LENGTH_SHORT);
}else
{
imageB.setAnimation(null);
showImageA();
imageA.startAnimation(sato1);
Toast.makeText(MainActivity.this, "数值44444", Toast.LENGTH_SHORT);
}
}
});
}
}
Ui代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/ivA"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/shi" />
<ImageView
android:id="@+id/ivB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/shishi" />
</RelativeLayout>