欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android图片翻转动画简易实现代码

程序员文章站 2023-12-05 18:03:10
下面给大家分享一个有趣的动画:这里比较适合一张图片的翻转,如果是多张图片,可以参考apidemo里的例子,就是加个arrayadapter,还是简单的,也可以自己发挥修改,...

下面给大家分享一个有趣的动画:这里比较适合一张图片的翻转,如果是多张图片,可以参考apidemo里的例子,就是加个arrayadapter,还是简单的,也可以自己发挥修改,实现自己想要的。这里的代码基本上可以直接运行项目了。
在main.xml里加个imageview,如

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<imageview
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">
></imageview>
</framelayout>

这个不需要解释吧,都可以看懂的
最后,还需要一个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);
}
}
}