Search code examples
androidandroid-animation

android Position an image inside an ImageView animation


i have imageView

<ImageView
                android:id="@+id/loading"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:scaleType="centerCrop"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/spinner"/>

and image is enter image description here

i want create an animation which change position of image inside imageView

i tried this

android:scrollX="-80dp"

this in xml and static i want animation


Solution

  • You can do it by creating a thread and a handler where the thread sleeps for a certain amount of time and informs handler by sending message when it wakes up and handler will update the image in image view.

    Create 4 Fields like below :

    private ImageView mImageView;
    private Handler mImageChangingHandler;
    private int mCurrentImageIndex = 0;
    private BackgroundThread mImageUpdateThread;
    

    In onCreate() get the ImageView, spilt your bitmaps and add it to a list and create a handler which updates imageview. Here you should know the number of individual bitmaps which is in main Bitmap. Like :

    mImageView = (ImageView) findViewById(R.id.img);
    Bitmap mainBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.anim);
    //number of individual bitmaps, which you should know.
    final int numberOfImages = 21;
    //individual bitmap width
    int bitmapWidth = mainBitmap.getWidth() / numberOfImages;
    //individual bitmap height
    int bitmapHeight = mainBitmap.getHeight();
    //list which holds individual bitmaps.
    final List<Bitmap> animBitmaps = new ArrayList<>(numberOfImages);
    //split your bitmap in to individual bitmaps
    for (int index = 0; index < numberOfImages; index++) {
        animBitmaps.add(Bitmap.createBitmap(mainBitmap, index * bitmapWidth, 0, bitmapWidth, bitmapHeight));
    }
    //set 1st bitmap to imageView.
    mImageView.setImageBitmap(animBitmaps.get(mCurrentImageIndex));
    mImageChangingHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            //increment current bitmap index
            mCurrentImageIndex++;
            //if current index is greater than the number of images, reset it to 0
            if (mCurrentImageIndex > numberOfImages - 1) {
                mCurrentImageIndex = 0;
            }
                mImageView.setImageBitmap(animBitmaps.get(mCurrentImageIndex));
                return true;
        }
    });
    //Create the background thread by passing the handler and start.
    mImageUpdateThread = new BackgroundThread(mImageChangingHandler);
    mImageUpdateThread.setRunning(true);
    mImageUpdateThread.start();
    

    And This will be your BackgroundThread.class :

    public static class BackgroundThread extends Thread {
    
        private Handler mHandler;
        private boolean mRunning;
    
        public BackgroundThread(Handler handler) {
            mHandler = handler;
        }
    
        public void setRunning(boolean running) {
            mRunning = running;
        }
    
        @Override
        public void run() {
            super.run();
            while (mRunning) {
                try {
                    //this sleeps for 100 milliseconds
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //send message to handler after sleep
                    mHandler.sendEmptyMessage(0);
                }
            }
        }
    }
    

    Finally in onDestroy() stop the thread. Like :

    mImageUpdateThread.setRunning(false);
    boolean stopThread = true;
        while (stopThread) {
            try {
                mImageUpdateThread.join();
                //thread already stopped
                stopThread = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    mImageUpdateThread = null;
    

    Note: if you want to increase the update speed, reduce the sleep time in thread.