I am trying to programatically change the width of an ImageView
without resizing the image. The ImageView
originally shows the entire image, but when the width of the image is decreased, a portion of the image should be cut off, like this:
However, when my code reduces the width of the ImageView
from 350dp
to 200dp
, the image is automatically resized instead:
How do I ensure that the ImageView does not resize the image when I decrease its width? Below is my code:
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:background="@drawable/android"/>
</LinearLayout>
MainActivity.java :
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reduce the width of the image preview box
ImageView imagePreviewBox = findViewById(R.id.photo_1_preview);
imagePreviewBox.getLayoutParams().width = 250;
}
}
Set the scale type to CENTER_CROP. That will center the image in the view, and crop out anything that doesn't fit.
To get your desired behavior, simply set android:scaleType="fitStart"
to your ImageView in xml. It will not change the aspect ratio of the src, but aligns it to the start of the ImageView, just as you mentioned.
The problem you're facing is because the drawable
is set as a background of ImageView
instead of being set as a source
After you fix that, the right scaleType
to choose for your use-case is fitStart
which will maintain the original aspect ratio for the bitmap and align it to the top-left of the ImageView
Please use the updated layout file -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:scaleType="fitStart"
android:src="@drawable/android"/>
</LinearLayout>
android:scaleType="matrix"
for the ImageView
and put the desired image in android:src
. NOTE : In this case, I set the LinearLayout
to be 400x400 because the image size is 400x400 px and wrap the ImageView
inside of a LinearLayout
just for clarity.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333333"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_height="400px"
android:layout_width="400px"
android:orientation="vertical"
android:background="#0000FF">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="@drawable/android"
android:scaleType="matrix"
android:id="@+id/iv"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = findViewById(R.id.iv);
ViewGroup.LayoutParams params = iv.getLayoutParams();
params.width = 200; //200 pixels.
//Not necessary in my test. You may need to call the following
//method if you set the params outside of onCreate(), onCreateView().
//iv.requestLayout();
}
}
Output:
Try with the following code.
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/image_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:src="@drawable/dumy" />
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.getLayoutParams().width = dpToPixels(150);
imageView.requestLayout();
imageView.invalidate();
}
private int dpToPixels(int size) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.