android 控件翻转切换布局

来源:互联网 发布:windows隐藏任务栏图标 编辑:程序博客网 时间:2024/04/30 17:07
 


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



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




Java代码  收藏代码
  1. 布局1:mygaller_item.xml  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent" android:orientation="vertical"  
  5.     android:layout_height="wrap_content" android:padding="25dip"  
  6.     android:id="@+id/container">  
  7.     <FrameLayout android:layout_width="fill_parent"  
  8.         android:orientation="vertical" android:layout_height="wrap_content"  
  9.         android:id="@+id/container_bg">  
  10.         <include layout="@layout/mygaller_item_bg_01" />  
  11.         <include layout="@layout/mygaller_item_bg_02" />  
  12.     </FrameLayout>  
  13. </LinearLayout>   








Java代码  收藏代码
  1. View翻转效果:来源:http://mobile.51cto.com/android-265495.htm  
  2. import android.graphics.Camera;  
  3. import android.graphics.Matrix;  
  4. import android.view.animation.Animation;  
  5. import android.view.animation.Transformation;  
  6.   
  7. public class Rotate3d extends Animation {  
  8.     private final float mFromDegrees;  
  9.     private final float mToDegrees;  
  10.     private final float mCenterX;  
  11.     private final float mCenterY;  
  12.     private final float mDepthZ;  
  13.     private final boolean mReverse;  
  14.     private Camera mCamera;  
  15.   
  16.     public Rotate3d(float fromDegrees, float toDegrees, float centerX,  
  17.             float centerY, float depthZ, boolean reverse) {  
  18.         mFromDegrees = fromDegrees;  
  19.         mToDegrees = toDegrees;  
  20.         mCenterX = centerX;  
  21.         mCenterY = centerY;  
  22.         mDepthZ = depthZ;  
  23.         mReverse = reverse;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void initialize(int width, int height, int parentWidth,  
  28.             int parentHeight) {  
  29.         super.initialize(width, height, parentWidth, parentHeight);  
  30.         mCamera = new Camera();  
  31.     }  
  32.   
  33.     @Override  
  34.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  35.         final float fromDegrees = mFromDegrees;  
  36.         float degrees = fromDegrees  
  37.                 + ((mToDegrees - fromDegrees) * interpolatedTime);  
  38.         final float centerX = mCenterX;  
  39.         final float centerY = mCenterY;  
  40.         final Camera camera = mCamera;  
  41.         final Matrix matrix = t.getMatrix();  
  42.         camera.save();  
  43.         if (mReverse) {  
  44.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  45.         } else {  
  46.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  47.         }  
  48.         camera.rotateY(degrees);  
  49.         camera.getMatrix(matrix);  
  50.         camera.restore();  
  51.         matrix.preTranslate(-centerX, -centerY);  
  52.         matrix.postTranslate(centerX, centerY);  
  53.     }  
  54. }  



Java代码  收藏代码
  1. 过程和动画翻转的控制  
  2.   
  3. public class ViewRotate implements OnClickListener {  
  4.     private ImageView imageview;  
  5.     private View bg;  
  6.     private DisplayNextView displayNextView;  
  7.     private Context context;  
  8.     private View convertView;  
  9.     private LayoutInflater mInflater;  
  10.     private boolean now_zhengfan;  
  11.     private View container_bg;  
  12.   
  13.     public ViewRotate(Context context, View convertView,  
  14.             LayoutInflater mInflater) {  
  15.         this.context = context;  
  16.         this.convertView = convertView;  
  17.         this.mInflater = mInflater;  
  18.         now_zhengfan = true;  
  19.         AnimationUtils.loadAnimation(context, R.anim.my_alpha_action);  
  20.         init();  
  21.     }  
  22.   
  23.     public void init() {  
  24.   
  25.           
  26.         bg = (ViewGroup) convertView.findViewById(R.id.container);  
  27.         bg.findViewById(R.id.btn_more).setOnClickListener(this);  
  28.         container_bg = convertView.findViewById(R.id.container_bg);  
  29.   
  30.         container_bg  
  31.         .setBackgroundDrawable(GraphicsBitmapUtils.  
  32.                 BitmapToDrawable(GraphicsBitmapUtils.getRoundedCornerBitmap(GraphicsBitmapUtils  
  33.                 .drawableToBitmap(context.getResources()  
  34.                         .getDrawable(R.drawable.zh)))));  
  35.     }  
  36.   
  37.     private void applyRotation(int position, float start, float end) {  
  38.         // Find the center of the container  
  39.         final float centerX = bg.getWidth() / 2.0f;  
  40.         final float centerY = bg.getHeight() / 2.0f;  
  41.         final Rotate3d rotation = new Rotate3d(start, end, centerX, centerY,  
  42.                 310.0f, false);  
  43.         rotation.setDuration(500);  
  44.         rotation.setFillAfter(false);  
  45.         rotation.setInterpolator(new AccelerateInterpolator());  
  46.         rotation.setAnimationListener(new DisplayNextView(position, true));  
  47.         bg.startAnimation(rotation);  
  48.           
  49.   
  50. AlphaAnimation alphaAnim = new AlphaAnimation(01);  
  51.     }  
  52.   
  53.     @Override  
  54.     public void onClick(View v) {  
  55.         // TODO Auto-generated method stub  
  56.         bg.setEnabled(false);  
  57.         applyRotation(0090);  
  58.     }  
  59.   
  60.     private final class DisplayNextView implements Animation.AnimationListener {  
  61.         private final int mPosition;  
  62.         private final boolean b;  
  63.   
  64.         private DisplayNextView(int position, boolean t) {  
  65.             mPosition = position;  
  66.             b = t;  
  67.         }  
  68.   
  69.         public void onAnimationStart(Animation animation) {  
  70.         }  
  71.   
  72.         public void onAnimationEnd(Animation animation) {  
  73.             if (b) {  
  74.                 bg.post(new SwapViews(mPosition));  
  75.                 if (now_zhengfan) {  
  76.                     bg.findViewById(R.id.backe_bg1).setVisibility(  
  77.                             View.INVISIBLE);  
  78.                 } else {  
  79.                     bg.findViewById(R.id.backe_bg2).setVisibility(  
  80.                             View.INVISIBLE);  
  81.                 }  
  82.             } else {  
  83.   
  84.                 bg.setEnabled(true);  
  85.                 if (now_zhengfan) {  
  86.                     bg.findViewById(R.id.backe_bg2).setVisibility(View.VISIBLE);  
  87.                     bg.setOnClickListener(ViewRotate.this);  
  88.                     bg.setClickable(true);  
  89.   
  90.                     AlphaAnimation alphaAnim = new AlphaAnimation(01);  
  91.                     alphaAnim.setDuration(2000);  
  92.                     alphaAnim.setStartOffset(500);  
  93.                     alphaAnim  
  94.                             .setAnimationListener(new CanClickAnimationListener(  
  95.                                     bg));  
  96.                     bg.findViewById(R.id.backe_bg2).startAnimation(alphaAnim);  
  97.                     now_zhengfan = false;  
  98.                 } else {  
  99.                     bg.findViewById(R.id.backe_bg1).setVisibility(View.VISIBLE);  
  100.                     bg.setOnClickListener(ViewRotate.this);  
  101.   
  102.                     now_zhengfan = true;  
  103.                     bg.setClickable(false);  
  104.                     View btn = bg.findViewById(R.id.btn_more);  
  105.                     btn.setOnClickListener(ViewRotate.this);  
  106.   
  107. //                  container_bg  
  108. //                          .setBackgroundDrawable(GraphicsBitmapUtils.BitmapToDrawable(GraphicsBitmapUtils.getRoundedCornerBitmap(GraphicsBitmapUtils  
  109. //                                  .drawableToBitmap(context.getResources()  
  110. //                                          .getDrawable(R.drawable.zh)))));  
  111.   
  112.                     AlphaAnimation alphaAnim = new AlphaAnimation(01);  
  113.                     alphaAnim.setDuration(2000);  
  114.                     alphaAnim.setStartOffset(500);  
  115.                     alphaAnim  
  116.                             .setAnimationListener(new CanClickAnimationListener(  
  117.                                     bg, btn));  
  118.   
  119.                     bg.findViewById(R.id.backe_bg1).startAnimation(alphaAnim);  
  120.                     // bg.findViewById(R.id.backe_bg1).startAnimation(  
  121.                     // CopyOfTestRotate.this.animation);  
  122.   
  123.                 }  
  124.             }  
  125.   
  126.         }  
  127.   
  128.         public void onAnimationRepeat(Animation animation) {  
  129.   
  130.         }  
  131.     }  
  132.   
  133.     private final class SwapViews implements Runnable {  
  134.         private final int mPosition;  
  135.   
  136.         public SwapViews(int position) {  
  137.             mPosition = position;  
  138.         }  
  139.   
  140.         public void run() {  
  141.             final float centerX = bg.getWidth() / 2.0f; 
  142.             final float centerY = bg.getHeight() / 2.0f;  
  143.             Rotate3d rotation;  
  144.             if (mPosition > -1) {  
  145.   
  146.                 rotation = new Rotate3d(90180, centerX, centerY, 310.0f,  
  147.                         false);  
  148.                 rotation.setAnimationListener(new DisplayNextView(mPosition,  
  149.                         false));  
  150.             } else {  
  151.                 rotation = new Rotate3d(900, centerX, centerY, 310.0f, false);  
  152.             }  
  153.             rotation.setDuration(500);  
  154.             rotation.setFillAfter(false);  
  155.             rotation.setInterpolator(new DecelerateInterpolator());  
  156.             bg.startAnimation(rotation);  
  157.             bg.setEnabled(false);  
  158.         }  
  159.     }  
  160.   
  161. }  

来自: http://b275518834201204034558.iteye.com/blog/1486820
0 0