我正在使用搜索栏来缩放图像。图像被缩放到一个特定的大小,无论您在哪里使用搜索栏并缩放一次,下次您更改搜索栏的进度时,图像仍保持相同的更改大小。我想随着搜索栏进度的增加或减少动态地缩放它。
搜索条码片段
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);
}
});
缩放图像的功能
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);
}
制作边界框的功能
private int dpToPx(int dp) {
float density = getActivity().getApplicationContext().getResources()
.getDisplayMetrics().density;
return Math.round((float) dp * density);
}