1

I want to control the horizontal crop of an ImageView using a SeekBar, like presented in this photo:

Crop controlled by a Seekbar

This is my code for the progressChanged event:

    cropSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            float tuning = (float) progress/seekBar.getMax();

            if (tuning == 0) {
                return;
            }

            int width = (int)(tuning * originalPhotoBitmap.getWidth());
            int height = originalPhotoBitmap.getHeight();

            modifiedPhotoBitmap = Bitmap.createBitmap(originalPhotoBitmap, 0, 0, width, height);

            imageView.setImageBitmap(modifiedPhotoBitmap);
        }
    });

The new width is calculated correctly (I checked the value in logcat), but when I set the imageView's bitmap with setImageBitmap it appears that the ScaleType FIT_CENTER isn't working anymore...

What I'm actually getting with my code is this:

Progress value: 100

Progress value is 100

Progress value: 50

Progress value is 50

Progress value: 25

Progress value is 25

As you can see, it is not the desired behavior...

My questions are two:

  1. What scale type should I use in the ImageView?
  2. Is my new bitmap defined correctly?
sports
  • 7,851
  • 14
  • 72
  • 129

3 Answers3

0

1- What scale type should I use in the ImageView? Not shure if i understood correctly but if you want to set the ImageView centralized you should use Gravity attribute.

2- Is my new bitmap defined correctly? I beleve the awnser here by Lumis should help you

Community
  • 1
  • 1
Gabriel Brito
  • 1,003
  • 2
  • 16
  • 26
0

The easiest way to do this isn't making new bitmaps- in fact that's the worst way to do it. The easiest way is to alter the width of the image view via its layout params

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • so the imageView width will not alter its content width(the actual photo)? the imageView width is, then, the container width? – sports Jul 31 '16 at 22:30
0

I hope this helps:

Bitmap getCroppedBitmap(Bitmap bitmap, int progress) {
    Bitmap output = Bitmap.createBitmap(
            bitmap.getWidth(),
            bitmap.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    canvas.drawRect(0, 0, bitmap.getWidth() * progress / 100, bitmap.getHeight(), paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

Use like this:

int progress = 70;
imageView.setImageBitmap(getCroppedBitmap(originalPhotoBitmap, progress));
nshmura
  • 5,940
  • 3
  • 27
  • 46
  • I keep my ImageView with FIT_CENTER? – sports Jul 31 '16 at 23:32
  • oh.. I posted sample to github https://github.com/nshmura/TestCrop . If this dose not works in your environment, Could you tell me your enviroment (Android OS version and you device) ? – nshmura Aug 01 '16 at 03:18