自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

簡單實現(xiàn)Android圖片翻轉(zhuǎn)動畫效果

移動開發(fā) Android
下面給大家分享一個有趣的動畫:這里比較適合一張圖片的翻轉(zhuǎn),如果是多張圖片,可以參考APIDemo里的例子,就是加個ArrayAdapter,還是簡單的,也可以自己發(fā)揮修改,實現(xiàn)自己想要的。這里的代碼基本上可以直接運行項目了。

什么都不說,先看效果

這是原始圖片的樣子

這是翻轉(zhuǎn)后的效果圖

如果是你想要的效果,那么繼續(xù)往下看,如果不是,那可以跳過了。這是一個動畫,而不是用matrix實現(xiàn)的直接翻轉(zhuǎn)圖片。

我這個是根據(jù)APIDemo簡單修改寫的
需要一個Rotate3d 類,繼承Animation

  1. public class Rotate3d extends Animation{  
  2.  private final float mFromDegrees;  
  3.     private final float mToDegrees;  
  4.     private final float mCenterX;  
  5.     private final float mCenterY;  
  6.     private final float mDepthZ;  
  7.     private final boolean mReverse;  
  8.     private Camera mCamera;  
  9.     public Rotate3d(float fromDegrees, float toDegrees,  
  10.             float centerX, float centerY, float depthZ, boolean reverse) {  
  11.         mFromDegrees = fromDegrees;  
  12.         mToDegrees = toDegrees;  
  13.         mCenterX = centerX;  
  14.         mCenterY = centerY;  
  15.         mDepthZ = depthZ;  
  16.         mReverse = reverse;  
  17.     }  
  18.     @Override  
  19.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  20.         super.initialize(width, height, parentWidth, parentHeight);  
  21.         mCamera = new Camera();  
  22.     }  
  23.     @Override  
  24.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  25.         final float fromDegrees = mFromDegrees;  
  26.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  
  27.         final float centerX = mCenterX;  
  28.         final float centerY = mCenterY;  
  29.         final Camera camera = mCamera;  
  30.         final Matrix matrix = t.getMatrix();  
  31.         camera.save();  
  32.         if (mReverse) {  
  33.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  34.         } else {  
  35.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  36.         }  
  37.         camera.rotateY(degrees);  
  38.         camera.getMatrix(matrix);  
  39.         camera.restore();  
  40.  
  41.         matrix.preTranslate(-centerX, -centerY);  
  42.         matrix.postTranslate(centerX, centerY);  
  43.     }  

這個類可以直接拷過去,不用做任何的修改。其中的方法自己找相關(guān)資料研究。

#p#
main.xml里加個ImageView,如

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@+id/container" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent"> 
  6. <ImageView 
  7. android:id="@+id/image" 
  8. android:layout_width="wrap_content" 
  9. android:layout_height="wrap_content" 
  10. android:text="Rotate" 
  11. android:textSize="50px" 
  12. android:layout_x="150px"   
  13. android:layout_y="30px" 
  14. android:src="@drawable/ro"> 
  15. ></ImageView> 
  16. </FrameLayout> 

這個不需要解釋吧,都可以看懂的

***,還需要一個activity類

如:

  1. public class TestRotate extends Activity implements OnClickListener{  
  2.  private mageView imageview;  
  3.  private ViewGroup mContainer;  
  4.  /**  
  5.   *這個變量設置的是圖片,如果是多張圖片,那么可以用數(shù)組,如  
  6.   *private static final int IMAGE = new int[]{  
  7.   * R.drawable.ro,  
  8.   * R.drawable.icon  
  9.   *};  
  10.   *有多少圖片就放多少,我這里做的只是一張圖片的翻轉(zhuǎn)  
  11.   *  
  12.   */  
  13.  private static final int IMAGE = R.drawable.ro;  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);          
  19.         imageview = (ImageView) findViewById(R.id.image);  
  20.         mContainer = (ViewGroup) findViewById(R.id.container);          
  21.         /**  
  22.          * 設置***顯示的圖片  
  23.          * 如果是數(shù)組,那么可以寫成IMAGE[int]  
  24.          *   
  25.          */  
  26.         imageview.setImageResource(IMAGE);          
  27.         /**  
  28.          *   
  29.          * 設置ImageView的OnClickListener  
  30.          *   
  31.          */          
  32.         imageview.setClickable(true);  
  33.         imageview.setFocusable(true);  
  34.         imageview.setOnClickListener(this);  
  35.     }  
  36.     private void applyRotation(int position, float start, float end) {  
  37.         // Find the center of the container  
  38.         final float centerX = mContainer.getWidth() / 2.0f;  
  39.         final float centerY = mContainer.getHeight() / 2.0f;  
  40.         final Rotate3d rotation =  
  41.                 new Rotate3d(start, end, centerX, centerY, 310.0f, true);  
  42.         rotation.setDuration(500);  
  43.         rotation.setFillAfter(true);  
  44.         rotation.setInterpolator(new AccelerateInterpolator());  
  45.         rotation.setAnimationListener(new DisplayNextView(position));  
  46.         mContainer.startAnimation(rotation);  
  47.     }      
  48.  @Override  
  49.  public void onClick(View v) {  
  50.   // TODO Auto-generated method stub  
  51.   /**  
  52.    *   
  53.    * 調(diào)用這個方法,就是翻轉(zhuǎn)圖片  
  54.    * 參數(shù)很簡單,大家都應該看得懂  
  55.    * 簡單說下,***個是位置,第二是開始的角度,第三個是結(jié)束的角度  
  56.    * 這里需要說明的是,如果是要回到上一張  
  57.    * 把***個參數(shù)設置成-1就行了  
  58.    *   
  59.    */  
  60.   applyRotation(0,0,90);  
  61.  }  
  62.  private final class DisplayNextView implements Animation.AnimationListener {  
  63.         private final int mPosition;  
  64.         private DisplayNextView(int position) {  
  65.             mPosition = position;  
  66.         }  
  67.         public void onAnimationStart(Animation animation) {  
  68.         }  
  69.         public void onAnimationEnd(Animation animation) {  
  70.             mContainer.post(new SwapViews(mPosition));  
  71.         }  
  72.         public void onAnimationRepeat(Animation animation) {  
  73.         }  
  74.     }  
  75.     /**  
  76.      * This class is responsible for swapping the views and start the second  
  77.      * half of the animation.  
  78.      */  
  79.     private final class SwapViews implements Runnable {  
  80.         private final int mPosition;  
  81.         public SwapViews(int position) {  
  82.             mPosition = position;  
  83.         }  
  84.         public void run() {  
  85.             final float centerX = mContainer.getWidth() / 2.0f;  
  86.             final float centerY = mContainer.getHeight() / 2.0f;  
  87.             Rotate3d rotation;             
  88.             if (mPosition > -1) {  
  89.              imageview.setVisibility(View.VISIBLE);  
  90.              imageview.requestFocus();  
  91.                 rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);  
  92.             } else {  
  93.              imageview.setVisibility(View.GONE);  
  94.                 rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);  
  95.             }  
  96.             rotation.setDuration(500);  
  97.             rotation.setFillAfter(true);  
  98.             rotation.setInterpolator(new DecelerateInterpolator());  
  99.             mContainer.startAnimation(rotation);  
  100.         }  
  101.     }  

【編輯推薦】

Android學習筆記:Activity跳轉(zhuǎn)

Android開發(fā)中插入新的Activity

Android開發(fā):Activity之間的傳值

Android應用開發(fā)教程:兩個運行的Activity之間的通信

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2017-02-06 13:00:49

Android翻轉(zhuǎn)卡片動畫效果

2011-07-08 10:15:15

IPhone 動畫

2016-03-29 10:18:48

Android圖片代碼

2017-05-03 16:36:32

Android圖片動畫

2012-06-04 14:47:42

HTML5

2022-03-29 11:28:24

HarmonyOS動畫css

2011-07-22 18:20:04

IOS View 動畫

2011-08-12 14:04:53

iPhone動畫

2015-01-23 16:29:44

2024-03-20 09:40:27

動畫技巧CSS逐幀動畫

2011-07-08 15:08:16

iPhone 圖片

2011-07-29 13:55:10

IPhone 動畫

2009-09-22 12:59:58

ibmdwDojo

2012-05-17 13:17:26

HTML5

2011-08-16 18:13:42

IPhone開發(fā)UIView動畫

2011-07-19 13:07:26

iOS4 HTML5 動畫

2015-06-18 10:33:02

iOS粘性動畫

2015-09-16 09:20:34

WWDC蘋果動畫效果

2013-07-05 10:26:40

Android

2011-02-17 10:54:59

CSS3變換 簡單快捷
點贊
收藏

51CTO技術(shù)棧公眾號