您的位置:首页 > 移动开发 > Android开发

Android学习篇章18-动画:Tween动画Animation

2013-11-01 17:24 288 查看
public class MyAnimation extends Animation{

float px=0;
float py=0;
float scale=1.5f;
Camera  cam=null;
//在动画的播放过程中每一个阶段时间点都会调用这个方法
//interpolatedTime用来告诉我们动画已经播放了多少  取值在0-1之间
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//获得目前动画的变换矩阵
Matrix m= t.getMatrix();
cam.save();
cam.translate(0, 0, 1300-interpolatedTime*1300);
cam.rotateY(720*interpolatedTime);
//   cam.rotateX(720*interpolatedTime);
cam.getMatrix(m);
m.preTranslate(-px, -py);
m.postTranslate(px,py);
cam.restore();
//		 m.setScale(interpolatedTime*scale, interpolatedTime*scale
//				 ,px,py);
//		 m.postRotate(720*interpolatedTime, px, py);

}

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
px=parentWidth/2;
py=parentHeight/2;
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
this.setDuration(2500);
this.setFillAfter(false);
cam=new Camera();
//设置差值器为加速度差值器
this.setInterpolator(new AccelerateInterpolator());
}
}


public class MainActivity extends Activity {
ListView  list1=null;
ImageView img1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1=(ImageView)findViewById(R.id.img1);
//		list1=(ListView)findViewById(R.id.list1);
//		String[] week={"星期天","星期一","星期二","星期三","星期四","星期五","星期六","星期六","星期六","星期六","星期六","星期六","星期六","星期六"};
//		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,week);
//		list1.setAdapter(adapter);
}
public void clickBtn(View view)
{
MyAnimation anim=new MyAnimation();
img1.startAnimation(anim);
}
@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;
}
}


mainxml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button  android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="播放"
/>

<ImageView  android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/s2"
/>

</LinearLayout>


anim_ctr.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/rotate"
/>


rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>

</set>


scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="0"
android:fromYScale="0"
android:toYScale="1.5"
android:toXScale="1.5"
android:duration="3000"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
/>


set_alpha_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate  android:fromYDelta="-100%"
android:toYDelta="0"
android:duration="3000"
/>

<alpha android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="3000"/>
</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: