How do I make my animation smoother Android

泄露秘密 提交于 2019-11-30 15:37:10

Using an Asynctask is along the lines of what you want to do. However, I would suggest using a thread-pool. You can then put your animation in as a task, add your audio recording as a task, the FFT as another task, and your additional analysis as a task.

The brief stutter is likely the result of allocating resources for the recording in the animation thread. Using a pool means you won't have to pause while creating the thread to run your audio tasks. Obviously, some code would be handy to fully understand your problem.

Take a look at:

ScheduledThreadPoolExecutor http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

or

ThreadPoolExecutor http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

A very simple example of what you might want to do:

In your activity you can define this to start up the tasks

ExecutorService threadPool = new ScheduledThreadPoolExecutor(2);
Recording record = new Recording();
Ball ball = new Ball(threadPool, record);
threadPool.submit(ball);

Ball.java

import java.util.concurrent.ExecutorService;

public class Ball implements Runnable { 
    private final Recording record;
    private final ExecutorService threadPool;

    private boolean condition;  
    private boolean active;

    public Ball(ExecutorService threadPool, Recording record) {
        this.record = record;
        this.threadPool = threadPool;
        this.active = true;
    }

    @Override public void run() {
        while (this.active) {
            moveBall();
            if (isBallHalfway()) {
                threadPool.submit(record);
            }
        }
    }

    private boolean isBallHalfway() {
        return condition; // Condition for determining when ball is halfway
    }

    private void moveBall() {
        // Code to move the ball
    }
}

Recording.java

public class Recording implements Runnable {
    @Override public void run() {
        // Do recording tasks here
    }
}

As Mohammed says, it's hard to guess what the problem might be without seeing the code. With that in mind...

It sounds like your AsyncTask might be blocking other threads from executing. Make sure you have some calls to Thread.sleep() in there to give the scheduler chance to switch back to the UI thread (Thread.yield() is also an option, but be aware there are some gotchas with yield).

without code it will be hard to guess with can optimize, but I will tell you what I did with more than 1 million operations in loading .. • If you have problem with performance, try : 1-check your data type of variables and set lower in case you are sure variable doesn't need more byte, i.e. for size you short instead of int.

2-check your code and try to get all results can be stored for next launch, and call them next time instead of calculating them again.

• But if your problem that animation is slow and you have no problem with memory, so try reduce period of animation or increase units of movements.

by the way how do you implement the animation? frame-by-frame animation, or layout animation, or view animation ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!