Java源码示例:org.oscim.backend.canvas.Bitmap
示例1
@Override
public void query(MapTile tile, ITileDataSink sink) {
QueryResult res = FAILED;
try {
byte[] imageBytes = db.getTile(tile.tileX, tile.tileY, tile.zoomLevel);
if (transparentColor != null || alpha != null) {
android.graphics.Bitmap bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
if (transparentColor != null) {
bmp = ImageUtilities.makeTransparent(bmp, transparentColor);
}
if (alpha != null) {
bmp = ImageUtilities.makeBitmapTransparent(bmp, alpha);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, bos);
imageBytes = bos.toByteArray();
}
Bitmap bitmap = AndroidGraphics.decodeBitmap(new ByteArrayInputStream(imageBytes));
sink.setTileImage(bitmap);
res = QueryResult.SUCCESS;
} catch (Exception e) {
log.debug("{} Error: {}", tile, e.getMessage());
} finally {
sink.completed(res);
}
}
示例2
public void setBitmap(Bitmap bitmap, int w, int h, TexturePool pool) {
mWidth = w;
mHeight = h;
mBitmap = bitmap;
if (textures == null) {
if (pool == null)
textures = new TextureItem(mBitmap);
else {
textures = pool.get(mBitmap);
}
}
TextureItem t = textures;
t.indices = TextureBucket.INDICES_PER_SPRITE;
}
示例3
public synchronized Rect add(Object key, Bitmap image) {
Rect rect = new Rect(0, 0, image.getWidth(), image.getHeight());
if (rect.width > atlasWidth || rect.height > atlasHeight) {
if (key == null)
throw new RuntimeException("PackerAtlasItem size too small for Bitmap.");
throw new RuntimeException("PackerAtlasItem size too small for Bitmap: " + key);
}
PackerAtlasItem packerAtlasItem = packStrategy.pack(this, key, rect);
if (key != null) {
packerAtlasItem.rects.put(key, rect);
packerAtlasItem.addedRects.add(key);
}
int rectX = rect.x, rectY = rect.y, rectWidth = rect.width, rectHeight = rect.height;
packerAtlasItem.drawBitmap(image, rectX,
flipY ? packerAtlasItem.image.getHeight() - rectY - rectHeight : rectY);
return rect;
}
示例4
/**
* Load a texture from a specified location and optional dimensions.
*/
public static TextureItem loadTexture(String relativePathPrefix, String src, int width, int height, int percent, int color) {
if (src == null || src.length() == 0)
return null;
try {
Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent, color);
if (bitmap != null) {
log.debug("loading {}", src);
return new TextureItem(potBitmap(bitmap), true);
}
} catch (Exception e) {
log.error("{}: missing file / {}", src, e.getMessage());
}
return null;
}
示例5
private MarkerSymbol createAdvancedSymbol(MarkerItem item, Bitmap poiBitmap) {
int bitmapHeight = poiBitmap.getHeight();
int margin = 3;
int dist2symbol = (int) Math.round(bitmapHeight / 2.0);
int symbolWidth = poiBitmap.getWidth();
int xSize = symbolWidth;
int ySize = symbolWidth + dist2symbol;
// markerCanvas, the drawing area for all: title, description and symbol
Bitmap markerBitmap = CanvasAdapter.newBitmap(xSize, ySize, 0);
org.oscim.backend.canvas.Canvas markerCanvas = CanvasAdapter.newCanvas();
markerCanvas.setBitmap(markerBitmap);
markerCanvas.drawBitmap(poiBitmap, xSize * 0.5f - (symbolWidth * 0.25f), ySize * 0.5f - (symbolWidth * 0.25f));
return (new MarkerSymbol(markerBitmap, MarkerSymbol.HotspotPlace.CENTER, true));
}
示例6
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return new AndroidBitmap(((BitmapDrawable) drawable).getBitmap());
}
android.graphics.Bitmap bitmap = android.graphics.Bitmap
.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Config.ARGB_8888);
android.graphics.Canvas canvas = new android.graphics.Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return new AndroidBitmap(bitmap);
}
示例7
private SymbolStyle(Bitmap bitmap, TextureRegion texture, int hash) {
this.bitmap = bitmap;
this.texture = texture;
this.hash = hash;
this.src = null;
this.symbolWidth = 0;
this.symbolHeight = 0;
this.symbolPercent = 100;
this.symbolColor = 0;
this.billboard = false;
this.rotate = false;
this.inverse = false;
this.mandatory = false;
this.repeat = false;
this.repeatStart = 0;
this.repeatGap = 0;
this.mergeGap = -1;
this.mergeGroup = null;
this.mergeGroupGap = -1;
this.textOverlap = true;
}
示例8
private void createAtlas(String elementName, Attributes attributes) throws IOException {
String img = null;
for (int i = 0; i < attributes.getLength(); i++) {
String name = attributes.getLocalName(i);
String value = attributes.getValue(i);
if ("img".equals(name)) {
img = value;
} else {
logUnknownAttribute(elementName, name, value, i);
}
}
validateExists("img", img, elementName);
Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), img);
if (bitmap != null)
mTextureAtlas = new TextureAtlas(bitmap);
}
示例9
private MarkerItem createMarkerItem(Context appContext, GeoPoint p, int resource, float offsetX, float offsetY) {
// Drawable drawable = activity.getDrawable(resource); // Since API21
Drawable drawable = ContextCompat.getDrawable(appContext, resource);
Bitmap bitmap = AndroidGraphics.drawableToBitmap(drawable);
MarkerSymbol markerSymbol = new MarkerSymbol(bitmap, offsetX, offsetY);
MarkerItem markerItem = new MarkerItem("", "", p);
markerItem.setMarker(markerSymbol);
return markerItem;
}
示例10
private TextureItem getTexture(Bitmap bitmap) {
TextureItem t;
for (t = prevTextures; t != null; t = t.next) {
if (t.bitmap == bitmap) {
prevTextures = Inlist.remove(prevTextures, t);
textures = Inlist.appendItem(textures, t);
t.offset = 0;
t.indices = 0;
return t;
}
}
return null;
}
示例11
public void set(float x, float y, Bitmap bitmap, int hash, float rotation, boolean billboard, int mergeGap, String mergeGroup, int mergeGroupGap, boolean textOverlap) {
this.x = x;
this.y = y;
this.bitmap = bitmap;
this.hash = hash;
this.rotation = rotation;
this.billboard = billboard;
this.mergeGap = mergeGap;
this.mergeGroup = mergeGroup;
this.mergeGroupGap = mergeGroupGap;
this.textOverlap = textOverlap;
}
示例12
/**
* @param bitmap with dimension being power of two
* @param width width used
* @param height height used
*/
public synchronized void setBitmap(Bitmap bitmap, int width, int height) {
mBitmap = bitmap;
mWidth = width;
mHeight = height;
initialized = false;
}
示例13
public TextureAtlas(Bitmap bitmap) {
texture = new TextureItem(bitmap);
mWidth = texture.width;
mHeight = texture.height;
mRegions = new HashMap<Object, TextureRegion>();
}
示例14
public void sort(ArrayList<Bitmap> Bitmaps) {
if (comparator == null) {
comparator = new Comparator<Bitmap>() {
public int compare(Bitmap o1, Bitmap o2) {
return Math.max(o1.getWidth(), o1.getHeight()) - Math.max(o2.getWidth(), o2.getHeight());
}
};
}
Collections.sort(Bitmaps, comparator);
}
示例15
public void sort(ArrayList<Bitmap> images) {
if (comparator == null) {
comparator = new Comparator<Bitmap>() {
public int compare(Bitmap o1, Bitmap o2) {
return o1.getHeight() - o2.getHeight();
}
};
}
Collections.sort(images, comparator);
}
示例16
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is)
throws IOException {
Bitmap bitmap = CanvasAdapter.decodeBitmap(is);
if (!bitmap.isValid()) {
log.debug("{} invalid bitmap", tile);
return false;
}
sink.setTileImage(bitmap);
return true;
}
示例17
@Override
public void setTileImage(Bitmap bitmap) {
if (isCanceled() || !mTile.state(LOADING)) {
bitmap.recycle();
return;
}
BitmapBucket l = new BitmapBucket(false);
l.setBitmap(bitmap, Tile.SIZE, Tile.SIZE, mLayer.pool);
RenderBuckets buckets = new RenderBuckets();
buckets.set(l);
mTile.data = buckets;
}
示例18
public MarkerSymbol(Bitmap bitmap, HotspotPlace hotspot, boolean billboard) {
switch (hotspot) {
case BOTTOM_CENTER:
mOffset = new PointF(0.5f, 1);
break;
case TOP_CENTER:
mOffset = new PointF(0.5f, 0);
break;
case RIGHT_CENTER:
mOffset = new PointF(1, 0.5f);
break;
case LEFT_CENTER:
mOffset = new PointF(0, 0.5f);
break;
case UPPER_RIGHT_CORNER:
mOffset = new PointF(1, 0);
break;
case LOWER_RIGHT_CORNER:
mOffset = new PointF(1, 1);
break;
case UPPER_LEFT_CORNER:
mOffset = new PointF(0, 0);
break;
case LOWER_LEFT_CORNER:
mOffset = new PointF(0, 1);
break;
default:
mOffset = new PointF(0.5f, 0.5f);
}
mBitmap = bitmap;
mBillboard = billboard;
mTextureRegion = null;
}
示例19
public Bitmap getBitmap() {
int width = mSize, height = mSize;
width = width > 0 ? width : 1;
height = height > 0 ? height : 1;
Bitmap bitmap = CanvasAdapter.newBitmap(width, height, 0);
Canvas canvas = CanvasAdapter.newCanvas();
canvas.setBitmap(bitmap);
draw(canvas);
return bitmap;
}
示例20
/**
* Gets a bitmap for a given cluster size
*
* @param size The cluster size. Can be greater than CLUSTER_MAXSIZE.
* @return A somewhat cool bitmap to be used as the cluster marker
*/
protected Bitmap getClusterBitmap(int size) {
final String strValue;
if (size >= CLUSTER_MAXSIZE) {
// restrict cluster indicator size. Bigger clusters will show as "+" instead of ie. "45"
size = CLUSTER_MAXSIZE;
strValue = "+";
} else {
strValue = String.valueOf(size);
}
// return cached bitmap if exists. cache hit !
if (mClusterBitmaps[size] != null)
return mClusterBitmaps[size];
// create and cache bitmap. This is unacceptable inside the GL thread,
// so we'll call this routine at the beginning to pre-cache all bitmaps
ScreenUtils.ClusterDrawable drawable = new ScreenUtils.ClusterDrawable(
MAP_MARKER_CLUSTER_SIZE_DP - CLUSTER_MAXSIZE + size, // make size dependent on cluster size
mStyleForeground,
mStyleBackground,
strValue
);
mClusterBitmaps[size] = drawable.getBitmap();
return mClusterBitmaps[size];
}
示例21
@Override
public void drawBitmapScaled(Bitmap bitmap) {
android.graphics.Bitmap scaledBitmap = android.graphics.Bitmap.createScaledBitmap(
((AndroidBitmap) bitmap).mBitmap, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(scaledBitmap, 0, 0, null);
scaledBitmap.recycle();
}
示例22
@Override
SymbolStyle buildSymbol(SymbolBuilder<?> b, String src, Bitmap bitmap) {
// we need to hash with the width/height included as the same symbol could be required
// in a different size and must be cached with a size-specific hash
String absoluteName = CanvasAdapter.getAbsoluteFile(mTheme.getRelativePathPrefix(), src).getAbsolutePath();
int hash = (absoluteName + b.symbolWidth + b.symbolHeight + b.symbolPercent + b.symbolColor).hashCode();
bitmapMap.put(hash, bitmap);
return b.hash(hash).build();
}
示例23
@Override
SymbolStyle buildSymbol(SymbolBuilder<?> b, String src, Bitmap bitmap) {
// we need to hash with the width/height included as the same symbol could be required
// in a different size and must be cached with a size-specific hash
String absoluteName = CanvasAdapter.getAbsoluteFile(mTheme.getRelativePathPrefix(), src).getAbsolutePath();
int hash = (absoluteName + b.symbolWidth + b.symbolHeight + b.symbolPercent + b.symbolColor).hashCode();
bitmapMap.put(hash, bitmap);
return b.hash(hash).build();
}
示例24
public GpsPositionLayer(GPMapView mapView) throws IOException {
super(mapView.map());
getName(mapView.getContext());
peferences = PreferenceManager.getDefaultSharedPreferences(mapView.getContext());
this.mapView = mapView;
// set billboard rendering for TextureRegion (false is default)
locationRenderer.setBillboard(false);
Bitmap activeGpsBitmap = VtmUtilities.getBitmapFromResource(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_my_location_moving_24dp);
locationRenderer.setBitmapMarker(activeGpsBitmap);
locationRenderer.setBitmapArrow(activeGpsBitmap);
}
示例25
protected static Bitmap createBitmap(String relativePathPrefix, String src, int width, int height, int percent, int color) throws IOException {
if (src == null || src.length() == 0) {
// no image source defined
return null;
}
InputStream inputStream;
if (src.startsWith(PREFIX_ASSETS)) {
src = src.substring(PREFIX_ASSETS.length());
inputStream = inputStreamFromAssets(relativePathPrefix, src);
} else if (src.startsWith(PREFIX_FILE)) {
src = src.substring(PREFIX_FILE.length());
inputStream = inputStreamFromFile(relativePathPrefix, src);
} else {
inputStream = inputStreamFromFile(relativePathPrefix, src);
if (inputStream == null)
inputStream = inputStreamFromAssets(relativePathPrefix, src);
}
// Fallback to internal resources
if (inputStream == null) {
inputStream = inputStreamFromAssets("", src);
if (inputStream != null)
log.info("internal resource: " + src);
}
if (inputStream == null) {
log.error("invalid resource: " + src);
return null;
}
Bitmap bitmap;
if (src.toLowerCase(Locale.ENGLISH).endsWith(".svg"))
bitmap = decodeSvgBitmap(inputStream, width, height, percent, color);
else
bitmap = decodeBitmap(inputStream, width, height, percent, color);
inputStream.close();
return bitmap;
}
示例26
@Override
public void onStart() {
super.onStart();
mRouteLayer = new RouteLayer(mMapHolder.getMap(), Color.RED, 5, mRoute);
mMapHolder.getMap().layers().add(mRouteLayer);
Bitmap bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(getContext(), R.drawable.dot_black, Color.RED));
MarkerSymbol symbol = new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.CENTER);
ArrayList<MarkerItem> items = new ArrayList<>(mRoute.length());
for (GeoPoint point : mRoute.getCoordinates()) {
items.add(new MarkerItem(point, null, null, point));
}
mPointLayer = new ItemizedLayer<>(mMapHolder.getMap(), items, symbol, MapTrek.density, this);
mMapHolder.getMap().layers().add(mPointLayer);
}
示例27
private void addWaypointMarker(Waypoint waypoint) {
MarkerItem marker = new MarkerItem(waypoint, waypoint.name, waypoint.description, waypoint.coordinates);
if (waypoint.style.color != 0 && waypoint.style.color != MarkerStyle.DEFAULT_COLOR) {
Bitmap bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(this, waypoint.style.color));
marker.setMarker(new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.BOTTOM_CENTER));
}
mMarkerLayer.addItem(marker);
}
示例28
private static Bitmap getHillshadesBitmap(int color) {
Bitmap bitmap;
try {
bitmap = CanvasAdapter.getBitmapAsset("", "symbols/hillshades.svg", 0, 0, 70, 0);
} catch (IOException e) {
log.error("Failed to read bitmap", e);
return null;
}
android.graphics.Bitmap bitmapResult = android.graphics.Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), android.graphics.Bitmap.Config.ARGB_8888);
android.graphics.Canvas canvas = new android.graphics.Canvas(bitmapResult);
android.graphics.Paint paint = new android.graphics.Paint();
paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(AndroidGraphics.getBitmap(bitmap), 0, 0, paint);
return new AndroidBitmap(bitmapResult);
}
示例29
private void drawSymbol(Canvas canvas, String foreground, int size) {
if (foreground.length() == 0)
return;
Integer foregroundColor = null;
String symbol = null;
if (VALID_FOREGROUNDS.contains(foreground)) {
// foreground is encoded as symbol without color
logger.debug(" foreground: black {}", foreground);
if ("shell".equals(foreground) || "shell_modern".equals(foreground))
foregroundColor = Color.YELLOW;
else
foregroundColor = Color.BLACK;
symbol = foreground;
} else {
// foreground should contain coloured symbol
String[] foreground_parts = foreground.trim().split("\\s*_\\s*", 2);
logger.debug(" foreground: {}", Arrays.toString(foreground_parts));
if (foreground_parts.length == 2) {
foregroundColor = ColorsCSS.get(foreground_parts[0]);
if (VALID_FOREGROUNDS.contains(foreground_parts[1]))
symbol = foreground_parts[1];
}
if (BuildConfig.DEBUG && foregroundColor == null)
logger.error("Unknown foreground color: {}", foreground_parts[0]);
if (BuildConfig.DEBUG && symbol == null)
logger.error("Unknown foreground symbol: {}", foreground_parts.length == 1 ?
foreground_parts[0] : foreground_parts[1]);
}
if (foregroundColor != null && symbol != null) {
try {
Bitmap symbolBitmap = CanvasAdapter.getBitmapAsset("symbols/osmc", symbol + ".svg", size, size, 100, foregroundColor);
canvas.drawBitmap(AndroidGraphics.getBitmap(symbolBitmap), 0, 0, null);
} catch (IOException e) {
logger.error("Failed to load bitmap for " + symbol, e);
}
}
}
示例30
public void clear() {
synchronized (cache) {
for (Bitmap bitmap : cache.values())
bitmap.recycle();
cache.clear();
}
}