10.2 Make an animation using the View class

In this section we use View class to implement an animation. We are preparing an animated image. Such as:

How to animate? We move the position of the view in order, then update the contents of the view size to the display and wait for about 0.02 seconds.

View Animation

The code for the animation as below:

from JMRPiFoundations.Skeleton.RPiSparkModule import RPiSparkModule
from PIL import Image
from time import sleep
import os.path

ANIMATION_IMG_FILE = "flower_small.jpg"
class ScrViewAnimation(RPiSparkModule):

    def _loadImage(self, filename):
        img_path = os.path.abspath(os.path.join('images/', filename))
        return Image.open(img_path)

    def setup(self):
        self.RPiSpark.Screen.redefineBuffer( self._loadImage( ANIMATION_IMG_FILE ) )

    def run(self):
        for row in range(0, 8):
            for col in range(0,4):
                self.RPiSpark.Screen.View.moveTo(col * 128, row * 72 )


        for row in range(7, -1, -1):
            for col in range(3, -1, -1):
                self.RPiSpark.Screen.View.moveTo(col * 128, row * 72 )

Save as ScrViewAnimation_10_2.py and execute the following command in the terminal:

$> rspk ScrViewAnimation_10_2 -f


Was this article helpful?

YES     |     NO


Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!