我有一个来自文件路径的屏幕快照图像,我正尝试将其加载到ImageView中。但是,imageView被切断。
这是它的外观:
应该是这样:
这是我的代码:
// MainActivity.java
//Setting image view from filepath
File imgFile = new File(arScreenshotPath);
ImageView myImage = findViewById(R.id.arScreen1);
if(imgFile.exists()){
myImage.setImageBitmap(Utils.decodeSampledBitmapFromFile(arScreenshotPath, 500, 250));
// myImage.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, 206, 144, false));
}else {
myImage.setImageResource(R.drawable.no_image);
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CostActivity">
<!-- Ar Screen capture -->
<!-- Ar Screen capture -->
<ImageView
android:id="@+id/arScreen1"
android:layout_width="match_parent"
android:layout_height="211dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
/>
<!-- Description -->
<TextView
android:id="@+id/quote2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DESCRIPTION"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/descriptionTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/descriptionInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Measurement -->
<TextView
android:id="@+id/measurement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MEASUREMENT"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Length -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/lengthTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="170dp"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/lengthInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="Length"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Breadth -->
<TextView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="X"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:textStyle="bold" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/breathTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="179dp"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/breathInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="Breadth"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<Spinner
android:id="@+id/cm"
style="@style/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
android:textStyle="bold"/>
</LinearLayout>
<!-- Cost Per Square CM -->
<TextView
android:id="@+id/cost_per_square_cm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COST PER SQUARE UNIT"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/costTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/costInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="$"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Variable -->
<TextView
android:id="@+id/variable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="VARIABLE (+)"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textStyle="bold"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/variableTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/variableInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:hint="$"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
我已经尝试了许多方法,例如减少布局权重,但是所有方法都无效。我怀疑这也可能是由于Java代码造成的。
在ImageView块中添加此属性
android:scaleType="fitCenter"
android:layout_weight="0.2"
您可以更改android:layout_weight
的值
并尝试下面的代码从文件中设置位图图像。
File imgFile = new File(arScreenshotPath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = findViewById(R.id.arScreen1);
myImage.setImageBitmap(myBitmap);
}
并将此权限包括在清单文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />