package com.tkraindesigns.gameutility; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import java.util.ArrayList; /** * Holds an array of {@link Bitmap} for animations. Used to select specific graphics. Implements * methods to scale and flip bitmaps as they are loaded. */ public class BitmapBuffer { /** * The actual buffer */ private ArrayList buffer = new ArrayList<>(); /** * Holds an array of {@link Bitmap} for animations. Used to select specific graphics. Implements * methods to scale and flip bitmaps as they are loaded. */ public BitmapBuffer() { super(); buffer.clear(); } /** * Adds a {@link Bitmap} to the existing buffer, read from the system resources. * @param context - Theapplication Context provided to your custom SurfaceView * @param resid - The R.drawable.bitmapname * @return An integer index into the buffer. */ @SuppressWarnings("unused") public int AddBitMapFromRes(Context context,int resid) { Bitmap tempbitmap; tempbitmap= BitmapFactory.decodeResource(context.getResources(),resid); buffer.add(tempbitmap); return buffer.size()-1; } /** * Adds bitmap to the existing buffer from R.drawable.resourcename. Scales image to x/y * @param context The application context provided to your custom SurfaceView * @param resid the R.drawable.bitmapname * @param x Width to scale image * @param y Height to scale image * @return An integer index into the buffer. */ @SuppressWarnings("unused") public int AddandScaleBitmapFromRes(Context context, int resid,int x, int y) { Bitmap tempbitmap; tempbitmap= BitmapFactory.decodeResource(context.getResources(),resid); if (x>0) //only rescale if it's valid to do so { if (y > 0) { tempbitmap = Bitmap.createScaledBitmap(tempbitmap, x, y, true); } } buffer.add(tempbitmap); return buffer.size()-1; } /** * Adds bitmap to the existing buffer from R.drawable.resourcename. Scales image to x/y * and flips the image right to left. Useful for creating direction. * @param context The application context provided to your custom SurfaceView * @param resid the R.drawable.bitmapname * @param x Width to scale image * @param y Height to scale image * @return An integer index into the buffer. */ @SuppressWarnings("unused") public int AddandScaleFlippedBitmapFromRes(Context context,int resid,int x, int y) { Bitmap tempbitmap; Matrix matrix = new Matrix(); matrix.setScale(-1,1); tempbitmap=BitmapFactory.decodeResource(context.getResources(),resid); if (x>0) { if (y > 0) { tempbitmap = Bitmap.createScaledBitmap(tempbitmap, x, y, true); } } tempbitmap = Bitmap.createBitmap(tempbitmap, 0, 0, tempbitmap.getWidth(), tempbitmap.getHeight(), matrix, true); buffer.add(tempbitmap); return buffer.size()-1; } /** * Returns {@link Bitmap} from the buffer * @param i The specific bitmap you want * @return a bitmap */ public Bitmap get(int i) { if (i0) { if (y > 0) { tempbitmap = Bitmap.createScaledBitmap(bitmap, x, y, true); } } buffer.add(tempbitmap); return buffer.size()-1; } /** * Adds {@link Bitmap} to buffer and flips direction. * @param bitmap The bitmap to be added * @return Index into the buffer */ @SuppressWarnings("unused") public int AddFlippedBitmap(Bitmap bitmap) { Bitmap tempbitmap; Matrix matrix; matrix=new Matrix(); matrix.setScale(-1,1); tempbitmap=Bitmap.createBitmap(bitmap, 0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); buffer.add(tempbitmap); return buffer.size()-1; } /** * Adds {@link Bitmap} to buffer, scales it, and flips direction * @param bitmap Bitmap to add to buffer * @param x Width to scale * @param y Height to scale * @return Index into buffer */ @SuppressWarnings("unused") public int AddandScaleFlippedBitmap(Bitmap bitmap, int x, int y) { Bitmap tempbitmap; Matrix matrix; matrix=new Matrix(); matrix.setScale(-1,1); tempbitmap = Bitmap.createScaledBitmap(bitmap, x, y, true); tempbitmap=Bitmap.createBitmap(tempbitmap, 0, 0, tempbitmap.getWidth(), tempbitmap.getHeight(), matrix, true); buffer.add(tempbitmap); return buffer.size()-1; } /** * Converts the current buffer into a scaled buffer... Useful to load bitmaps at their native * resolutions, and then scale them to the resolution you'll be using them in. Ideally, pre * scaled bitmaps are faster than bitmaps that need to be scaled in the draw() phase. * Note that this does nothing to this buffer, but instead returns a new copy of the buffer with the * scaling applied. (i.e. this is a non-destructive method * @param x desired width * @param y desired height * @return A scaled copy of the buffer */ @SuppressWarnings("unused") public BitmapBuffer scaleBuffer(int x, int y) { BitmapBuffer temp = new BitmapBuffer(); for (int i=0;i