0
我正在使用seekbar縮放圖像。圖像被縮放到一個特定的大小,無論您採用seekbar並將其縮放爲一次,下一次您更改seekbar的進度時,圖像保持相同的更改大小。我想隨着seekbar進度的增加或減少而動態縮放它。通過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);
}
此行後qrImage.setImageDrawable(result);嘗試把qrImage.requestLayout()。 –
你根本不需要縮放位圖,使用scaleType = matrix和setImageMatrix方法 – pskink