VLeonovs
VLeonovs

Reputation: 2251

Scale bitmap android and performance

Good Day, I have an Image which is loaded using Picasso the size is 1110x300px, I need to crop image to 550 x 300px, currently I'm trying to use Bitmap.createScaledBitmap() but this method only resize my image, not crop.

And there is another problem - bad performance.

My code:

 Runnable r = new Runnable(){
                @Override
                public void run() {
                    Picasso.with(getActivity()).load(builder.toString()).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        int width = Double.valueOf(bitmap.getWidth() * 0.5).intValue();
                        int height = bitmap.getHeight()


                        Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                        imageView.setImageBitmap(newBitmap);
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {}
                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {}

                    });
                  }
};
r.run();

The question: How to make my Bitmap scaled, not re-sized and how to increase performance of my code.

Thank you!

Upvotes: 0

Views: 146

Answers (1)

Robbie188
Robbie188

Reputation: 173

To answer one part of your question, to crop your bitmap you can use:

resizedBitmap = Bitmap.createBitmap(bitmap, 0,0,yourwidth, yourheight);

So you supply original bitmap, start X,start Y,width & height as parameters

Upvotes: 1

Related Questions