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

Android tween动画(补间动画)

2017-05-03 18:55 387 查看
<RelativeLayout 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"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:onClick="rotate"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转" />

<Button
android:onClick="scale"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放" />

<Button
android:onClick="trans"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="位移" />

<Button
android:onClick="alpha"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="透明度" />

<Button
android:onClick="set"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="组合动画" />
</LinearLayout>

<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />

</RelativeLayout>


public class MainActivity extends Activity {
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
//透明度动画
public void alpha(View view){
AlphaAnimation  aa = new AlphaAnimation(0.0f, 1.0f);
aa.setDuration(2000);
aa.setRepeatCount(1);
aa.setRepeatMode(Animation.REVERSE);
aa.setFillAfter(true);
iv.startAnimation(aa);
}
//位移动画
public void trans(View view){
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
ta.setDuration(2000);
ta.setRepeatCount(1);
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
}
//缩放动画
public void scale(View view){
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000);
sa.setRepeatCount(1);
sa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(sa);
}

//旋转动画
public void rotate(View view){
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
ra.setDuration(2000);
ra.setRepeatCount(1);
ra.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ra);
}
//动画组合
public void set(View view){
AnimationSet set = new AnimationSet(false);
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, -0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f);
ta.setDuration(2000);
ta.setRepeatCount(1);
ta.setRepeatMode(Animation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000);
sa.setRepeatCount(1);
sa.setRepeatMode(Animation.REVERSE);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
ra.setDuration(2000);
ra.setRepeatCount(1);
ra.setRepeatMode(Animation.REVERSE);
set.addAnimation(ra);
//set.addAnimation(ta);
set.addAnimation(sa);
iv.startAnimation(set);
}
}


参考:

Android动画
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: