Friday, August 28, 2015

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