我的应用程序用智能手机的相机拍照。拍摄照片后,我想将照片分配给 ImageView。但随后 ImageView 的大小发生了变化。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE
&& resultCode == Activity.RESULT_OK) {
ContentResolver cr = getContentResolver();
Bitmap bitmap = null;
Bitmap scaledBitmap = null;
InputStream in = null;
Image image = this.getActualImage();
try {
in = cr.openInputStream(this.imageUri);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 1;
bitmap = BitmapFactory.decodeStream(in, null, o);
ImageView view = null;
view = (ImageView) findViewById(R.id.img_face1);
int height = view.getHeight();
int width = view.getWidth();
Log.i(TAG, "Scale operation dimenions - width: " + width
+ ", height: " + height);
scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
view.setImageBitmap(scaledBitmap);
System.gc();
in.close();
} catch (Exception e) {
Log.e("Camera", e.toString());
}
我的布局 xml 如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen_capture_face2"
style="@style/linearLayoutVertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2" >
<ImageView
android:id="@+id/img_face1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="@string/desc_dummy_face"
android:src="@drawable/dummy_face"
/>
<ImageView
android:id="@+id/img_face2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="@string/desc_dummy_face"
android:src="@drawable/dummy_face"
/>
</LinearLayout>
在我将位图分配给 ImageView img_face1 后,大小会发生变化。为什么会这样?我错过了什么?我是 Android 新手,也许这只是一些小错误。