首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android如何使用动画处理线程

android如何使用动画处理线程
EN

Stack Overflow用户
提问于 2011-12-19 12:30:35
回答 1查看 1.6K关注 0票数 0

我怎样才能throw any object(Stone) continuouslymoving Horizontally object?我使用了一个线程,它可以连续使用翻译动画来扔石头,但是内存使用量很大,我的设备在3-4 min.How之后就会慢下来来解决dis问题?

EN

回答 1

Stack Overflow用户

发布于 2011-12-19 12:38:48

我认为您最好实现自己的SurfaceView。在它中,您可以更巧妙地绘制动画对象(使用专用线程),而不是使用视图动画。(当然,您必须为此重写部分代码,但从长远来看,这可能是最好的)。

如果您想尝试SurfaceView,我建议您查看一下来自安卓的月球着陆器示例。

示例:

代码语言:javascript
复制
public class ThrowView extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener{
    //The OnGestureListener can detect a fling motion.
    private class DrawingThread extends Thread{
        private final float timefactor = 0.0001f; //This is used to make the animation a bit slower. Play with this value if the animation seems too be to slow or too fast.
        private final float damp = 0.9; //This is used to slow down the object. If it stops too fast or slow, this is the value to change.
        private float stoneX = 0; //The x-coordinate of our stone object. Use this when drawing.
        private float stoneY = 0; //The y-coordinate of our stone object. Use this when drawing.
        @Override
        public void run(){
            while(running){
            Canvas c = null;
            try{
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) { 
                    updatePhysics();
                    doDraw(c); //doDraw is not in this example, but it should essentially just draw our object at stoneX, stoneY.
                }
            }finally{
                if(c!=null) surfaceHolder.unlockCanvasAndPost(c); 
            }
            SystemClock.sleep(40);
        }
        private void updatePhysics(long time){
            //Calculate how much time has passed since last step:
            time1 = System.currentTimeMillis();
            dT = (time1 - time2)*timefactor;
            time2 = time1;

            //Move the stone in x depending on the time and velocity:
            stoneX += vX*dT;
            //Decrease the velocity:
            vX -= dT*damp:
        }
    }
    protected volatile float vX = 0; //This is the x-speed. To be able to throw in y too, just make another variable for that.
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //This is our onFling, here we take the velocity of the users fling and set our variable to that.
        //Note that this value is based on pixels.
        vX = velocityX;
        return true;
    }
    @Override
    public boolean onDown(MotionEvent e) {
        //The onDown should return true so onFling can be activated:
        return true;
    }
}

为便于使用,根据月球着陆器样本制作了此示例。这里省略了许多方法(对于这个例子来说不是必需的),但是可以根据月球着陆器来实现。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8561400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档