Skip to main content

How to use ImageView ScaleType in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#dddce4"
    >
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:src="@drawable/elephant"
        android:background="#9c9ba2"
        android:layout_toRightOf="@id/spinner"
        />
    <TextView
        android:id="@+id/tv_original"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Original Image Size"
        android:layout_below="@id/iv"
        android:layout_alignLeft="@id/iv"
        android:fontFamily="sans-serif-condensed"
        />
    <!--
        ImageView.ScaleType
            Options for scaling the bounds of an image to the bounds of this view.

            CENTER
                Center the image in the view, but perform no scaling.
            CENTER_CROP
                Scale the image uniformly (maintain the image's aspect ratio) so that both
                dimensions (width and height) of the image will be equal to or larger than
                the corresponding dimension of the view (minus padding).
            CENTER_INSIDE
                Scale the image uniformly (maintain the image's aspect ratio) so that both
                dimensions (width and height) of the image will be equal to or less than
                the corresponding dimension of the view (minus padding).
            FIT_CENTER
                Scale the image using CENTER.
            FIT_END
                Scale the image using END.
            FIT_START
                Scale the image using START.
            FIT_XY
                Scale the image using FILL.
            MATRIX
                Scale using the image matrix when drawing.
    -->
    <!--
        Matrix.ScaleToFit
            Controls how the src rect should align into the dst rect for setRectToRect().

            Matrix.ScaleToFit CENTER
                Compute a scale that will maintain the original src aspect ratio,
                but will also ensure that src fits entirely inside dst. At least
                one axis (X or Y) will fit exactly. The result is centered inside dst.

            Matrix.ScaleToFit END
                Compute a scale that will maintain the original src aspect ratio,
                but will also ensure that src fits entirely inside dst. At least
                one axis (X or Y) will fit exactly. END aligns the result to
                the right and bottom edges of dst.

            Matrix.ScaleToFit FILL
                Scale in X and Y independently, so that src matches dst exactly.
                This may change the aspect ratio of the src.

            Matrix.ScaleToFit START
                Compute a scale that will maintain the original src aspect ratio,
                but will also ensure that src fits entirely inside dst. At least
                one axis (X or Y) will fit exactly. START aligns the result to
                the left and top edges of dst.
    -->
    <ImageView
        android:id="@+id/iv_scaled"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:layout_toRightOf="@id/iv"
        android:src="@drawable/elephant"
        android:background="#9c9ba2"
        />
    <TextView
        android:id="@+id/tv_scaled"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scaled Image"
        android:layout_below="@id/iv_scaled"
        android:layout_alignLeft="@id/iv_scaled"
        android:fontFamily="sans-serif-condensed"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final ImageView iv = (ImageView) findViewById(R.id.iv);
        final ImageView iv_scaled = (ImageView) findViewById(R.id.iv_scaled);
        final TextView tv_scaled = (TextView) findViewById(R.id.tv_scaled);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        // Initialize a String array of ImageView image scale type
        String[] scaleType = new String[]{
                "CENTER",
                "CENTER_CROP",
                "CENTER_INSIDE",
                "FIT_CENTER",
                "FIT_END",
                "FIT_START",
                "FIT_XY",
                "MATRIX"
        };

        // Create a list from scale type array elements
        final List<String> scaleTypeList = new ArrayList<>(Arrays.asList(scaleType));
        // Initialize an ArrayAdapter
        final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this,R.layout.spinner_item,scaleTypeList){
            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                // Get the current item from spinner
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
                if(position%2 == 1) {
                    // Set a background color for spinner item
                    tv.setBackgroundColor(Color.parseColor("#FFCEC7DB"));
                }
                else {
                    // Set a background color for spinner alternate item
                    tv.setBackgroundColor(Color.parseColor("#FFBDB6CA"));
                }
                return view;
            }
        };
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);;
        spinner.setAdapter(spinnerArrayAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // Get the selected item text from spinner
                String selectedItem = adapterView.getItemAtPosition(i).toString();

                // Set the image scale type of ImageView
                // public static ImageView.ScaleType valueOf (String name)
                iv_scaled.setScaleType(ImageView.ScaleType.valueOf(selectedItem));

                // Notify the user about image scale type changed
                tv_scaled.setText("Image ScaleType : " + selectedItem);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // Do something
            }
        });
 }
}








More android examples

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker Example activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…