【发布时间】:2015-11-13 03:25:13
【问题描述】:
我是 Android 开发的新手。 在这个 stackoverflow 社区的一位开发人员的帮助下,我解决了图像景观问题,并留下了另外 2 个问题来完成我的项目。
- 在将其设置为 imageview 的背景之前裁剪图像。
- 即使保留 ImageView.ScaleType.FIT_XY ,如何阻止 imageview 缩小。我的意思是我从图库中选择的图像应该占据整个屏幕(布局)而不缩小。 我已经浏览了本网站中关于相同内容的一些问题和答案,但没有发现它们完全符合我的代码。
1.Crop an image from gallery in android
2.android:select image from gallery then crop that and show in an imageview
这是我的片段的源代码:
public class FragmentTab1 extends Fragment {
RelativeLayout homes;
private static int RESULT_LOAD_IMAGE = 1;
ImageView bitmapView;
BitmapFactory.Options options;
String filePath;
Button rot;
private int mDegree = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
homes = (RelativeLayout) view.findViewById(R.id.hmt);
homes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
final String filePath = prefs.getString("profilePic", "");
if (!filePath.equals("")) {
bitmapView = (ImageView) view.findViewById(R.id.keka);
bitmapView.setImageBitmap(ExifUtils.rotateBitmap(filePath, decodeSampledBitmap(new File(filePath), 400, 400)));
bitmapView.setScaleType(ImageView.ScaleType.FIT_XY);
}
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
Uri picUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(picUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
bitmapView = (ImageView) getActivity().findViewById(R.id.keka);
bitmapView.setImageBitmap(ExifUtils.rotateBitmap(filePath, decodeSampledBitmap(new File(filePath), 400, 400)));
bitmapView.setScaleType(ImageView.ScaleType.FIT_XY);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(getActivity());
Editor edit = shre.edit();
edit.putString("profilePic", filePath);
edit.commit();
}
}
public Bitmap decodeSampledBitmap(File res, int reqWidth, int reqHeight) {
if (res != null) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
FileInputStream stream2 = new FileInputStream(res);
BitmapFactory.decodeStream(stream2, null, options);
stream2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Calculate inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
o2.inJustDecodeBounds = false;
FileInputStream stream = null;
try {
stream = new FileInputStream(res);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, o2);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
} else
return null;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}`enter code here`
}`enter code here`
【问题讨论】: