Android ImageSwitcher 实现按钮的3d旋转效果

Rotate3d继承Android的Animation实现3d旋转:

public class Rotate3d extends Animation {
    private float mFromDegree;   
    private float mToDegree;   
    private float mCenterX;   
    private float mCenterY;   
    private float mLeft;   
    private float mTop;   
    private Camera mCamera;   
    private static final String TAG = "Rotate3d";
    
    public Rotate3d(float fromDegree, float toDegree, float left, float top,   
            float centerX, float centerY) {   
        this.mFromDegree = fromDegree;   
        this.mToDegree = toDegree;   
        this.mLeft = left;   
        this.mTop = top;   
        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 = mFromDegree;   
        float degrees = FromDegree + (mToDegree - mFromDegree)   
                * 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);
	}
    
}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

   
</LinearLayout>

activity:

public class TestActivity extends Activity {

	private int mCenterX = 160;
	private int mCenterY = 0;
	private ImageSwitcher imageSwitcher;

	private Rotate3d leftAnimation;
	private Rotate3d rightAnimation;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);// 显示front
		initSecond();
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		imageSwitcher.setFactory(new OnFactorylmpl());// 设置转换工厂
		imageSwitcher.setInAnimation(leftAnimation);// 设置动画
		imageSwitcher.setOutAnimation(rightAnimation);// 设置动画
		imageSwitcher.setImageResource(R.drawable.background_01);// 设置初始图片
		
		imageSwitcher.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				imageSwitcher.setImageResource(R.drawable.background_02);
				  v.setEnabled(false);   
			}
		});
	}

	// 左旋转
	public void initFirst() {
		leftAnimation = new Rotate3d(0, -90, 0.0f, 0.0f, mCenterX, mCenterY);
		rightAnimation = new Rotate3d(90, 0, 0.0f, 0.0f, mCenterX, mCenterY);
		leftAnimation.setFillAfter(true);
		leftAnimation.setDuration(1000);
		rightAnimation.setFillAfter(true);
		rightAnimation.setDuration(1000);
	}

	// 右旋转
	public void initSecond() {
		leftAnimation = new Rotate3d(-90, 0, 0.0f, 0.0f, mCenterX, mCenterY);
		rightAnimation = new Rotate3d(0, 90, 0.0f, 0.0f, mCenterX, mCenterY);
		leftAnimation.setFillAfter(true);
		leftAnimation.setDuration(1000);
		rightAnimation.setFillAfter(true);
		rightAnimation.setDuration(1000);
	}

	private class OnFactorylmpl implements ViewFactory {
		@Override
		public View makeView() {
			ImageView image = new ImageView(TestActivity.this);// 创建图片控件
			image.setBackgroundColor(0xFFFFFFFF);// 设置背景颜色
			image.setScaleType(ImageView.ScaleType.CENTER); // 设置显示方式
			image.setLayoutParams(new ImageSwitcher.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));// 定义组建
			return image;
		}

	}

}


你可能感兴趣的:(android,ImageSwitch)