Android 3D旋轉動畫之Camera 和 Matrix,androidcamera

來源:互聯網
上載者:User

Android 3D旋轉動畫之Camera 和 Matrix,androidcamera

前面兩篇博文講解的都是Android 的2D動畫效果,要想做出非常炫酷的3D動畫效果怎麼辦?android 並沒有提供3D動畫介面給使用者,所以我們得自己重寫這樣一個3D介面動畫。

介面如下:

/* * @Title: My3dAnimation.java * @Description: TODO<請描述此檔案是做什麼的> * @author: xjp * @data: 2014年9月15日 上午8:54:10 * @version: V1.0 */package com.xjp.animator;import android.graphics.Camera;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.Transformation;/** * TODO<請描述這個類是幹什麼的> *  * @author xjp * @data: 2014年9月15日 上午8:54:10 * @version: V1.0 */public class My3dAnimation extends Animation {private final float mFromDegrees;private final float mToDegrees;private final float mCenterX;private final float mCenterY;private final float mDepthZ;private Camera mCamera;private int mDirection;private final static int ROTATE_X = 0;//沿著x軸旋轉private final static int ROTATE_Y = 1;//沿著y軸旋轉/** * Creates a new 3D rotation on the Y axis. The rotation is defined by its * start angle and its end angle. Both angles are in degrees. The rotation * is performed around a center point on the 2D space, definied by a pair of * X and Y coordinates, called centerX and centerY. When the animation * starts, a translation on the Z axis (depth) is performed. The length of * the translation can be specified, as well as whether the translation * should be reversed in time. *  * @param direction *            the direction of the 3D rotation * @param fromDegrees *            the start angle of the 3D rotation * @param toDegrees *            the end angle of the 3D rotation * @param centerX *            the X center of the 3D rotation * @param centerY *            the Y center of the 3D rotation */public My3dAnimation(int direction, float fromDegrees, float toDegrees,float centerX, float centerY, float depthZ) {mDirection = direction;mFromDegrees = fromDegrees;mToDegrees = toDegrees;mCenterX = centerX;mCenterY = centerY;mDepthZ = depthZ;}@Overridepublic void initialize(int width, int height, int parentWidth,int parentHeight) {super.initialize(width, height, parentWidth, parentHeight);mCamera = new Camera();}@Overrideprotected 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 (centerX!=0){if (interpolatedTime < 0.5) {camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);} else {camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));}}switch (mDirection) {case ROTATE_X:camera.rotateX(degrees);break;case ROTATE_Y:camera.rotateY(degrees);break;}camera.getMatrix(matrix);camera.restore();matrix.preTranslate(-centerX, -centerY);matrix.postTranslate(centerX, centerY);}}

範例程式碼如下:

package com.xjp.animator;import com.xjp.animator.R;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.animation.LinearInterpolator;import android.widget.ImageView;import android.app.Activity;public class MainActivity extends Activity implementsandroid.view.View.OnClickListener {private ImageView img1;private ImageView img2;private ImageView img3;private ViewGroup mContainer;private final static int ROTATE_X = 0;private final static int ROTATE_Y = 1;private My3dAnimation my3dAnimation;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();}/** * TODO<請描述這個方法是幹什麼的> *  * @throw * @return void * @param */private void findViews() {// TODO Auto-generated method stubmContainer = (ViewGroup) findViewById(R.id.container);mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);img1 = (ImageView) findViewById(R.id.img_left);img1.setOnClickListener(this);img2 = (ImageView) findViewById(R.id.img_right);img2.setOnClickListener(this);img3 = (ImageView) findViewById(R.id.img_3);img3.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfloat centerX = v.getWidth() / 2.0f;float centerY = v.getHeight() / 2.0f;if (v.getId() == R.id.img_left) {my3dAnimation = new My3dAnimation(ROTATE_X, 0, 180, centerX,centerY, 310f);}if (v.getId() == R.id.img_right) {my3dAnimation = new My3dAnimation(ROTATE_Y, 0, 180, centerX,centerY, 310f);}if (v.getId() == R.id.img_3) {centerX = 0;my3dAnimation = new My3dAnimation(ROTATE_Y, 0, 20, centerX,centerY, 310f);}my3dAnimation.setDuration(1000);my3dAnimation.setInterpolator(new LinearInterpolator());my3dAnimation.setFillAfter(true);v.startAnimation(my3dAnimation);}}

完結。


android 上下滑動按鈕 按鈕圖片翻轉180度從正面到反面 再滑動回到正面 如此迴圈 就代碼 先了

代碼沒有,沒實現過類似需求。
上下滑動按鈕是類似UC瀏覽器中看小說的那種,點擊可以向上滾動一屏內容的?這個暫時沒實現思路。回頭有時間看看。
圖片翻轉180從正面到反面這個Android API Demo裡面就有,你開個模擬器,開啟Views——>Animation——>3D Transition這裡面第一個就是翻轉效果,匯入API Demo代碼到Eclipse你就可以看源碼了。
 
android 不用XML,怎在代碼裡用animation同時實現圖片的縮放與移動

.....樓上複製帝啊。。。
隨便定義一個AnimationSet as吧(onCreate前,共用)
然後你自己選擇是觸發按鈕還是開始時同時處理啥的~觸發兩個動畫的處理
隨便寫了
suofang();
yidong();
下面定義下。。。。。。
private void suofang()}
as= new AnimationSet(true);//定義一個新AnimationSet
ScaleAnimation sa = new ScaleAnimation(開始x座標伸縮尺寸,開始y座標伸縮尺寸,)
結束x座標伸縮尺寸,結束y座標伸縮尺寸,x軸的百分比,y軸的百分比)//全員float可,
前4個伸縮尺寸0.0伸縮到沒有,1.0正常,大於1.0放大
sa.setDuration(動畫的時間long型);
as.addAnimation(sa);
這個位置寫需要動畫的組件你自己定義的image1,button1什麼的.startAnimation(as);
}
private void yidong(){
as = new Animation(true);
TranslateAnimation ta = new TranslateAnimation(開始x座標,開始y座標,結束x座標,結束y座標);
ta.setDuration();
as.addAnimation(ta);
.startAnimation(as);//跟前面一樣的地方就不寫了。。。比較懶。。。
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.