Casa > H > How To Stretch An Image Using Seekbar In Android

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.

  1. android:id="@+id/progress" 
  2. android:layout_width="match_parent" 
  3. android:layout_height="wrap_content" 
  4. android:gravity="center" 
  5. android:progress="50" /> 
  6.  
  7. SeekBar seek_bar = (SeekBar) findviewById(R.id.progress); 
  8. ImageView img = (ImageView) findviewById(R.id.image); 
  9.  
  10. private static final int WIDTH_SCALE_RATIO = 10; 
  11. private static final int HEIGHT_SCALE_RATIO = 10; 
  12. private int previousProcess = 0; 
  13.  
  14.  
  15. seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
  16. @Override 
  17. public void onStopTrackingTouch(SeekBar arg0) { 
  18.  
  19. @Override 
  20. public void onStartTrackingTouch(SeekBar arg0) { 
  21.  
  22. @Override 
  23. public void onProgressChanged(SeekBar seekBar, 
  24. int progresValue, boolean fromUser) { 
  25. int diff = progresValue - previousProcess; 
  26. scaleImage(img, diff); 
  27. previousProcess = progresValue; 
  28. }); 
  29.  
  30. public void scaleImage(ImageView img, int scale) { 
  31. Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap(); 
  32. float width = bitmap.getWidth(); 
  33. float height = bitmap.getHeight(); 
  34. width += scale * WIDTH_SCALE_RATIO; 
  35. height += scale * HEIGHT_SCALE_RATIO; 
  36. bitmap = Bitmap.createScaledBitmap(bitmap, (int) width, (int) height, 
  37. true); 
  38. img.setImageBitmap(bitmap); 

De Chobot

É seguro comprar o iPhone da Apple em ofertas Paytm? :: O kernel ajuda a fazer overclock no processador do Android Phones?