Simulate animation inside a custom view on Android

I have a custom view that looks like a spinning wheel with numbers from 0 to 9. Basically it scrolls from 0 to 9 while I load something from the server, and when the value returns, the animation stops on it. The animation is created by updating the Y value of the text I draw

Some relevant codes:

public class TimeSpinner extends View
{

    .....
    private void initialize()
    {
    .....

        handler = new Handler();
        repetitiveRunnable = new Runnable() {
            @Override
            public void run() {
                updateData();
            }
        };

    }

    public void updateData() {

        float delta = 3;

        mDigitY += delta;
        mDigitAboveY += delta;
        mDigitBelowY += delta;

        //Test if animation needs to stop
        if (mCurrentDigit == animateToDigit) {
            handler.removeCallbacks(repetitiveRunnable);
        } else {
            handler.postDelayed(repetitiveRunnable, 5);
        }

        invalidate();
    }


    public void startLoadingDigit() {
        handler.postDelayed(repetitiveRunnable, 5);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        canvas.drawText(mDigitString, mDigitX, mDigitY, mDigitPaint);
        canvas.drawText(mDigitAboveString, mDigitX, mDigitAboveY, mDigitPaint);
        canvas.drawText(mDigitBelowString, mDigitX, mDigitBelowY, mDigitPaint);

    }

}

The problem is that there is some kind of drawing for other views in the user interface stream, so depending on the speed of the phone, the animation is not fluid. This is similar to an unsuccessful frame rate, and sometimes for half a second. On powerful devices reasonably good.

, , , ? , SurfaceView. -? .

.

AsycTask Y

public class TimeSpinnerTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

            while (animationRunning) {

                updateData();
                publishProgress();

                try {
                    Thread.sleep(35);
                } catch (InterruptedException e) {

                }
            }
            return null;
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            invalidate();
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            invalidate();
            super.onPostExecute(result);
        }
    }

public void updateData() {

        mDigitY += delta;
        mDigitAboveY += delta;
        mDigitBelowY += delta;

        if (mDigitAboveY > findCenterY(mCurrentDigit)) {
            setCurrentDigit(mDigitAbove);
        }

        if (mCurrentDigit == animateToDigit) {

            animationRunning = false;

            setCurrentDigit(animateToDigit);
        }
    }

, , 1 , .

executeOnExecutor, "" Android Maps V2. , .

, . , : enter image description here ?

+4
3

ValueAnimator? , . .

// Create a new value animator that will use the range 0 to 1
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);

// It will take 5000ms for the animator to go from 0 to 1
animator.setDuration(5000);

// Callback that executes on animation steps. 
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float value = ((Float) (animation.getAnimatedValue())).floatValue();

        Log.d("ValueAnimator", "value=" + value);

        // Here you can now translate or redraw your view
        // You need to map 'value' to your animation in regards to time
        // eg) mDigitY = value; invalidate();
    }
});

, OvershootInterpolator.

animator.setInterpolator(new OvershootInterpolator());
+10

AsyncTasks, , AsyncTask task.executeOnExecutor, . - .

, , . GC_FOR_ALLOC freed 190K, 13% free 5409K/6164K, paused 67ms, total 71ms .

, drawText - . .

, , OpenGL .

0

, . XML , 0 9. imageView. , , -, reset , , , .

XML (number_slider.xml)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/num_0"
        android:duration="150"/>
    <item
        android:drawable="@drawable/num_1"
        android:duration="150"/>
    <item
        android:drawable="@drawable/num_2"
        android:duration="150"/>
    <item
        android:drawable="@drawable/num_3"
        android:duration="150"/>
</animation-list> 

: a)

imageView.setBackgroundResource(R.anim.number_slider);
AnimationDrawable  loadingViewAnim = (AnimationDrawable) thumbnail.getBackground();
loadingViewAnim.start();

b) , -,

loadingViewAnim.stop();
imageView.setBackgroundColor(Color.WHITE);
imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.num_3));

, number_slider.xml, "". " ", "Speed-Control", , . , , , .

0

Source: https://habr.com/ru/post/1532787/


All Articles