I have this method where I paint some text over an image:
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
Typeface mFace = Typeface.createFromAsset(this.getAssets(),"fonts/progbot.ttf");
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setTypeface(mFace);
paint.setTextSize(30);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
This is how I call this method:
lineIconView.setImageDrawable(writeOnDrawable(R.drawable.icon_line_numbre, linea.getNumero()));
This ImageView (lineIconView) already has the R.drawable.icon_line_numbre resource set. If I don't call the writeOnDrawable function then the image is shown in its original size, but after calling it the image gets heavily reduced in size.
Is this normal behavior?
Turns out I was using the wrong constructor for BitmapDrawable.
The official documentation says the following on this matter:
BitmapDrawable(Bitmap bitmap) This constructor is deprecated. Use
BitmapDrawable(Resources, Bitmap) to ensure that the drawable has
correctly set its target density.
So I ended up using:
return new BitmapDrawable(bm);
Related
I am trying to build a simple game on android and while setting the background image and other image assets on my main screen it is appearing too big as shown in the below image.
The actual size of the image is 1920x1080 and I want it to fit my screen like the image shown below:
The code for I have used is:
public class PoliceView extends View {
private Bitmap police;
private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];
public PoliceView(Context context) {
super(context);
police = BitmapFactory.decodeResource(getResources(),R.drawable.police);
background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);
scorePaint.setColor(Color.BLACK);
scorePaint.setTextSize(70);
scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
scorePaint.setAntiAlias(true);
life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//The order of draw matters as objects are drawn in this same order
canvas.drawBitmap(background,0,0,null);
canvas.drawBitmap(police, 0, 0, null);
canvas.drawText("Score:",20, 60, scorePaint);
//Three lives for the police
canvas.drawBitmap(life[0],580, 10, null);
canvas.drawBitmap(life[0],680, 10, null);
canvas.drawBitmap(life[0],780, 10, null);
}}
How can I resize the images to fit the screen??
i use this code for resize image
Bitmap tmp = BitmapFactory.decodeFile(mPath);
ResizeWidth = (int) (your size);
ResizeHeight = (int) (your size);
Bitmap bitmap = Bitmap.createBitmap(ResizeWidth, ResizeHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Bitmap scaled = Bitmap.createScaledBitmap(tmp, ResizeWidth, ResizeHeight, true);
int leftOffset = 0;
int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
How to resize bitmap in canvas?
canvas.save(); // Save current canvas params (not only scale)
canvas.scale(scaleX, scaleY);
canvas.restore(); // Restore current canvas params
scale = 1.0f will draw the same visible size bitmap
My requirement is, need to crop the image in circle shape and save as new image. For that, i have cropped the bitmap image using DrawRoundRect method of canvas in Android.
After cropped the image as circle and saving in PNG format, image background is transparent. But if i saved in JPEG format, image background was black. Please find my code
RoundedBitmap(Bitmap bitmap)
{
Bitmap roundBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(roundBitmap);
Paint paint = new Paint();
paint.AntiAlias = true;
RectF rectF = new RectF(0, 0, bitmap.Width, bitmap.Height);
canvas.DrawRoundRect(rectF, bitmap.Width / 2, bitmap.Height / 2, paint);
paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
canvas.DrawBitmap(bitmap, 0, 0, paint);
return roundedBitmap;
}
Using canvas.drawColor(Color.WHITE); not helped. I need transparent background color for JPEG format. Is it possible.?
Regards,
Bharathi.
PNG support transparent background while JPG doesn't . So you could create a white image, and then paint the original onto it.
public static void convertBitmapToJpg(Bitmap bitmap, string newImgpath)
{
Bitmap outB = bitmap.Copy(Bitmap.Config.Argb8888, true);
Canvas canvas = new Canvas(outB);
canvas.DrawColor(Color.White);
canvas.DrawBitmap(bitmap, 0, 0, null);
Java.IO.File file = new Java.IO.File(newImgpath);
try
{
MemoryStream outFile = new MemoryStream();
if (outB.Compress(Bitmap.CompressFormat.Jpeg, 100, outFile))
{
outFile.Flush();
outFile.Close();
}
}
catch (System.IO.FileNotFoundException e)
{
}
catch (System.IO.IOException e)
{
}
}
When I get Drawable from Image from one white pixel (1×1) Texture, its work fine, but I can't change its color. Why?
public static Drawable getDrawable(Texture texture, Color color) {
Image image = new Image(texture);
image.setColor(color);
return image.getDrawable();
}
When I call it:
// can't change color!! ==> still white (default)
getDrawable(pixel, new Color(0, 1f, 0, 0.5f));
Anyone Can advice me :).
The only Drawable types that support having their own color are SpriteDrawable and NinePatchDrawable, and that is because they wrap Sprites and NinePatches, which both have color parameters.
public static Drawable getTintedDrawable(Texture texture, Color color) {
Sprite sprite = new Sprite(texture);
sprite.setColor(color);
return new SpriteDrawable(sprite);
}
If you have an existing TextureRegionDrawable or NinePatchDrawable, you can call tint() on it to generate a new Drawable instance with the color you want.
My application is downloading a bitmap which is later populated into custom ImageView. However I want this bitmap to be updated before it is actually drawn on a canvas (I want to add some points over it). However as the result I see only populated bitmap without changes I made. Can anyone help?
Here is my onDraw method. "bitmap" object is set via setImageBitmap received from AsyncTask in onPostExecute() method.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap != null) {
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int padding = (width - bitmap.getWidth()) / 2;
canvas.drawBitmap(bitmap, padding, 0, null);
paint.setColor(Color.RED);
paint.setStrokeWidth(5f);
for (Face face : getPhotoObject().getFaces()) {
canvas.drawPoint(face.getLeftEye().x, face.getLeftEye().y, paint);
canvas.drawPoint(face.getRightEye().x, face.getRightEye().y, paint);
}
}
}
You can create a canvas with a bitmap at any time:
Canvas canvas = new Canvas(myBitmap);
// draw code
// the resulting bitmap will be edited.
Hope this helps
I get 3 warning for object allocation during draw/layout
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint textPaint = new Paint();
textPaint.setARGB(50,100,100,250);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(50);
textPaint.setTypeface(font);
canvas.drawText("Logan is awesom",canvas.getWidth()/2,200,textPaint);
canvas.drawBitmap(pBall, (canvas.getWidth()/2), changingY, null);
if (changingY <canvas.getHeight()){
changingY += 10;
}else{
changingY=0;
}
Rect middleRect = new Rect();
middleRect.set(0, 400, canvas.getWidth(), 550);
Paint ourBlue = new Paint();
ourBlue.setColor(Color.BLUE);
canvas.drawRect(middleRect, ourBlue);
I get an error on new Rect();
and on both new Paint();
The exact error is avoid object allocation during draw/layout operations (prelocate and reuse instead)
Well, your 'error' points to exact problem. onDraw() method is called by OS many-many times, thus allocating something within this function is extremely bad idea.
You need to allocate your Rect and Paint beforehand and just use them within onDraw
class YourClass extends View
{
Rect middleRect;
Paint ourBlue;
Paint textPaint;
public YourClass()
{
//constructor
init();
}
private void init()
{
middleRect = new Rect();
ourBlue; = new Paint();
textPaint = new Paint();
ourBlue.setColor(Color.BLUE);
textPaint.setARGB(50,100,100,250);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(50);
textPaint.setTypeface(font);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawText("Logan is awesom",canvas.getWidth()/2,200,textPaint);
canvas.drawBitmap(pBall, (canvas.getWidth()/2), changingY, null);
if (changingY <canvas.getHeight()){
changingY += 10;
}else{
changingY=0;
}
//if canvas size doesn't change - this can be moved to init() as well
middleRect.set(0, 400, canvas.getWidth(), 550);
canvas.drawRect(middleRect, ourBlue);
}
}
For me, I have got this error in layout especially when using display metrices.
I have used this imageview :
<com.steve.thirdparty.ScalableImageView
android:id="#+id/iv_posted_img_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:background="#drawable/golive_load_image"
android:layout_below="#+id/tv_user_posted_msg_post_items_home"
android:contentDescription="#string/cont_desc"/>
ScalableImageView.java:
import static com.steve.TabHomeActivity.displaymetrics;
public class ScalableImageView extends ImageView {
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
}
I solved this issue by adding the static DisplayMetrices in onCreate() method in Activity.
TabHomeActivity.java:
public static DisplayMetrics displaymetrics;
*inside onCreate()*
displaymetrics = new DisplayMetrics();
avoid object allocation during draw/layout operations (prelocate and reuse instead)
The message give You a solution on plate, so I don't understand what is Your question.
You have to move creation of new objects to onCreate method, so they are created just once and use them later in onDraw method.