【移动应用开发技术】Android中怎么实现图片翻转动画效果_第1页
【移动应用开发技术】Android中怎么实现图片翻转动画效果_第2页
【移动应用开发技术】Android中怎么实现图片翻转动画效果_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】Android中怎么实现图片翻转动画效果

这篇文章将为大家详细讲解有关Android中怎么实现图片翻转动画效果,文章内容质量较高,因此在下分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。需要一个Rotate3d类,继承Animationpublic

class

Rotate3d

extends

Animation{

private

final

float

mFromDegrees;

private

final

float

mToDegrees;

private

final

float

mCenterX;

private

final

float

mCenterY;

private

final

float

mDepthZ;

private

final

boolean

mReverse;

private

Camera

mCamera;

public

Rotate3d(float

fromDegrees,

float

toDegrees,

float

centerX,

float

centerY,

float

depthZ,

boolean

reverse)

{

mFromDegrees

=

fromDegrees;

mToDegrees

=

toDegrees;

mCenterX

=

centerX;

mCenterY

=

centerY;

mDepthZ

=

depthZ;

mReverse

=

reverse;

}

@Override

public

void

initialize(int

width,

int

height,

int

parentWidth,

int

parentHeight)

{

super.initialize(width,

height,

parentWidth,

parentHeight);

mCamera

=

new

Camera();

}

@Override

protected

void

applyTransformation(float

interpolatedTime,

Transformation

t)

{

final

float

fromDegrees

=

mFromDegrees;

float

degrees

=

fromDegrees

+

((mToDegrees

-

fromDegrees)

*

interpolatedTime);

final

float

centerX

=

mCenterX;

final

float

centerY

=

mCenterY;

final

Camera

camera

=

mCamera;

final

Matrix

matrix

=

t.getMatrix();

camera.save();

if

(mReverse)

{

camera.translate(0.0f,

0.0f,

mDepthZ

*

interpolatedTime);

}

else

{

camera.translate(0.0f,

0.0f,

mDepthZ

*

(1.0f

-

interpolatedTime));

}

camera.rotateY(degrees);

camera.getMatrix(matrix);

camera.restore();

matrix.preTranslate(-centerX,

-centerY);

matrix.postTranslate(centerX,

centerY);

}

}这个类可以直接拷过去,不用做任何的修改。其中的方法自己找相关资料研究。在main.xml里加个ImageView,如<?xml

version="1.0"

encoding="utf-8"?>

<FrameLayout

xmlns:android="/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.VISIBL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论