Created
July 27, 2016 21:08
-
-
Save Domacoski/743619ad30fcc8e83cb6dca1a781becb to your computer and use it in GitHub Desktop.
Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.LayerDrawable; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
public class DrawUtils | |
{ | |
private static final float BASE_DIP_MARKER_WIDTH = 42.f; | |
private static final float BASE_DIP_MARKER_HEIGHT = 42.f; | |
private DrawUtils(){} | |
public static Drawable criarProfileMarker(Drawable marker, Drawable foto) | |
{ | |
return new LayerDrawable(new Drawable[]{marker, foto}); | |
} | |
public static BitmapDrawable criarMarkerFotoDrawable(Resources resources, Bitmap foto) | |
{ | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
int dipWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | |
BASE_DIP_MARKER_WIDTH, metrics); | |
int dipHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | |
BASE_DIP_MARKER_HEIGHT, metrics); | |
Bitmap redimencionado = Bitmap.createScaledBitmap(foto, dipWidth, dipHeight, true); | |
return new BitmapDrawable(resources, redimencionado); | |
} | |
public static BitmapDrawable criarMarkerFotoDrawable(Resources resources, Bitmap foto, final DisplayMetrics metrics) | |
{ | |
int dipWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | |
BASE_DIP_MARKER_WIDTH, metrics); | |
int dipHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | |
BASE_DIP_MARKER_HEIGHT, metrics); | |
Bitmap redimencionado = Bitmap.createScaledBitmap(foto, dipWidth, dipHeight, true); | |
return new BitmapDrawable(resources, redimencionado); | |
} | |
public static byte[] criarPNGByteArray(Bitmap bitmap) | |
{ | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); | |
return stream.toByteArray(); | |
} | |
public static Bitmap decodificarBitmap(byte[] data) | |
{ | |
ByteArrayInputStream input = new ByteArrayInputStream(data); | |
return BitmapFactory.decodeStream(input); | |
} | |
public static void drawCirculoPrecisao(Canvas canvas, float raio, int x, int y) | |
{ | |
Paint paint = new Paint(); | |
paint.setAntiAlias(true); | |
//preenchimento | |
paint.setColor(Color.GRAY); | |
paint.setAlpha(90); | |
paint.setStyle(Paint.Style.FILL); | |
canvas.drawCircle(x, y, raio, paint); | |
//borda | |
paint.setColor(Color.GRAY); | |
paint.setAlpha(255); | |
paint.setStyle(Paint.Style.STROKE); | |
paint.setStrokeWidth(1); | |
canvas.drawCircle(x, y, raio, paint); | |
} | |
public static Bitmap desenharStaticMapMarker(Bitmap mapa, Drawable marker) | |
{ | |
Bitmap bitmap = mapa.copy(mapa.getConfig(), true); | |
Canvas canvas = new Canvas(bitmap); | |
int centerX = bitmap.getWidth() / 2; | |
int centerY = bitmap.getHeight() / 2; | |
Rect bounds = drawableCentralizarInferior(marker); | |
Rect padding = new Rect(); | |
marker.getPadding(padding); | |
marker.setBounds(centerX + bounds.left, centerY + bounds.top, | |
centerX + bounds.right, centerY + bounds.bottom); | |
marker.draw(canvas); | |
return bitmap; | |
} | |
private static Rect drawableCentralizarInferior(Drawable drawable) | |
{ | |
int width = drawable.getMinimumWidth(); | |
int height = drawable.getMinimumHeight(); | |
return new Rect(-(width/2), -(height - 1), (width/2), 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment