If you think the Android project droidengine2d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/* Copyright 2013-2014 Miguel Vicente Linares
*//www.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.miviclin.droidengine2d.resources;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.miviclin.droidengine2d.util.math.Vector2;
/**
* Utility class that can be used to load resources.
*
* @author Miguel Vicente Linares
*
*/publicclass AssetsLoader {
/**
* Private constructor. This class should not be instantiable.
*/private AssetsLoader() {
}
/**
* Returns an opened InputStream to read the specified asset.
*
* @param context Context.
* @param path File path. Relative to the assets folder.
* @return InputStream
* @throws IOException
*/publicstatic InputStream getAsset(Context context, String path) throws IOException {
return context.getAssets().open(path);
}
/**
* Returns an opened InputStream to read the specified asset.
*
* @param assetManager AssetManager.
* @param path File path. Relative to the assets folder.
* @return InputStream
* @throws IOException
*/publicstatic InputStream getAsset(AssetManager assetManager, String path) throws IOException {
return assetManager.open(path);
}
/**
* Loads a Bitmap from the assets folder.
*
* @param context Context.
* @param path File path. Relative to the assets folder.
* @return Bitmap
*/publicstatic Bitmap loadBitmap(Context context, String path) {
Bitmap bitmap = null;
InputStream in = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
try {
in = getAsset(context, path);
bitmap = BitmapFactory.decodeStream(in, null, options);
} catch (IOException e) {
thrownew RuntimeException("Unable to load '" + path + "'", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return bitmap;
}
/**
* Loads a Bitmap from the assets folder.
*
* @param assetManager AssetManager.
* @param path File path. Relative to the assets folder.
* @return Bitmap
*/publicstatic Bitmap loadBitmap(AssetManager assetManager, String path) {
Bitmap bitmap = null;
InputStream in = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
try {
in = getAsset(assetManager, path);
bitmap = BitmapFactory.decodeStream(in, null, options);
} catch (IOException e) {
thrownew RuntimeException("Unable to load '" + path + "'", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return bitmap;
}
/**
* Returns the bounds of the specified Bitmap. This method does not load the Bitmap into memory.
*
* @param context Context.
* @param path File path. Relative to the assets folder.
* @return Vector2 that contains the width and height of the specified Bitmap
*/publicstatic Vector2 getBitmapBounds(Context context, String path) {
InputStream in = null;
int width;
int height;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
in = getAsset(context, path);
BitmapFactory.decodeStream(in, null, options);
width = options.outWidth;
height = options.outHeight;
} catch (IOException e) {
thrownew RuntimeException("Unable to load '" + path + "'", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
returnnew Vector2(width, height);
}
/**
* Returns an AssetFileDescriptor that can be used to read the specified asset.
*
* @param context Context.
* @param path File path. Relative to the assets folder.
* @return AssetFileDescriptor
*/publicstatic AssetFileDescriptor getAssetFileDescriptor(Context context, String path) {
AssetFileDescriptor descriptor = null;
try {
descriptor = context.getAssets().openFd(path);
} catch (IOException e) {
e.printStackTrace();
}
return descriptor;
}
}