How to stretch an image using Seekbar in Android
Use below code for resize or stretch your image using seekbar
Create an seek bar in xml with an image view on top
Initialize the seekbar and image view with image in your java class
This worked for me.
- android:id="@+id/progress"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:progress="50" />
- SeekBar seek_bar = (SeekBar) findviewById(R.id.progress);
- ImageView img = (ImageView) findviewById(R.id.image);
- private static final int WIDTH_SCALE_RATIO = 10;
- private static final int HEIGHT_SCALE_RATIO = 10;
- private int previousProcess = 0;
- seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0) {
- }
- @Override
- public void onProgressChanged(SeekBar seekBar,
- int progresValue, boolean fromUser) {
- int diff = progresValue - previousProcess;
- scaleImage(img, diff);
- previousProcess = progresValue;
- }
- });
- }
- public void scaleImage(ImageView img, int scale) {
- Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();
- float width = bitmap.getWidth();
- float height = bitmap.getHeight();
- width += scale * WIDTH_SCALE_RATIO;
- height += scale * HEIGHT_SCALE_RATIO;
- bitmap = Bitmap.createScaledBitmap(bitmap, (int) width, (int) height,
- true);
- img.setImageBitmap(bitmap);
- }
- }
Articoli simili
- How to detect an object from static image and crop it from the image using openCV
- To set an image to fill 50% of the screen on all Android devices, what size of image should I keep in a resource folder?
- How to add a GIF image using HTML code
- How to make a to-do-list using PHP and connecting it using MySQL