android动画实现实例,如何实现Android动画的实例

大家看到动画人物都是非常漂亮,不管是大朋友还是小朋友应该都很喜欢,但是对于程序员要在Android系统上实现却不像我们想象的那么简单,大家跟着爱站技术频道小编的步伐进入下文了解了解如何实现Android动画的实例吧!

在main.xml里加个ImageView,如

android:id="@+id/container"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rotate"

android:textSize="50px"

android:layout_x="150px"

android:layout_y="30px"

android:src="@drawable/ro">

>

这个不需要解释吧,都可以看懂的

最后,还需要一个activity类

如:

public class TestRotate extends Activity implements OnClickListener{

private mageView imageview;

private ViewGroup mContainer;

/**

*这个变量设置的是图片,如果是多张图片,那么可以用数组,如

*private static final int IMAGE = new int[]{

* R.drawable.ro,

* R.drawable.icon

*};

*有多少图片就放多少,我这里做的只是一张图片的翻转

*

*/

private static final int IMAGE = R.drawable.ro;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imageview = (ImageView) findViewById(R.id.image);

mContainer = (ViewGroup) findViewById(R.id.container);

/**

* 设置最新显示的图片

* 如果是数组,那么可以写成IMAGE[int]

*

*/

imageview.setImageResource(IMAGE);

/**

*

* 设置ImageView的OnClickListener

*

*/

imageview.setClickable(true);

imageview.setFocusable(true);

imageview.setOnClickListener(this);

}

private void applyRotation(int position, float start, float end) {

// Find the center of the container

final float centerX = mContainer.getWidth() / 2.0f;

final float centerY = mContainer.getHeight() / 2.0f;

final Rotate3d rotation =

new Rotate3d(start, end, centerX, centerY, 310.0f, true);

rotation.setDuration(500);

rotation.setFillAfter(true);

rotation.setInterpolator(new AccelerateInterpolator());

rotation.setAnimationListener(new DisplayNextView(position));

mContainer.startAnimation(rotation);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

/**

*

* 调用这个方法,就是翻转图片

* 参数很简单,大家都应该看得懂

* 简单说下,第一个是位置,第二是开始的角度,第三个是结束的角度

* 这里需要说明的是,如果是要回到上一张

* 把第一个参数设置成-1就行了

*

*/

applyRotation(0,0,90);

}

private final class DisplayNextView implements Animation.AnimationListener {

private final int mPosition;

private DisplayNextView(int position) {

mPosition = position;

}

public void onAnimationStart(Animation animation) {

}

public void onAnimationEnd(Animation animation) {

mContainer.post(new SwapViews(mPosition));

}

public void onAnimationRepeat(Animation animation) {

}

}

/**

* This class is responsible for swapping the views and start the second

* half of the animation.

*/

private final class SwapViews implements Runnable {

private final int mPosition;

public SwapViews(int position) {

mPosition = position;

}

public void run() {

final float centerX = mContainer.getWidth() / 2.0f;

final float centerY = mContainer.getHeight() / 2.0f;

Rotate3d rotation;

if (mPosition > -1) {

imageview.setVisibility(View.VISIBLE);

imageview.requestFocus();

rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);

} else {

imageview.setVisibility(View.GONE);

rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);

}

rotation.setDuration(500);

rotation.setFillAfter(true);

rotation.setInterpolator(new DecelerateInterpolator());

mContainer.startAnimation(rotation);

}

}

}

上文是如何实现Android动画的实例的全部内容,大家都清楚了吗?希望本篇内容对你学习有帮助,爱站技术频道还为大家准备了其他内容,感兴趣的一起来了解吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值