unbind View Invisible Image Drawables - Android User Interface

Android examples for User Interface:View Hide Show

Description

unbind View Invisible Image Drawables

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

public class Main {
    static int screenHeight = -1;
    static int[] location = new int[2];

    public static void unbindInvisibleImageDrawables(View view) {
        if (null == view) {
            return;
        }//from  w ww. ja v  a  2s .c om

        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;

            if (imageView.getDrawable() != null && isInVisible(imageView)) {
                imageView.getDrawable().setCallback(null);
                imageView.setImageDrawable(null);
            }
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindInvisibleImageDrawables(((ViewGroup) view)
                        .getChildAt(i));
            }
        }
    }

    public static boolean isInVisible(View view) {
        if (screenHeight == -1) {
            screenHeight = view.getContext().getResources()
                    .getDisplayMetrics().heightPixels;
        }

        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int height = view.getHeight();

        //in top
        if ((top + height) < -0.5 * screenHeight) {
            return true;
        }

        //in bottom
        if (top > screenHeight + 0.5 * screenHeight) {
            return true;
        }

        return false;
    }
}

Related Tutorials