首页 > 代码库 > 简单的动画效果

简单的动画效果

alpha.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="3000"/>
</set>

rotate.xml:

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

scale.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="0.0"
        android:toXScale="1.4"
        android:fromYScale="0.0"
        android:toYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700"/>
</set>

translate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate android:fromXDelta="30"
       android:toXDelta="-80"
       android:fromYDelta="30"
       android:toYDelta="300"
       android:duration="2000"/>
</set>

layout:

<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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="18dp"
        android:src="http://www.mamicode.com/@drawable/img1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginRight="16dp"
        android:src="http://www.mamicode.com/@drawable/img4" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="30dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/imageView2"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:src="http://www.mamicode.com/@drawable/img5" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView3"
        android:layout_below="@+id/imageView3"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_alignTop="@+id/button3"
        android:layout_marginTop="16dp"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/button4"
        android:layout_alignTop="@+id/imageView3"
        android:layout_marginTop="16dp"
        android:src="http://www.mamicode.com/@drawable/img2" />
   
</RelativeLayout>

Activity:

public class MainActivity extends Activity implements OnClickListener{
private ImageView img1;
private ImageView img2;
private ImageView img3;
private ImageView img4;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  img1=(ImageView)findViewById(R.id.imageView1);
  img2=(ImageView)findViewById(R.id.imageView2);
  img3=(ImageView)findViewById(R.id.imageView3);
  img4=(ImageView)findViewById(R.id.imageView4);
  findViewById(R.id.button1).setOnClickListener(this);
  findViewById(R.id.button2).setOnClickListener(this);
  findViewById(R.id.button3).setOnClickListener(this);
  findViewById(R.id.button4).setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.button1:
   Animation animation=AnimationUtils.loadAnimation(this, R.anim.alpha);
   img1.startAnimation(animation);
   break;
  case R.id.button2:
   Animation animation1=AnimationUtils.loadAnimation(this, R.anim.scale);
   img2.startAnimation(animation1);
   break;
  case R.id.button3:
   Animation animation2=AnimationUtils.loadAnimation(this, R.anim.translate);
   img3.startAnimation(animation2);
   break;
  case R.id.button4:
   Animation animation3=AnimationUtils.loadAnimation(this, R.anim.rotate);
   img4.startAnimation(animation3);
   break;
  }
  
 }
 

简单的动画效果