问题
I am using seekbar to scale the image. The image is scaled to one specific size, where ever you take the seekbar and its scaled for the once, next time you change the progress of seekbar the image remains in same changed size. I want to scale it dynamically with the increase or decrease of seekbar progress.
Seekbar code snippet
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
// TODO Auto-generated method stub
Log.i("Test", "Progress value = " + Integer.toString(progresValue));
Log.i("Test", "Image width = " + Integer.toString(width));
Log.i("Test", "Image height = " + Integer.toString(height));
scaleImage(image, width, height);
}
});
Function to scale image
public void scaleImage(Bitmap bitmap, int w, int h) {
// Get current dimensions AND the desired bounding box
int bounding = dpToPx(150);
Log.i("Test", "original width = " + Integer.toString(w));
Log.i("Test", "original height = " + Integer.toString(h));
Log.i("Test", "bounding = " + Integer.toString(bounding));
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) bounding) / w;
float yScale = ((float) bounding) / h;
float scale = (xScale <= yScale) ? xScale : yScale;
Log.i("Test", "xScale = " + Float.toString(xScale));
Log.i("Test", "yScale = " + Float.toString(yScale));
Log.i("Test", "scale = " + Float.toString(scale));
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the
// ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
true);
sWidth = scaledBitmap.getWidth(); // re-use
sHeight = scaledBitmap.getHeight(); // re-use
@SuppressWarnings("deprecation")
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(sWidth));
Log.i("Test", "scaled height = " + Integer.toString(sHeight));
qrImage.setImageDrawable(result);
}
Function to make a bounding box
private int dpToPx(int dp) {
float density = getActivity().getApplicationContext().getResources()
.getDisplayMetrics().density;
return Math.round((float) dp * density);
}
回答1:
use below code for resize your image using seekbar
public class MyActivity extends Activity {
private static final int WIDTH_SCALE_RATIO = 10;
private static final int HEIGHT_SCALE_RATIO = 10;
private int previousProcess = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView img = (ImageView) findViewById(R.id.img);
((SeekBar) findViewById(R.id.seekBar))
.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);
}
}
来源:https://stackoverflow.com/questions/24968514/scale-bitmap-through-seekbar