1. 程式人生 > >android camera 實現3d效果

android camera 實現3d效果

效果圖

其中 圖片使用ImageView來展示

       50下邊其實是個文字框

       變形:按鈕,點選後 將圖片沿著Y軸 旋轉文字框 指定的角度。

實現的程式碼很簡單:

public class Base3dImp extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_3d_imp);

this.findViewById(R.id.btn_changeShape).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
EditText tvGress=(EditText) Base3dImp.this.findViewById(R.id.et_degress);
int degress=Integer.parseInt(tvGress.getText().toString().trim());
if(degress%90==0)
degress+=1;
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.p1);

Matrix matrix=new Matrix();System.out.println("martrix:"+matrix.toString());
Camera camera=new Camera();
camera.save();
// camera.rotateY(degress);
// camera.rotateX(degress);
camera.getMatrix(matrix);
camera.restore();System.out.println("martrix1:"+matrix.toString());

ImageView img=(ImageView)Base3dImp.this.findViewById(R.id.iv_base_3d);
int centerX=img.getWidth()/2;
int centerY=img.getHeight()/2;

Bitmap bmp1=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
System.out.println("martrix1:"+matrix.toString());

img.setImageBitmap(bmp1);

}
});

}
}

上述程式碼 為了便於觀察你 rotateY(degress)方法旋轉的實現過程, 下面將旋轉動畫:

先上效果圖:

  OpenGLActivity的程式碼如下:

package com.example.opengl;

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

import com.example.opengl.animation.Rotate3dAnimation;
import com.example.opengl.render.CylinderRender;
import com.example.opengl.render.HaxagonRender;
import com.example.opengl.render.RotateTriangle;
/**
* OpenGL練習
* @author YangBaoBao
*
*/
public class OpenGLActivity extends Activity {

private View v;
private ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// GLSurfaceView surface=new GLSurfaceView(this);
// surface.requestFocus();//獲取焦點
// surface.setFocusableInTouchMode(true);//設定為可觸控
// trigle(surface);
// sixshape(surface);
// clindershape(surface);
setContentView(R.layout.activity_open_gl);
v=this.findViewById(R.id.rl_main);
img=(ImageView) this.findViewById(R.id.img_animation);
this.findViewById(R.id.btn_startAnimation).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
applyRotation(0, 0, 360); 
//
// Intent i=new Intent(OpenGLActivity.this,Transition3d.class);
// OpenGLActivity.this.startActivity(i);
}
});

// View v=null;v.set

}
private void trigle(GLSurfaceView surface)
{
RotateTriangle rtgl=new RotateTriangle();
surface.setOnTouchListener(new RotateTriangle.MyTouchListener(rtgl));
surface.setRenderer(rtgl);
}
private void sixshape(GLSurfaceView surface)
{
HaxagonRender hr=new HaxagonRender();
surface.setOnTouchListener(new HaxagonRender.MyTouchListener(hr,surface));
surface.setRenderer(hr);
}
private void clindershape(GLSurfaceView surface)
{
CylinderRender hr=new CylinderRender();
surface.setOnTouchListener(new CylinderRender.MyTouchListener(hr,surface));
surface.setRenderer(hr);
}
// public void staticAnimation()
// {
// Rotate3dAnimation ra=new Rotate3dAnimation(0, 180,img.getX()/2,img.getY()/2,310,false);
// img.startAnimation(ra);
// }
private void applyRotation(int position, float start, float end) { 
// Find the center of the container 
final float centerX = img.getWidth() / 2.0f; 
final float centerY = img.getHeight() / 2.0f; 

// Create a new 3D rotation with the supplied parameter 
// The animation listener is used to trigger the next animation 
final Rotate3dAnimation rotation = 
new Rotate3dAnimation(start, end, centerX, centerY, 0, true); 
rotation.setDuration(5000); 
rotation.setFillAfter(true); 
rotation.setInterpolator(new AccelerateInterpolator()); 
// rotation.setAnimationListener(new DisplayNextView(position)); 

img.startAnimation(rotation); 

}

Rotate3dAnimation為實現的動畫類,也是很簡單的:

  先上效果圖:

package com.example.opengl.animation;

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

public class Rotate3dAnimation 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 Rotate3dAnimation(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);
System.out.println("degress:"+degrees);

System.out.println("interpolatedTime:"+((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);//mDepthZ * interpolatedTime+100
} else {
camera.translate(0.0f, 0.0f, mDepthZ* (1.0f - interpolatedTime));//mDepthZ * (1.0f - interpolatedTime)+100
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();

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