Android ImageView


Android ImageView Tutorial.

Quick ImageView Examples and HowTo's

1. How to create a Square ImageView

First we will have our SquareHelper class:

import android.content.res.TypedArray;
import android.support.annotation.IntDef; 
import android.util.AttributeSet;
import android.view.View;

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy;

import online.sniper.R; 

/** 
 * Square view helper class 
 */ 
public class SquareHelper {    

    publicstaticfinalintBASE_WIDTH=0;      
    publicstaticfinalintBASE_HEIGHT=1;      
    publicstaticfinalintBASE_MIN_EDGE=2;      
    publicstaticfinalintBASE_MAX_EDGE=3;      

    @IntDef({BASE_WIDTH, BASE_HEIGHT, BASE_MAX_EDGE, BASE_MIN_EDGE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SquareViewEdge { 

    } 

    private View mView;
    private int mBaseEdge = BASE_WIDTH;

    public SquareHelper(View view, AttributeSet attrs) {
        mView = view;
        TypedArray a = view.getContext().obtainStyledAttributes(attrs, R.styleable.SquareView);
        try { 
            mBaseEdge = a.getInt(R.styleable.SquareView_baseEdge, BASE_WIDTH);
        } finally { 
            a.recycle();
        } 
    } 

    public void setBaseEdge(@SquareViewEdge int baseEdge) {
        mBaseEdge = baseEdge;
    } 

    public int getMeasuredSize() { 
        switch (mBaseEdge) {
            caseBASE_WIDTH: 
                return mView.getMeasuredWidth();
            caseBASE_HEIGHT: 
                return mView.getMeasuredHeight();
            caseBASE_MIN_EDGE: 
                return Math.min(mView.getMeasuredWidth(), mView.getMeasuredHeight());
            caseBASE_MAX_EDGE: 
                return Math.max(mView.getMeasuredWidth(), mView.getMeasuredHeight());
            default: 
                returnmView.getMeasuredWidth(); 
        } 
    } 

} 

Then our SquareImageView

import android.content.Context;
import android.support.v7.widget.AppCompatImageView; 
import  android.util.AttributeSet ;

public class SquareImageView extends AppCompatImageView { 

    private final SquareHelper mHelper ;     

    public SquareImageView(Context context) {
        this(context,null); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context,attrs); 
        mHelper = new SquareHelper(this, attrs);
    } 

    @Override 
    protectedvoid onMeasure(intwidthMeasureSpec,intheightMeasureSpec){     
        super.onMeasure(widthMeasureSpec,heightMeasureSpec); 
        int size = mHelper.getMeasuredSize();
        setMeasuredDimension(size, size);
    } 

} 

Top Android ImageView Examples

Let's look at some examples.

Hello.Lets see how to load images from  online to our android application.

Displaying images in adapterviews is quite common. Especially ListView, GridView and RecyclerView. In this example we see how to fetch images from the web and into a RecyclerView.

This is an android tutorial that explores the usage of Cloudinary, a cloud service to do the following:

Quick Glide Examples

1. How to Display Image with Glide

Here's a static method to load an image into an imageview. You provide a url and the imageview and Glide will load the image, showing a placeholder as the image loads.

You can also set the cache strategy:

    public static void displayI...

Cheers.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post