使用 LayoutParams 参数来设置 View 的宽度和高度,然後 Bitmap 放大後显示在 ImageView上,並保存這個寬高度,在打開另一個IMAGEView的時候用同樣的寬高度來顯示那個圖片
时间: 2023-09-23 13:06:43 浏览: 50
### 回答1:
可以使用以下代码来设置 View 的宽度和高度,并在 ImageView 中显示 Bitmap:
```
//设置View的宽度和高度
LayoutParams params = imageView.getLayoutParams();
params.width = bitmap.getWidth();
params.height = bitmap.getHeight();
imageView.setLayoutParams(params);
//将Bitmap放大后显示在ImageView上
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(scaledBitmap);
```
要保存宽度和高度,可以将其存储在 SharedPreferences 中,以便在打开另一个 ImageView 时使用。例如:
```
//保存宽度和高度
SharedPreferences.Editor editor = getSharedPreferences("imageSize", MODE_PRIVATE).edit();
editor.putInt("width", bitmap.getWidth());
editor.putInt("height", bitmap.getHeight());
editor.apply();
//在打开另一个ImageView时使用相同的宽度和高度
SharedPreferences preferences = getSharedPreferences("imageSize", MODE_PRIVATE);
int width = preferences.getInt("width", 0);
int height = preferences.getInt("height", 0);
LayoutParams params = imageView.getLayoutParams();
params.width = width;
params.height = height;
imageView.setLayoutParams(params);
```
### 回答2:
使用 LayoutParams 参数可以动态地设置 View 的宽度和高度。我们通常使用 RelativeLayout.LayoutParams 或 LinearLayout.LayoutParams 来设置宽高。
要将放大后的 Bitmap 显示在 ImageView 上,可以先获取 ImageView 的 LayoutParams,然后设置宽高参数。例如:
```
// 获取 ImageView 的 LayoutParams
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
// 设置 Bitmap 的宽高
params.width = newWidth; // 设置宽度
params.height = newHeight; // 设置高度
// 将 LayoutParams 应用到 ImageView
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
```
对于保存这个宽高度,在需要的时候可以使用 SharedPreferences 或通过其他方式保存到本地。例如使用 SharedPreferences:
```
// 保存宽高度
SharedPreferences.Editor editor = getSharedPreferences("image_size", MODE_PRIVATE).edit();
editor.putInt("width", newWidth);
editor.putInt("height", newHeight);
editor.apply();
```
当需要打开另一个 ImageView 显示图片时,可以将之前保存的宽高度取出并应用到新的 ImageView:
```
// 获取保存的宽高度
SharedPreferences prefs = getSharedPreferences("image_size", MODE_PRIVATE);
int width = prefs.getInt("width", 0);
int height = prefs.getInt("height", 0);
// 设置新的 ImageView 的宽高
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) newImageView.getLayoutParams();
params.width = width;
params.height = height;
newImageView.setLayoutParams(params);
```
这样,新的 ImageView 将会以之前保存的宽高度来显示图片。
### 回答3:
要使用LayoutParams参数来设置View的宽度和高度,首先需要获取到要设置宽高的View对象。可以通过findViewById()方法或者动态创建View来获取 View 对象。
然后,可以创建一个LayoutParams对象,通过该对象可以设置View的宽度和高度。LayoutParams有多种类型,例如LinearLayout.LayoutParams、RelativeLayout.LayoutParams等,可以根据需要选择合适的LayoutParams。
Bitmap放大后显示在ImageView上的操作可以通过以下步骤实现:
1. 使用BitmapFactory.decodeFile()或者其他方法,将要显示的图片文件转换为Bitmap对象。
2. 使用Bitmap的createScaledBitmap()方法,设置Bitmap的宽度和高度来进行放大。
3. 将放大后的Bitmap对象设置给ImageView的setImageBitmap()方法,即可显示在ImageView上。
为了保存这个宽高度,可以在设置LayoutParams对象前,先获取到Bitmap对象的宽度和高度,然后将这个宽高度保存在一个变量中。再设置LayoutParams对象时,直接使用保存的宽高度来进行设置。
当打开另一个ImageView时,可以根据上述步骤将要显示的图片文件转换为Bitmap对象,并将保存的宽高度设置给LayoutParams对象。然后,将Bitmap对象设置给ImageView来显示该图片。这样就能保证在不同的ImageView上显示的图片都具有相同的宽高度。