Android-android如何实现推门的效果?

发布于 2016-11-29 02:11:56 字数 32 浏览 1346 评论 1

手指往右滑动,界面朝里面旋转,手离开,界面恢复正常

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2017-10-14 02:50:28

使用Android中的camera类,我把代码贴上来,你一运行就明白了。
1.封装好的camera类:

package com.rotate3d;

import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;

public class Rotate3dAnimation 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;
/**
* 初始化方法
* @param fromDegrees 开始的旋转角度
* @param toDegrees 结束的旋转角度
* @param centerX 中心X坐标
* @param centerY 中心Y坐标
* @param depthZ Z轴
* @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}

public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}

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));
}
//绕Y轴旋转
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

2.如何使用:

package com.rotate3d;

import com.rotate3d.cjy.R;

import android.app.Activity;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class Rotate3dActivity extends Activity {
private FrameLayout mContainer;
private ImageView imageView1;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 测试用的view
*/
mContainer = new FrameLayout(this);
mContainer.setBackgroundColor(Color.BLACK);
imageView1=new ImageView(this);
imageView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView1.setId(20);
imageView1.setImageResource(R.drawable.back);
//第1个图片的按键监听
imageView1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView1,mContainer);

}
});
mContainer.addView(imageView1);

imageView2 =new ImageView(this);
imageView2.setId(30);
imageView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView2.setImageResource(R.drawable.front);
//第2个图片的按键监听
imageView2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView2,mContainer);

}
});
mContainer.addView(imageView2);
setContentView(mContainer);

}

public void startRotation3D(float start,float end,final View rotateView,final View containerView){
//获得中心点
final float centerX=containerView.getWidth()/2.0f;
final float centerY =containerView.getHeight() / 2.0f;
//开始翻转,将当前界面从0°翻转到90°
Rotate3dAnimation rotation=new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
rotation.setDuration(500);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {

containerView.post(new Runnable() {
public void run() {
//判断哪个图片需要隐藏
Log.e("sC", String.valueOf(rotateView.getId()));
if(rotateView.getId() == 20){
imageView1.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}else if (rotateView.getId() == 30) {
imageView2.setVisibility(View.GONE);
imageView1.setVisibility(View.VISIBLE);
}
//第二次翻转,将新页面从90°翻转到0°
Rotate3dAnimation rotatiomAnimation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());

containerView.startAnimation(rotatiomAnimation);
}
});

}
public void onAnimationRepeat(Animation arg0) {
}

public void onAnimationStart(Animation arg0) {
}
});
containerView.startAnimation(rotation);
}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文