Giter VIP home page Giter VIP logo

Comments (6)

ja2375 avatar ja2375 commented on August 16, 2024 1

This is what i did exactly:

  1. First of all, in LoadBitmapTask class i changed the getBitmap() method to look as follows:
public Bitmap getBitmap(int number) {
        return getNextBitmap(number);
}

Note that i removed all the code that is supposed to run in another thread as it is not really needed.

  1. In that same class, change the getRandomBitmap() method to what i wrote in my last comment but leaving the array bidimensional as you mentioned. That should work.
private Bitmap getNextBitmap(int number) {
        int resId = mPortraitBGs[mBGSizeIndex][number - 1];
        Bitmap b = BitmapFactory.decodeResource(mResources, resId);
        if (mIsLandscape) {
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap lb = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                    matrix, true);
            b.recycle();
            return lb;
        }

        return b;
    }
  1. In SinglePageRender as well as in DoublePagesRender classes, in the drawPage(int number) method change the line Bitmap background = LoadBitmapTask.get(mContext).getBitmap(); to Bitmap background = LoadBitmapTask.get(mContext).getBitmap(number); and you should be good to go.

Note that at this point the IDE will complain at compile time because there is another call to getBitmap() method that actually needs an argument. But, as i said before, i deleted all the code that runs in another thread so this call got deleted as well.

I leave here my LoadBitmapTask class so you can take a look in case something is not clear at all:

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

public final class LoadBitmapTask {

    private final static String TAG = "LoadBitmapTask";
    private static LoadBitmapTask __object;

    private int mQueueMaxSize;
    private Resources mResources;
    private int[] mPortraitBGs;
    private boolean mIsLandscape;

    public static LoadBitmapTask get(Context context) {
        if (__object == null) {
            __object = new LoadBitmapTask(context);
        }
        return __object;
    }

    public static void destroyInstance(){
        if (__object != null)
            __object = null;
    }

    private LoadBitmapTask(Context context) {
        mResources = context.getResources();
        mIsLandscape = false;
        mQueueMaxSize = 1;

        // init all available bitmaps
        mPortraitBGs = new int[] {R.drawable.pag01, R.drawable.pag02, R.drawable.pag03,
                                    R.drawable.pag04, R.drawable.pag05, R.drawable.pag06,
                                    R.drawable.pag07, R.drawable.pag08, R.drawable.pag09,
                                    R.drawable.pag10, R.drawable.pag11, R.drawable.pag12,
                                    R.drawable.pag13, R.drawable.pag14, R.drawable.pag15,
                                    R.drawable.pag16, R.drawable.pag17, R.drawable.pag18,
                                    R.drawable.pag19, R.drawable.pag20, R.drawable.pag21,
                                    R.drawable.pag22, R.drawable.pag23, R.drawable.pag24,
                                    R.drawable.pag25, R.drawable.pag26, R.drawable.pag27,
                                    R.drawable.pag28, R.drawable.pag29, R.drawable.pag30,
                                    R.drawable.pag31, R.drawable.pag32, R.drawable.pag33,
                                    R.drawable.pag34};
    }

    /**
     * Acquire a bitmap to show
     * <p>If there is no cached bitmap, it will load one immediately</p>
     *
     * @return
     */
    public Bitmap getBitmap(int number) {
        return getNextBitmap(number);
    }

    /**
     * Set bitmap width , height and maximum size of cache queue
     *
     * @param w width of bitmap
     * @param h height of bitmap
     * @param maxCached maximum size of cache queue
     */
    public void set(int w, int h, int maxCached) {
        mIsLandscape = w > h;

        if (maxCached != mQueueMaxSize) {
            mQueueMaxSize = maxCached;
        }
    }

    /**
     * Load next image
     *
     * @param number
     * @return
     */
    private Bitmap getNextBitmap(int number) {
        int resId = mPortraitBGs[number - 1];
        Bitmap b = BitmapFactory.decodeResource(mResources, resId);
        if (mIsLandscape) {
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap lb = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                    matrix, true);
            b.recycle();
            return lb;
        }

        return b;
    }
}

Another thing to point out is that i made the mPortraitBGs array 1D and i changed images to drawable-nodpi folder so Android itself rescales the images according to device density which i believe by doing that images won't take a lot of space on memory when the app is running.

from android-pageflip.

sayansubhrabanerjee avatar sayansubhrabanerjee commented on August 16, 2024 1

That's an amazing answer!

I have understood the code snippet pretty well.

Along with these modifications I have disabled the following two lines from SampleActivity as well :

LoadBitmapTask.get(this).start(); //disabled this from onResume()
LoadBitmapTask.get(this).stop(); //disabled this from onPause()

I haven't modified anything else. And the code is working like a charm!!

Let me get back to you again if I come across any other issues on this.

Do you have any blogs ? I believe I can learn from that if you have any blogs / YouTube videos / or anything else.

from android-pageflip.

ja2375 avatar ja2375 commented on August 16, 2024

It took some time for me as well to figure that out.
In the PageRender class, the drawPage method takes an argument which represents the number of the page to draw. I achieved the order of the pages by passing that argument all the way to the getRandomBitmap method of the class LoadBitmapTask which i renamed and changed it a bit. Now it looks as follows:

/**
  * Load next image
  *
  * @param number of page to load
  * @return correctly ordered bitmap
  */
 private Bitmap getNextBitmap(int number) {
     int resId = mPortraitBGs[number - 1];
     Bitmap b = BitmapFactory.decodeResource(mResources, resId);
     if (mIsLandscape) {
         Matrix matrix = new Matrix();
         matrix.postRotate(90);
         Bitmap lb = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
                 matrix, true);
         b.recycle();
         return lb;
     }

     return b;
 }

This way the bitmap order should be the same as you have it in you initialization array.
Hope that helps :)

from android-pageflip.

sayansubhrabanerjee avatar sayansubhrabanerjee commented on August 16, 2024

Thanks for your input!
Based on your answer I need a few clarification:

  1. How are you passing this "number" argument of drawPage(int number) from Single/DoublePageRender to LoadBitmapTask ?

  2. From where should I call this new method getNextBitmap(int number) and what should be the argument value while calling this method ?

  3. As you have mentioned in this new method:

int resId = mPortraitBGs[number - 1];

But mPortraitBGs[][] is basically a 2D array.
So if I want to maintain 2D array like following:

int resId = mPortraitBGs[mBGSizeIndex][number - 1];

Will this work ?

It'd also be great if you could provide those modified class files. I could integrate them in my project and try understanding the code.

from android-pageflip.

ja2375 avatar ja2375 commented on August 16, 2024

Hey, sorry for late reply, I was really busy with work untill now. I'm glad the answer helped you :)

I do have a blog but it's in Spanish and i'm not posting too frequently nor it's about coding xD
No Youtube channels nor any social network. I don't really like them, sorry.

But if you run into any issues just let me know via email if you want and i will be happy to help. My email address is on my profile here.

Good luck!

from android-pageflip.

sayansubhrabanerjee avatar sayansubhrabanerjee commented on August 16, 2024

That's fine.

I'll mail you if I come across any issues. Thanks again. :)

from android-pageflip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.