Example usage for android.widget ImageView getDrawable

List of usage examples for android.widget ImageView getDrawable

Introduction

In this page you can find the example usage for android.widget ImageView getDrawable.

Prototype

public Drawable getDrawable() 

Source Link

Document

Gets the current Drawable, or null if no Drawable has been assigned.

Usage

From source file:com.micabyte.android.app.BaseActivity.java

private static void unbindViewReferences(View view) {
    // set all listeners to null
    try {//from  w ww  .  ja  v a2  s.c om
        view.setOnClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnCreateContextMenuListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnFocusChangeListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnKeyListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnLongClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    // set background to null
    Drawable d = view.getBackground();
    if (d != null) {
        d.setCallback(null);
    }
    if (view instanceof ImageView) {
        final ImageView imageView = (ImageView) view;
        d = imageView.getDrawable();
        if (d != null) {
            d.setCallback(null);
        }
        imageView.setImageDrawable(null);
        imageView.setImageBitmap(null);
    }
    if (view instanceof ImageButton) {
        final ImageButton imageB = (ImageButton) view;
        d = imageB.getDrawable();
        if (d != null) {
            d.setCallback(null);
        }
        imageB.setImageDrawable(null);
    }
    // destroy WebView
    if (view instanceof WebView) {
        view.destroyDrawingCache();
        ((WebView) view).destroy();
    }
}

From source file:com.image.oom.ImageUtil.java

private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
    if (imageView != null) {
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof DownloadedDrawable) {
            DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
            return downloadedDrawable.getBitmapDownloaderTask();
        }//from w  ww  .  j  a va 2  s .co  m
    }
    return null;
}

From source file:android.support.v7.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches <code>ImageView</code>s which have drawable flat-filled
 * with the specific color./*from w  ww.j  a v  a  2  s .c  om*/
 */
public static Matcher drawable(@ColorInt final int color) {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {
        private String failedComparisonDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText("with drawable of color: ");

            description.appendText(failedComparisonDescription);
        }

        @Override
        public boolean matchesSafely(final ImageView view) {
            Drawable drawable = view.getDrawable();
            if (drawable == null) {
                return false;
            }

            // One option is to check if we have a ColorDrawable and then call getColor
            // but that API is v11+. Instead, we call our helper method that checks whether
            // all pixels in a Drawable are of the same specified color.
            try {
                TestUtils.assertAllPixelsOfColor("", drawable, view.getWidth(), view.getHeight(), true, color,
                        0, true);
                // If we are here, the color comparison has passed.
                failedComparisonDescription = null;
                return true;
            } catch (Throwable t) {
                // If we are here, the color comparison has failed.
                failedComparisonDescription = t.getMessage();
                return false;
            }
        }
    };
}

From source file:com.fast.access.kam.widget.colorpicker.dashclockpicker.ColorPickerDialogDash.java

private static void setColorViewValue(View view, int color) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {/*w  w  w. j a v a  2s  .c  o m*/
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        int darkenedColor = Color.rgb(Color.red(color) * 192 / 256, Color.green(color) * 192 / 256,
                Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        colorChoiceDrawable.setStroke(
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics()),
                darkenedColor);
        imageView.setImageDrawable(colorChoiceDrawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:org.jak_linux.dns66.main.StartFragment.java

public static void updateStatus(View rootView, int status) {
    Context context = rootView.getContext();
    TextView stateText = (TextView) rootView.findViewById(R.id.state_textview);
    ImageView startButton = (ImageView) rootView.findViewById(R.id.start_button);

    if (startButton == null || stateText == null)
        return;/*from  www.  ja  v a 2 s .  c  o m*/

    stateText.setText(rootView.getContext().getString(AdVpnService.vpnStatusToTextId(status)));

    startButton.getDrawable().setTintList(null);
    switch (status) {
    case AdVpnService.VPN_STATUS_RECONNECTING:
    case AdVpnService.VPN_STATUS_STARTING:
    case AdVpnService.VPN_STATUS_STOPPING:
        startButton.setImageAlpha(128);
        break;
    case AdVpnService.VPN_STATUS_STOPPED:
        startButton.setImageAlpha(64);
        break;
    case AdVpnService.VPN_STATUS_RUNNING:
        startButton.setImageAlpha(255);
        break;
    case AdVpnService.VPN_STATUS_RECONNECTING_NETWORK_ERROR:
        startButton.setImageAlpha(255);
        startButton.getDrawable().setTint(ContextCompat.getColor(context, R.color.stateError));
        break;
    }
}

From source file:org.smssecure.smssecure.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {//  w ww  .  j a va  2  s.  c  o m
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res.getDrawable(R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:com.tingtingapps.securesms.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {//from w w  w  .  j  av  a2  s  .c o m
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res
                    .getDrawable(com.tingtingapps.securesms.R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:mobisocial.musubi.objects.PictureObj.java

/**
 * Pass in one of raw or fd as the source of the image.
 * Not thread safe, only call on the ui thread.
 */// w w w .  j  a  v  a 2  s.c  o m
protected static void bindImageToView(Context context, ImageView imageView, byte[] raw, FileDescriptor fd) {
    // recycle old images (vs. caching in ImageCache)
    if (imageView.getDrawable() != null) {
        BitmapDrawable d = (BitmapDrawable) imageView.getDrawable();
        if (d != null && d.getBitmap() != null) {
            d.getBitmap().recycle();
        }
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    if (fd != null) {
        BitmapFactory.decodeFileDescriptor(fd, null, options);
    } else {
        BitmapFactory.decodeByteArray(raw, 0, raw.length, options);
    }
    Resources res = context.getResources();

    float scaleFactor;
    if (res.getBoolean(R.bool.is_tablet)) {
        scaleFactor = 3.0f;
    } else {
        scaleFactor = 2.0f;
    }
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int pixels = dm.widthPixels;
    if (dm.heightPixels < pixels) {
        pixels = dm.heightPixels;
    }
    int width = (int) (pixels / scaleFactor);
    int height = (int) ((float) width / options.outWidth * options.outHeight);
    int max_height = (int) (AppStateObj.MAX_HEIGHT * dm.density);
    if (height > max_height) {
        width = width * max_height / height;
        height = max_height;
    }

    options.inJustDecodeBounds = false;
    options.inTempStorage = getTempData();
    options.inSampleSize = 1;
    //TODO: lame, can just compute
    while (options.outWidth / (options.inSampleSize + 1) >= width
            && options.outHeight / (options.inSampleSize + 1) >= height) {
        options.inSampleSize++;
    }
    options.inPurgeable = true;
    options.inInputShareable = true;

    Bitmap bm;
    if (fd != null) {
        bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
    } else {
        bm = BitmapFactory.decodeByteArray(raw, 0, raw.length, options);
    }
    imageView.getLayoutParams().width = width + 13;
    imageView.getLayoutParams().height = height + 14;
    imageView.setImageBitmap(bm);
}

From source file:com.ohso.util.ImageHandler.java

private static ImageGetter getImageGetter(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getImageGetter();
        }//  w  ww  .j  a  v a  2s.  c om
    }
    return null;
}

From source file:com.cerema.cloud2.datamodel.ThumbnailsCacheManager.java

public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }/*from  w ww.j  a va  2  s.  c o  m*/
    }
    return null;
}