`

android 控件翻转切换布局

阅读更多



过程是:
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);
		}
	}

}
分享到:
评论
1 楼 离艮巽 2014-06-23  
谢谢,刚好有用到

相关推荐

Global site tag (gtag.js) - Google Analytics