Android中怎么實(shí)現(xiàn)圖片翻轉(zhuǎn)動(dòng)畫效果

這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么實(shí)現(xiàn)圖片翻轉(zhuǎn)動(dòng)畫效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、國(guó)際域名空間、雅安服務(wù)器托管、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。

需要一個(gè)Rotate3d類,繼承Animation

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

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


main.xml里加個(gè)ImageView,如

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/container"     android:layout_width="fill_parent"     android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotate" android:textSize="50px" android:layout_x="150px"   android:layout_y="30px" android:src="@drawable/ro"> ></ImageView> </FrameLayout>

這個(gè)不需要解釋吧,都可以看懂的

***,還需要一個(gè)activity類

如:

public class TestRotate extends Activity implements OnClickListener{   private mageView imageview;   private ViewGroup mContainer;   /**    *這個(gè)變量設(shè)置的是圖片,如果是多張圖片,那么可以用數(shù)組,如    *private static final int IMAGE = new int[]{    * R.drawable.ro,    * R.drawable.icon    *};    *有多少圖片就放多少,我這里做的只是一張圖片的翻轉(zhuǎn)    *    */   private static final int IMAGE = R.drawable.ro;      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                  imageview = (ImageView) findViewById(R.id.image);          mContainer = (ViewGroup) findViewById(R.id.container);                  /**           * 設(shè)置***顯示的圖片           * 如果是數(shù)組,那么可以寫成IMAGE[int]           *            */          imageview.setImageResource(IMAGE);                  /**           *            * 設(shè)置ImageView的OnClickListener           *            */                  imageview.setClickable(true);          imageview.setFocusable(true);          imageview.setOnClickListener(this);      }      private void applyRotation(int position, float start, float end) {          // Find the center of the container          final float centerX = mContainer.getWidth() / 2.0f;          final float centerY = mContainer.getHeight() / 2.0f;          final Rotate3d rotation =                  new Rotate3d(start, end, centerX, centerY, 310.0f, true);          rotation.setDuration(500);          rotation.setFillAfter(true);          rotation.setInterpolator(new AccelerateInterpolator());          rotation.setAnimationListener(new DisplayNextView(position));          mContainer.startAnimation(rotation);      }       @Override   public void onClick(View v) {    // TODO Auto-generated method stub    /**     *      * 調(diào)用這個(gè)方法,就是翻轉(zhuǎn)圖片     * 參數(shù)很簡(jiǎn)單,大家都應(yīng)該看得懂     * 簡(jiǎn)單說下,***個(gè)是位置,第二是開始的角度,第三個(gè)是結(jié)束的角度     * 這里需要說明的是,如果是要回到上一張     * 把***個(gè)參數(shù)設(shè)置成-1就行了     *      */    applyRotation(0,0,90);   }   private final class DisplayNextView implements Animation.AnimationListener {          private final int mPosition;          private DisplayNextView(int position) {              mPosition = position;          }          public void onAnimationStart(Animation animation) {          }          public void onAnimationEnd(Animation animation) {              mContainer.post(new SwapViews(mPosition));          }          public void onAnimationRepeat(Animation animation) {          }      }      /**       * This class is responsible for swapping the views and start the second       * half of the animation.       */      private final class SwapViews implements Runnable {          private final int mPosition;          public SwapViews(int position) {              mPosition = position;          }          public void run() {              final float centerX = mContainer.getWidth() / 2.0f;              final float centerY = mContainer.getHeight() / 2.0f;              Rotate3d rotation;                         if (mPosition > -1) {               imageview.setVisibility(View.VISIBLE);               imageview.requestFocus();                  rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);              } else {               imageview.setVisibility(View.GONE);                  rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);              }              rotation.setDuration(500);              rotation.setFillAfter(true);              rotation.setInterpolator(new DecelerateInterpolator());              mContainer.startAnimation(rotation);          }      }  }

關(guān)于Android中怎么實(shí)現(xiàn)圖片翻轉(zhuǎn)動(dòng)畫效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章題目:Android中怎么實(shí)現(xiàn)圖片翻轉(zhuǎn)動(dòng)畫效果
本文地址:http://bm7419.com/article34/igiepe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站制作、App開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)