首先是主要代码 的实现 ,去调用 各个xml 配置文件:
MainActivity:
- public class MainActivity extends Activity {
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //获取显示控件ID
- imageView = (ImageView) findViewById(R.id.imageView1);
- }
- @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;
- }
- public void animImpl(View v){
- //调用 动画的配置文件
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_demo);
- imageView.startAnimation(animation);
- }
- //旋转动画
- public void rotate(){
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_demo);
- imageView.startAnimation(animation);
- }
- //缩放动画
- public void scaleImpl(){
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_demo);
- imageView.startAnimation(animation);
- }
- //移动效果
- public void translateImpl(){
- //XML文件
- /* Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_demo);
- animation.setRepeatCount(Animation.INFINITE);*/
- /* 第一种
- * imageView.setAnimation(animation);
- animation.start();*/
- //第二种
- // imageView.startAnimation(animation);
- //Java代码
- TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 0);
- translateAnimation.setDuration(2000);
- imageView.startAnimation(translateAnimation);
- }
- //缩放效果
- }
alpha_demo.xml
- <alpha xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromAlpha="1.0"
- android:toAlpha="0.1"
- android:duration="2000"/>
- <!--
- fromAlpha :起始透明度
- toAlpha:结束透明度
- 1.0表示完全不透明
- 0.0表示完全透明
- -->
rotate_demo.xml
- <rotate xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="360"
- android:duration="1000"
- android:repeatCount="1"
- android:repeatMode="reverse"/>
- <!--
- fromDegrees:表示旋转的起始角度
- toDegrees:表示旋转的结束角度
- repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次
- repeatCount=-1 或者infinite 循环了
- repeatMode: 默认值:restart 模式要想其作用 repeatCount值必须大于0 或者是-1
- "reverse" 反转:
- -->
scale_demo.xml
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromXScale="0.2"
- android:toXScale="1.5"
- android:fromYScale="0.2"
- android:toYScale="1.5"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="2000"/>
- <!--
- fromXScale:表示沿着x轴缩放的起始比例
- toXScale:表示沿着x轴缩放的结束比例
- fromYScale:表示沿着y轴缩放的起始比例
- toYScale:表示沿着y轴缩放的结束比例
- 图片中心点:
- android:pivotX="50%"
- android:pivotY="50%"
- -->
translate_demo.xml
- <translate xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXDelta="0"
- android:toXDelta="320"
- android:fromYDelta="0"
- android:toYDelta="0"
- android:duration="2000"/>
- <!--
- android:interpolator 动画的渲染器
- 1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
- 2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
- 3、accelerate_decelerate_interpolator(动画加速减速器)
- 中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
- 使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
- fromXDelta 动画起始位置的横坐标
- toXDelta 动画起结束位置的横坐标
- fromYDelta 动画起始位置的纵坐标
- toYDelta 动画结束位置的纵坐标
- duration 动画的持续时间
- -->
activity_main.xml
- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:onClick="animImpl"
- android:text="@string/text_translate" />
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button1"
- android:layout_below="@+id/button1"
- android:layout_marginTop="36dp"
- android:src="@drawable/ic_launcher" />
- </RelativeLayout>