animationDrawable only displays last frame - java

I'm trying to create an animation out of multiple png images. Here's my code:
AnimationDrawable animation = new AnimationDrawable();
for (int i = 0; i < translate_text.length(); i++)
{
byte[] byteArray = Base64.getDecoder().decode(client._fromServer.elementAt(i));
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.sign);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
animation.addFrame(image.getDrawable(), 1000);
}
animation.setOneShot(true);
animation.start();
but this only displays the last frame... Any ideas?
Edit: Probably should've done this earlier, but here goes:
translate_text is a string. It represents the image sequence. For example if the string is "bob" then there should be 3 images: the letter B, the letter O and the letter B.
client._fromServer is a vector of strings. Each string is the image itself encoded in base64. That's why client._fromServer.elementsAt(i) is a string that needs to be decoded and turned into byteArray.

I think it is because you get the Drawable from the same ImageView.
When you do image.setImageBitmap() it updates the reference of the Drawable in the ImageView and the AnimationDrawable gets affected also.
You should use a different Drawable instance for each addFrame call.
Something like that:
AnimationDrawable animation = new AnimationDrawable();
ImageView image = (ImageView) findViewById(R.id.sign);
for (int i = 0; i < translate_text.length(); i++)
{
byte[] byteArray = Base64.getDecoder().decode(client._fromServer.elementAt(i));
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
final Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false);
Drawable drawable = new BitmapDrawable(getResources(), scaledBitmap);
animation.addFrame(drawable, 1000);
}
animation.setOneShot(true);
animation.start();

Related

Android: How to make a percentage of a bitmap black and white, retaining color in the rest?

I've got a bitmap displayed in an ImageView, and I want to be able to make a certain percentage of the image black and white, and have the other part retain it's color. For example, if 60% is the target percentage, the image would look like this: . Thanks.
I've got a bitmap displayed in an ImageView, and I want to be able to
make a certain percentage of the image black and white, and have the
other part retain it's color. For example, if 60% is the target
percentage.
It seems (from your image) you mean monochrome (i.e. greyscale), not black and white.
Something like this should do it (tested o.k.):
void doIt(ImageView image)
{
//get bitmap from your ImageView (image)
Bitmap originalBitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
int height = originalBitmap.getHeight();
int fortyPercentHeight = (int) Math.floor(height * 40.0 / 100.0);
//create a bitmap of the top 40% of image height that we will make black and white
Bitmap croppedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth() , fortyPercentHeight );
//make it monochrome
Bitmap blackAndWhiteBitmap = monoChrome(croppedBitmap);
//copy the monochrome bmp (blackAndWhiteBitmap) to the original bmp (originalBitmap)
originalBitmap = overlay(originalBitmap, blackAndWhiteBitmap);
//set imageview to new bitmap
image.setImageBitmap(originalBitmap );
}
Bitmap monoChrome(Bitmap bitmap)
{
Bitmap bmpMonochrome = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bmpMonochrome;
}
Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
Bitmap bmp3 = bmp1.copy(Bitmap.Config.ARGB_8888,true);//mutable copy
Canvas canvas = new Canvas(bmp3 );
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmp3 ;
}

Draw in a canvas and save it to a larger image

I have a requirement, like drawing something in canvas and saving it to a larger image. As of now whatever I draw inside onDraw() method and save, it gives device provided image/canvas size, say something around 538(w)/852(h). I need image of almost double size, around 1000(w)/1500(h) without losing resolution. Any sample code, reference link would help definitely. Thanks in advance.
One way is to create Bitmap of desired size and draw on it using canvas:
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas canvas = new Canvas(bmp);
Finally you will have you bmp with your drawing and necessary dimentions.
EDIT::
#Override
protected void onDraw(Canvas canvas) {
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas mycanvas = new Canvas(bmp);
super.onDraw(mycanvas);
...
}
First get bitmap of your view using the function getDrawingCache(), then scale the bitmap as per your requirements.
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
//bitmap = bitmap.createScaledBitmap(..as your requirements..);
EDIT
You are saying that you want image in higher quality. You can't simply get a higher quality image from a lower quality image. However you can apply bitmap filtering to get slightly better quality.
Bitmap bitmap = getDrawingCache();
Bitmap output = Bitmap.createBitmap(/*width, height, bla bla bla*/);
Canvas canvas = new Canvas(output);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
Rect srcRect = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight());
Rect destRect = new Rect(0,0,output.getWidth(), output.getHeight());
canvas.drawBitmap(bitmap, srcRect, destRect, paint);

Android Studio: How to resize an image before uploading to server?

I have an app that takes a picture, assigns it to an imageview and then uploads it to a parse server.
The problem is that I need the images to be 200 x 200 px before upload.
Is there a way to do this programatically?
this is my function so far:
public void Myfunction (){
//image
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] imageInByte = baos.toByteArray();
ParseFile file = new ParseFile("myimage.jpg",imageInByte);
Could anyone help me please?
To scale bitmap down before compress, add
bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false));
If your bitmap is not square, you probably want to crop it before scaling

How to convert the view into bitmap without adding into the UI in Android

I am using View in android and I need to convert this to Bitmap without adding this into the activity.
view.setDrawingCacheEnabled(true);
Bitmap bitmap= view.getDrawingCache();
It returns bitmap as null.
Also I have tried the method view.buildDrawingCache() but still getDrawingCache() return null.
Thanks in advance.
First you need to create empty bitmap and get canvas from it.
use below code for this
int w = WIDTH_PX, h = HEIGHT_PX;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
Now you need to draw you view in this bitmap.
use below code for this
LinearLayout layout = new LinearLayout(getContext());
TextView textView = new TextView(getContext());
int padding = 4;
textView.setPadding(padding, padding, padding, padding);
textView.setVisibility(View.VISIBLE);
textView.setText("Hello how are you");
layout.addView(textView);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
layout.draw(canvas);
now your view is converted into the bitmap (bmp).

How to convert byteArray into bitmap image to show in an imageview in Android?

I have the following code for this purpose in my app but it does nothing, and halts the application:
bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
Try like this
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Then I suppose you draw the image too like this:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bmImage, 0, 0, paint);
use below line to convert bytes into Bitmap, it is working for me.
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.
P.S. :- here imageData is bytes array of Image
Try this :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 95, blob);
byte[] bitmapdata = blob.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
ImageView image = (ImageView) findViewById(R.id.ivPhoto);
image.setImageBitmap(bmp);

Categories