Android编程实现3D旋转效果实例

MainView.java中代码：

```package com.android.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MainView extends View{
//Camera类
private Camera mCamera;
private Bitmap face;
private Matrix mMatrix = new Matrix();
private Paint mPaint = new Paint();
private int mLastMotionX, mLastMotionY;
//图片旋转时的中心点坐标
private int centerX, centerY;
//转动的总距离，跟度数比例1:1
private int deltaX, deltaY;
//图片宽度高度
private int bWidth, bHeight;
public MainView(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
setWillNotDraw(false);
mCamera = new Camera();
mPaint.setAntiAlias(true);
face = BitmapFactory.decodeResource(getResources(), R.drawable.x);
bWidth = face.getWidth();
bHeight = face.getHeight();
centerX = bWidth>>1;
centerY = bHeight>>1;
}
void rotate(int degreeX, int degreeY) {
deltaX += degreeX;
deltaY += degreeY;
mCamera.save();
mCamera.rotateY(deltaX);
mCamera.rotateX(-deltaY);
mCamera.translate(0, 0, -centerX);
mCamera.getMatrix(mMatrix);
mCamera.restore();
//以图片的中心点为旋转中心,如果不加这两句，就是以（0,0）点为旋转中心
mMatrix.preTranslate(-centerX, -centerY);
mMatrix.postTranslate(centerX, centerY);
mCamera.save();
postInvalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mLastMotionX;
int dy = y - mLastMotionY;
rotate(dx, dy);
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
@Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.drawBitmap(face, mMatrix, mPaint);
}
}

```

main.xml中代码：

```<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.android.graphics.MainView
android:id="@+id/cv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

```

Android实现在map上画出路线的方法

ubuntu16.04怎么远程远程登录linux系统?

Android中RecyclerView点击Item设置事件
ASP.NET连接sql2008数据库的实现代码
Android中自定义标题栏样式的两种方法
Java调用浏览器打开网页完整实例
Android组件之DrawerLayout实现抽屉菜单
iOS动画实现雨花与樱花特效
【HTML 元素】嵌入图像的实现方法

JS获取及验证开始结束日期的方法