标签:animation 动画 缩放 移动 渐进
android开发之Animations的使用(一)
本博文主要讲述的是android开发中的动画效果(Animations)API的使用,主要讲述动画的四种效果的实现:
第一,图片缩放效果
第二,图片移动效果
第三,图片翻转效果
第四,图片的渐进效果
下面我们来看代码:
首先我们来看MainActivity.java:
package com.example.animationtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView = null;
private Button scaleButton = null;
private Button translateButton = null;
private Button rotateButton = null;
private Button alphaButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.myImage);
scaleButton = (Button)findViewById(R.id.scaleButton);
translateButton = (Button)findViewById(R.id.translateButton);
rotateButton = (Button)findViewById(R.id.rotateButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
scaleButton.setOnClickListener(new setScaleListener());
translateButton.setOnClickListener(new setTranslateListener());
rotateButton.setOnClickListener(new setRotateListener());
alphaButton.setOnClickListener(new setAlphaListener());
}
//动画缩放效果监听器
class setScaleListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个Animation对象 (1,1) --> (0.5,0.5)
Animation animation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画过程的时间
animation.setDuration(1000);
//设置动画开始时间偏移量
animation.setStartOffset(1000);
//设置重复播放的次数
animation.setRepeatCount(0);
//结束时的状态
animation.setFillAfter(true);
//将Animation对象添加到AnimationSet对象中
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//动画移动效果监听器
class setTranslateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//(0,0) --> (1,1)
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//旋转动画效果监听器
class setRotateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//360旋转,RELATIVE_TO_PARENT 以父控件的(0,1)为中心
//RELATIVE_TO_SELF 自身
Animation animation = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//渐入渐出动画效果监听器
class setAlphaListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//1:表示完全可见
//0:表示完全不可见
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
@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;
}
}
主布局文件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" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/imgLayout"
android:layout_below="@id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="55dp"
>
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgLayout"
android:text="@string/scale"
/>
<Button
android:id="@+id/translateButton"
android:layout_below="@id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/translate"
/>
<Button
android:id="@+id/rotateButton"
android:layout_below="@id/translateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/rotate"
/>
<Button
android:id="@+id/alphaButton"
android:layout_below="@id/rotateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/alpha"
/>
</RelativeLayout>
实现效果如下:
分别点击四个按钮就会看到图片会展示对应的四种效果
android开发之Animations的使用(一)
标签:animation 动画 缩放 移动 渐进
原文地址:http://blog.csdn.net/ajhsdj/article/details/41944329