android 控件翻转切换布局

8 篇文章 0 订阅
7 篇文章 0 订阅
[img]http://dl.iteye.com/upload/picture/pic/111473/70e8a0b3-f3ff-3f39-8725-ef39d9662298.gif[/img]


过程是:
1:准备好布局如下:
2:翻转view动画,翻转动画的监听。
3:翻转后布局的替换,这里用了隐藏
我用了setVisibility(View.INVISIBLE)


layout/mygaller_item_bg_01是正面
layout/mygaller_item_bg_02是反面


布局1:mygaller_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="wrap_content" android:padding="25dip"
android:id="@+id/container">
<FrameLayout android:layout_width="fill_parent"
android:orientation="vertical" android:layout_height="wrap_content"
android:id="@+id/container_bg">
<include layout="@layout/mygaller_item_bg_01" />
<include layout="@layout/mygaller_item_bg_02" />
</FrameLayout>
</LinearLayout>



View翻转效果:来源:http://mobile.51cto.com/android-265495.htm
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public 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);
}
}




过程和动画翻转的控制

public class ViewRotate implements OnClickListener {
private ImageView imageview;
private View bg;
private DisplayNextView displayNextView;
private Context context;
private View convertView;
private LayoutInflater mInflater;
private boolean now_zhengfan;
private View container_bg;

public ViewRotate(Context context, View convertView,
LayoutInflater mInflater) {
this.context = context;
this.convertView = convertView;
this.mInflater = mInflater;
now_zhengfan = true;
AnimationUtils.loadAnimation(context, R.anim.my_alpha_action);
init();
}

public void init() {


bg = (ViewGroup) convertView.findViewById(R.id.container);
bg.findViewById(R.id.btn_more).setOnClickListener(this);
container_bg = convertView.findViewById(R.id.container_bg);

container_bg
.setBackgroundDrawable(GraphicsBitmapUtils.
BitmapToDrawable(GraphicsBitmapUtils.getRoundedCornerBitmap(GraphicsBitmapUtils
.drawableToBitmap(context.getResources()
.getDrawable(R.drawable.zh)))));
}

private void applyRotation(int position, float start, float end) {
// Find the center of the container
final float centerX = bg.getWidth() / 2.0f;
final float centerY = bg.getHeight() / 2.0f;
final Rotate3d rotation = new Rotate3d(start, end, centerX, centerY,
310.0f, false);
rotation.setDuration(500);
rotation.setFillAfter(false);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position, true));
bg.startAnimation(rotation);


AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bg.setEnabled(false);
applyRotation(0, 0, 90);
}

private final class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private final boolean b;

private DisplayNextView(int position, boolean t) {
mPosition = position;
b = t;
}

public void onAnimationStart(Animation animation) {
}

public void onAnimationEnd(Animation animation) {
if (b) {
bg.post(new SwapViews(mPosition));
if (now_zhengfan) {
bg.findViewById(R.id.backe_bg1).setVisibility(
View.INVISIBLE);
} else {
bg.findViewById(R.id.backe_bg2).setVisibility(
View.INVISIBLE);
}
} else {

bg.setEnabled(true);
if (now_zhengfan) {
bg.findViewById(R.id.backe_bg2).setVisibility(View.VISIBLE);
bg.setOnClickListener(ViewRotate.this);
bg.setClickable(true);

AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
alphaAnim.setDuration(2000);
alphaAnim.setStartOffset(500);
alphaAnim
.setAnimationListener(new CanClickAnimationListener(
bg));
bg.findViewById(R.id.backe_bg2).startAnimation(alphaAnim);
now_zhengfan = false;
} else {
bg.findViewById(R.id.backe_bg1).setVisibility(View.VISIBLE);
bg.setOnClickListener(ViewRotate.this);

now_zhengfan = true;
bg.setClickable(false);
View btn = bg.findViewById(R.id.btn_more);
btn.setOnClickListener(ViewRotate.this);

// container_bg
// .setBackgroundDrawable(GraphicsBitmapUtils.BitmapToDrawable(GraphicsBitmapUtils.getRoundedCornerBitmap(GraphicsBitmapUtils
// .drawableToBitmap(context.getResources()
// .getDrawable(R.drawable.zh)))));

AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
alphaAnim.setDuration(2000);
alphaAnim.setStartOffset(500);
alphaAnim
.setAnimationListener(new CanClickAnimationListener(
bg, btn));

bg.findViewById(R.id.backe_bg1).startAnimation(alphaAnim);
// bg.findViewById(R.id.backe_bg1).startAnimation(
// CopyOfTestRotate.this.animation);

}
}

}

public void onAnimationRepeat(Animation animation) {

}
}

private final class SwapViews implements Runnable {
private final int mPosition;

public SwapViews(int position) {
mPosition = position;
}

public void run() {
final float centerX = bg.getWidth() / 2.0f;
final float centerY = bg.getHeight() / 2.0f;
Rotate3d rotation;
if (mPosition > -1) {

rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f,
false);
rotation.setAnimationListener(new DisplayNextView(mPosition,
false));
} else {
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(false);
rotation.setInterpolator(new DecelerateInterpolator());
bg.startAnimation(rotation);
bg.setEnabled(false);
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值