Android开发教程:SurfaceView使用实例
| [日期:2012-06-30] |
来源:Linux社区
作者:Sodino |
[字体:大 中 小]
|
- ParabolaView.java
- package lab.sodino.surfaceview;
-
- import Android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.PixelFormat;
- import android.util.AttributeSet;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
-
-
-
-
- public class ParabolaView extends SurfaceView implements SurfaceHolder.Callback {
-
- private static final long SLEEP_DURATION = 10l;
- private SurfaceHolder holder;
-
- private Bitmap bitmap;
- private DrawThread thread;
- private PhysicalTool physicalTool;
- private ParabolaView.ParabolaListener listener;
-
- private boolean surfaceDestoryed = true;
-
- public ParabolaView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
-
- public ParabolaView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
-
- public ParabolaView(Context context) {
- super(context);
- init();
- }
-
- private void init() {
- holder = getHolder();
- holder.addCallback(this);
- holder.setFormat(PixelFormat.TRANSPARENT);
-
- setZOrderOnTop(true);
-
-
- physicalTool = new PhysicalTool();
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- surfaceDestoryed = false;
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
-
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- LogOut.out(this, "surfaceDestroyed");
- surfaceDestoryed = true;
- physicalTool.cancel();
- }
-
- public void handleThread() {
- Canvas canvas = null;
-
- Paint pTmp = new Paint();
- pTmp.setAntiAlias(true);
- pTmp.setColor(Color.RED);
-
- Paint paint = new Paint();
-
- paint.setAntiAlias(true);
- paint.setColor(Color.CYAN);
- physicalTool.start();
- LogOut.out(this, "doing:" + physicalTool.doing());
- if (listener != null) {
- listener.onParabolaStart(this);
- }
- while (physicalTool.doing()) {
- try {
- physicalTool.compute();
- canvas = holder.lockCanvas();
-
- canvas.drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
-
- float x = (float) physicalTool.getX();
-
- float y = (float) physicalTool.getMirrorY(getHeight(), bitmap.getHeight());
-
- canvas.drawRect(x, y, x + bitmap.getWidth(), y + bitmap.getHeight(), pTmp);
- canvas.drawBitmap(bitmap, x, y, paint);
- holder.unlockCanvasAndPost(canvas);
- Thread.sleep(SLEEP_DURATION);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- if (surfaceDestoryed == false) {
- canvas = holder.lockCanvas();
- canvas.drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
- holder.unlockCanvasAndPost(canvas);
- }
-
- thread.setRunning(false);
- if (listener != null) {
- listener.onParabolaEnd(this);
- }
- }
-
- public void showMovie() {
- if (thread == null) {
- thread = new DrawThread(this);
- } else if (thread.getState() == Thread.State.TERMINATED) {
- thread.setRunning(false);
- thread = new DrawThread(this);
- }
- LogOut.out(this, "thread.getState:" + thread.getState());
- if (thread.getState() == Thread.State.NEW) {
- thread.start();
- }
- }
-
-
- public boolean isShowMovie() {
- return physicalTool.doing();
- }
-
- public void setIcon(Bitmap bit) {
- bitmap = bit;
- }
-
- public void setParams(int height, int width) {
- physicalTool.setParams(height, width);
- }
-
-
- public void setParabolaListener(ParabolaView.ParabolaListener listener) {
- this.listener = listener;
- }
-
- static interface ParabolaListener {
- public void onParabolaStart(ParabolaView view);
-
- public void onParabolaEnd(ParabolaView view);
- }
- }
- PhysicalTool.java
- package lab.sodino.surfaceview;
-
-
-
-
-
- public class PhysicalTool {
-
- private static final float GRAVITY = 400.78033f;
-
- private static final float WASTAGE = 0.3f;
-
- private int height;
-
- private int width;
-
- private double velocity;
-
- private double x, y;
-
- private long startTime;
-
- private double t1;
-
- private double t2;
-
- private boolean doing;
-
- public void start() {
- startTime = System.currentTimeMillis();
- doing = true;
- }
-
-
- public void setParams(int h, int w) {
- height = h;
- width = w;
-
- t1 = Math.sqrt(2 * height * 1.0d / GRAVITY);
- t2 = Math.sqrt((1 - WASTAGE) * 2 * height * 1.0d / GRAVITY);
- velocity = width * 1.0d / (t1 + 2 * t2);
- LogOut.out(this, "t1=" + t1 + " t2=" + t2);
- }
-
-
- public void compute() {
- double used = (System.currentTimeMillis() - startTime) * 1.0d / 1000;
- x = velocity * used;
- if (0 <= used && used < t1) {
- y = height - 0.5d * GRAVITY * used * used;
- } else if (t1 <= used && used < (t1 + t2)) {
- double tmp = t1 + t2 - used;
- y = (1 - WASTAGE) * height - 0.5d * GRAVITY * tmp * tmp;
- } else if ((t1 + t2) <= used && used < (t1 + 2 * t2)) {
- double tmp = used - t1 - t2;
- y = (1 - WASTAGE) * height - 0.5d * GRAVITY * tmp * tmp;
- } else {
- LogOut.out(this, "used:" + used + " set doing false");
- x = velocity * (t1 + 2 * t2);
- y = 0;
- doing = false;
- }
- }
-
- public double getX() {
- return x;
- }
-
- public double getY() {
- return y;
- }
-
-
- public double getMirrorY(int parentHeight, int bitHeight) {
- int half = parentHeight >> 1;
- double tmp = half + (half - y);
- tmp -= bitHeight;
- return tmp;
- }
-
- public boolean doing() {
- return doing;
- }
-
- public void cancel() {
- doing = false;
- }
- }
- RotateAnimation.java
- package lab.sodino.surfaceview;
-
- import android.graphics.Camera;
- import android.graphics.Matrix;
- import android.view.animation.Animation;
- import android.view.animation.Transformation;
-
-
-
-
-
- public class RotateAnimation extends Animation {
-
- public static final boolean DEBUG = false;
-
- public static final boolean ROTATE_DECREASE = true;
-
- public static final boolean ROTATE_INCREASE = false;
-
- public static final float DEPTH_Z = 310.0f;
-
- public static final long DURATION = 800l;
-
- private final boolean type;
- private final float centerX;
- private final float centerY;
- private Camera camera;
-
- private InterpolatedTimeListener listener;
-
- public RotateAnimation(float cX, float cY, boolean type) {
- centerX = cX;
- centerY = cY;
- this.type = type;
- setDuration(DURATION);
- }
-
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
-
- super.initialize(width, height, parentWidth, parentHeight);
- camera = new Camera();
- }
-
- public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
- this.listener = listener;
- }
-
- protected void applyTransformation(float interpolatedTime, Transformation transformation) {
-
- if (listener != null) {
- listener.interpolatedTime(interpolatedTime);
- }
- float from = 0.0f, to = 0.0f;
- if (type == ROTATE_DECREASE) {
- from = 0.0f;
- to = 180.0f;
- } else if (type == ROTATE_INCREASE) {
- from = 360.0f;
- to = 180.0f;
- }
- float degree = from + (to - from) * interpolatedTime;
- boolean overHalf = (interpolatedTime > 0.5f);
- if (overHalf) {
-
- degree = degree - 180;
- }
-
- float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;
- final Matrix matrix = transformation.getMatrix();
- camera.save();
- camera.translate(0.0f, 0.0f, depth);
- camera.rotateY(degree);
- camera.getMatrix(matrix);
- camera.restore();
- if (DEBUG) {
- if (overHalf) {
- matrix.preTranslate(-centerX * 2, -centerY);
- matrix.postTranslate(centerX * 2, centerY);
- }
- } else {
- matrix.preTranslate(-centerX, -centerY);
- matrix.postTranslate(centerX, centerY);
- }
- }
-
-
- public static interface InterpolatedTimeListener {
- public void interpolatedTime(float interpolatedTime);
- }
- }

评论声明
- 尊重网上道德,遵守中华人民共和国的各项有关法律法规
- 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
- 本站管理人员有权保留或删除其管辖留言中的任意内容
- 本站有权在网站内转载或引用您的评论
- 参与本评论即表明您已经阅读并接受上述条款
|
|
第 1 楼 匿名 发表于 2013/8/27 16:14:53受教了,谢谢。
|
|