Showing posts with label Equal height and width of imageview.. Show all posts
Showing posts with label Equal height and width of imageview.. Show all posts

Monday, December 26, 2016

Square image view

Square image view is used to show a non square image into the form of square.




Steps to use SquareImageView in your project.

  1. Crate a project(SquareImageDemo) in android studio.

  2. Create a class SquareImageView.Java and update it-


    package com.pankaj.square_image.demo;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class SquareImageView extends ImageView {
    
        //________auto created constructor
        public SquareImageView(Context context) {
            super(context);
        }
    
        //________auto created constructor
        public SquareImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        //________auto created constructor
        public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            //_______________pass same paramere value for width and geight for the SquareImageView.
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        }
    }
    
    
    
    
    
     
  3. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <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:background="#203F51B5"
        android:padding="@dimen/activity_horizontal_margin"
        tools:context="com.pankaj.square_image.demo.MainActivity">
    
        <!--scroll view to provide the scrollable screen if contant is large-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">
    
            <!--main child layout for scroll view-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">
    
                <!--title for default image view-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Default Image"
                    android:textSize="20dp" />
    
                <!--default image view-->
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:scaleType="fitXY"
                    android:src="@drawable/madhya_pradesh" />
    
                <!--title for square image view-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:text="Square Image"
                    android:textSize="20dp" />
    
                <!--square image view-->
                <com.pankaj.square_image.demo.SquareImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_horizontal_margin"
                    android:scaleType="fitXY"
                    android:src="@drawable/madhya_pradesh" />
            </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>
    
    
    
     

  4. All the application development process with squareImageView has completed, Now run the app and look the screen. 
     
     





  5. Good bye, Thanks to read this blog.