我正在尝试使用相机单击图像并将其显示在我的片段中。这是我的代码:
fragment_image
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AutoCompleteTextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:background="@color/translucent_grey"
android:ems="10"
android:hint="@string/hint_location"
android:inputType="text"
android:textSize="14sp" >
<requestFocus />
</AutoCompleteTextView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/snap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/image"
android:layout_alignParentRight="true"
android:layout_marginBottom="108dp"
android:layout_marginRight="160dp"
android:background="@color/white"
android:src="@drawable/camera" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:orientation="horizontal" />
</RelativeLayout>
<FrameLayout
android:id="@+id/datetime_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="2dp" />
<FrameLayout
android:id="@+id/eventDetails_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp" />
</LinearLayout>
</ScrollView>
ImageFragment.java
@SuppressLint("NewApi")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
ContentResolver cr = getActivity().getContentResolver();
try {
cr.notifyChange(imageUri, null);
File imageFile = new File(tempPhoto.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Bitmap photo=null;
if (resultCode == Activity.RESULT_OK) {
try {
photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
//image.setImageBitmap(photo);
Drawable drawable = new BitmapDrawable(getResources(), photo);
image.setImageDrawable(drawable);
//image.setScaleType(ScaleType.FIT_CENTER);
image.setScaleType(ScaleType.FIT_XY);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
尽管遵循了这个帖子的建议,我的图像仍然如图所示拉伸:
你知道我在做什么问题吗?
答案 0 :(得分:1)
将此添加到imageview中:
android:minHeight="120dp"
android:minWidth="180dp"
android:maxWidth="420dp"
android:maxHeight="680dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
答案 1 :(得分:0)
将缩放比例类型ScaleType.FIT_XY
替换为ScaleType.CENTER_INSIDE
:
image.setScaleType(ScaleType.CENTER_INSIDE);
如Android developer reference中所述:
CENTER_INSIDE均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)。