在我的android应用程序中,我有一个用ImageViews和textViews填充的列表。 imageViews是从SD卡动态加载的。我的问题是我有更高分辨率的图像,为了列表的目的,我需要它们像缩略图一样小得多。那么如何将图像缩放为像imageView?为图像缩放图像
Q
为图像缩放图像
0
A
回答
2
我希望下面这段代码是给你
public static Bitmap resizeImage(Bitmap orignal, int new_width,
int new_height) {
// load the origial BitMap
int width = orignal.getWidth();
int height = orignal.getHeight();
float scaleWidth = ((float) new_width)/width;
float scaleHeight = ((float) new_height)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(orignal, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
0
这里有用的是规模代码
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
int scale=1;
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=250; // here provide your max size required
int width_tmp=o.outWidth, height_tmp=o.outHeight;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
return bmp;
} catch (FileNotFoundException e) {}
return null;
}
0
此代码可以帮助扩展您的图像根据您的ImageView
private void scaleImage()
{
// Get the ImageView and its bitmap
ImageView view = (ImageView) findViewById(R.id.image_box);
Drawable drawing = view.getDrawable();
if (drawing == null) {
return; // Checking for null & return, as suggested in comments
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(250);
Log.i("Test", "original width = " + Integer.toString(width));
Log.i("Test", "original height = " + Integer.toString(height));
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)/width;
float yScale = ((float) bounding)/height;
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, width, height, matrix, true);
width = scaledBitmap.getWidth(); // re-use
height = scaledBitmap.getHeight(); // re-use
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(width));
Log.i("Test", "scaled height = " + Integer.toString(height));
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
Log.i("Test", "done");
}
private int dpToPx(int dp)
{
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
xml代码为ImageView
:
<ImageView a:id="@+id/image_box"
a:background="#ff0000"
a:src="@drawable/star"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_marginTop="20dp"
a:layout_gravity="center_horizontal"/>
礼貌:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
0
您也可以从图像视图中使用SETMAX:
imageView.setMaxHeight(myMaxHeight);
imageView.setMaxWidth(myMaxWidth);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_CENTER);
您还可以使用该
的XML属性,使用这种方法的问题是你设置的最大尺寸,所以一个较小的图像将不会填充图像视图
相关问题
- 1. 图像缩放为UITabBar背景图像
- 2. 缩放图像
- 3. 图像缩放
- 4. jquery缩放图像与图像地图
- 5. 图像缩放像素化
- 6. 缩放图像CSS
- 7. 缩放图像jQuery
- 8. imagecreatefromjpeg()缩放图像
- 9. 图像缩放IE8
- 10. NSToolbarItem图像缩放
- 11. CGAffineTransformMakeRotation缩放图像
- 12. WPF图像缩放
- 13. jqgrid图像缩放
- 14. 图像缩放+ Modal?
- 15. Bootstrap:Responsively缩放图像
- 16. 帧图像缩放
- 17. CollapsingToolbarLayout图像缩放
- 18. Symfony2:缩放图像
- 19. C缩放图像#
- 20. jquery缩放图像
- 21. Java图像缩放
- 22. UISegmentedControl图像缩放
- 23. 缩放图像javascript
- 24. Pygame - 缩放图像
- 25. 缩放图像4
- 26. KineticJS图像缩放
- 27. Android缩放位图图像
- 28. 将图像缩放为原图
- 29. Java高级图像缩放图像
- 30. 缩放UI图像源图像
thx mate!这正是我所期待的 – Libathos