android 控件 3d 旋转效果

本文详细介绍了在Android应用中使用自定义的Rotate3d类实现控件旋转动画的方法,包括类的初始化、参数设置以及在Activity中应用动画的步骤。通过设置旋转角度、旋转中心点等参数,可以灵活地控制控件的旋转效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中 用到控件的旋转效果 ,研究了一下 这里做个记录

旋转类 

Rotate3d

package com.appdoll.rotate3dtest;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Rotate3d extends Animation {
	private float fromDegree;	// 旋转起始角度
	private float toDegree;		// 旋转终止角度
	private float mCenterX;		// 旋转中心x
	private float mCenterY;		// 旋转中心y
	private Camera mCamera;

	public Rotate3d(float fromDegree, float toDegree, float centerX, float centerY) {
		this.fromDegree = fromDegree;
		this.toDegree = toDegree;
		this.mCenterX = centerX;
		this.mCenterY = centerY;

	}

	@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 FromDegree = fromDegree;
		float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime;	
		final float centerX = mCenterX;
		final float centerY = mCenterY;
		final Matrix matrix = t.getMatrix();

		if (degrees <= -76.0f) {
			degrees = -90.0f;
			mCamera.save();
			mCamera.rotateY(degrees);		
			mCamera.getMatrix(matrix);
			mCamera.restore();
		} else if (degrees >= 76.0f) {
			degrees = 90.0f;
			mCamera.save();
			mCamera.rotateY(degrees);
			mCamera.getMatrix(matrix);
			mCamera.restore();
		} else {
			mCamera.save();
			mCamera.translate(0, 0, centerX);		
			mCamera.rotateY(degrees);
			mCamera.translate(0, 0, -centerX);
			mCamera.getMatrix(matrix);
			mCamera.restore();
		}

		matrix.preTranslate(-centerX, -centerY);
		matrix.postTranslate(centerX, centerY);
	}
}

activity

package com.appdoll.rotate3dtest;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Rotate3dTestActivity extends Activity {
	private ViewGroup layout1,layout2;
	Button btnNext1, btnNext2;
	private Rotate3d layout1Animation ,layout2Animation;
	private int mCenterX, mCenterY;
	Context context=this;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        layout1 = (ViewGroup) findViewById(R.id.layout1);
        layout2 = (ViewGroup) findViewById(R.id.layout2);
        btnNext1 = (Button) layout1.findViewById(R.id.btn_next1);
        btnNext2 = (Button) layout2.findViewById(R.id.btn_next2);
        
        initAnimation();
        btnNext1.setOnClickListener(listener);
        btnNext2.setOnClickListener(listener);
    }
    private View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
				case R.id.btn_next1:
					Toast.makeText(context, "next1", 0).show();
					layout1.startAnimation(layout1Animation);
					layout2.startAnimation(layout2Animation);
					break;
				case R.id.btn_next2:
					Toast.makeText(context, "next2", 0).show();
					layout2.startAnimation(layout1Animation);
					layout1.startAnimation(layout2Animation);
					break;
			}
		}
    	
    };
    public void initAnimation(){
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();
		mCenterX = dm.widthPixels / 2;
		mCenterY = dm.heightPixels / 2;   
		int duration = 1000;
		
		layout1Animation = new Rotate3d(0, -90, mCenterX, mCenterY);	
		layout1Animation.setFillAfter(true);
		layout1Animation.setDuration(duration);

		layout2Animation = new Rotate3d(90, 0, mCenterX, mCenterY);		
		layout2Animation.setFillAfter(true);
		layout2Animation.setDuration(duration);
		
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   	android:background="#eeeeee"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<FrameLayout
	    android:layout_height="fill_parent"
	    android:layout_width="fill_parent"  
	     >
	    <RelativeLayout
	        android:id="@+id/layout2"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:background="#cccccc"
	        android:gravity="center_vertical"
	        android:orientation="vertical"	 
	         >
		    <ImageView 
		        android:id="@+id/img1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:src="@drawable/photo"
		        />
		    <Button
		        android:id="@+id/btn_next2"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="next2"
		        android:layout_alignParentBottom="true"
		         />	        
	    </RelativeLayout>	     
	     <RelativeLayout
	         android:id="@+id/layout1"
	         android:layout_width="fill_parent"
	         android:layout_height="fill_parent"
	         android:orientation="vertical"
	          >
		    <ImageView 
		        android:id="@+id/img1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:src="@drawable/p2"
		        />
		    <Button
		        android:id="@+id/btn_next1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_alignParentBottom="true"
		        android:layout_alignParentRight="true"
		        android:text="next1"
		         />
	    </RelativeLayout>

	    
	</FrameLayout>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值