我正在使用搜索栏来缩放图像。图像缩放到一个特定大小,无论您采用搜索条还是缩放一次,下次更改搜索条的进度时图像保持相同的更改大小。我希望随着搜索栏进度的增加或减少而动态扩展它。
Seekbar代码段
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);
}
答案 0 :(得分:0)
使用以下代码使用搜索栏调整图片大小
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);
}
}