Example usage for android.widget ImageView getContext

List of usage examples for android.widget ImageView getContext

Introduction

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

Prototype

@ViewDebug.CapturedViewProperty
public final Context getContext() 

Source Link

Document

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Usage

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 {//from w w  w . j  av  a 2  s .  com
            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.frostwire.android.offers.InHouseBannerFactory.java

public static void loadAd(final ImageView placeholder, AdFormat adFormat) {
    Message randomMessage = Message.random();
    int randomDrawable = getRandomDrawable(adFormat, randomMessage);
    placeholder.setImageDrawable(ContextCompat.getDrawable(placeholder.getContext(), randomDrawable));
    placeholder.setOnClickListener(CLICK_LISTENERS.get(randomMessage));
}

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 ww . j a  v a 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(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:org.goseumdochi.android.leash.EfficientAnimation.java

private static void animate(final List<EaFrame> frames, final ImageView imageView, final int frameNumber,
        final boolean first, final int duration) {
    final EaFrame thisFrame = frames.get(frameNumber);

    if (first) {/*from ww  w.  j  a v  a 2  s . c o m*/
        thisFrame.drawable = new BitmapDrawable(imageView.getContext().getResources(),
                BitmapFactory.decodeByteArray(thisFrame.bytes, 0, thisFrame.bytes.length));
    } else {
        int prevFrameNumber;
        if (frameNumber == 0) {
            prevFrameNumber = frames.size() - 1;
        } else {
            prevFrameNumber = frameNumber - 1;
        }
        EaFrame previousFrame = frames.get(prevFrameNumber);
        ((BitmapDrawable) previousFrame.drawable).getBitmap().recycle();
        previousFrame.drawable = null;
        previousFrame.isReady = false;
    }

    final int nextFrameNumber = (frameNumber >= (frames.size() - 1)) ? 0 : frameNumber + 1;
    imageView.setImageDrawable(thisFrame.drawable);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (imageView.getDrawable() == thisFrame.drawable) {
                EaFrame nextFrame = frames.get(nextFrameNumber);
                if (nextFrame.isReady) {
                    animate(frames, imageView, nextFrameNumber, false, duration);
                } else {
                    nextFrame.isReady = true;
                }
            }
        }
    }, duration);

    new Thread(new Runnable() {
        @Override
        public void run() {
            EaFrame nextFrame = frames.get(nextFrameNumber);
            nextFrame.drawable = new BitmapDrawable(imageView.getContext().getResources(),
                    BitmapFactory.decodeByteArray(nextFrame.bytes, 0, nextFrame.bytes.length));
            if (nextFrame.isReady) {
                animate(frames, imageView, nextFrameNumber, false, duration);
            } else {
                nextFrame.isReady = true;
            }

        }
    }).run();
}

From source file:com.yanzhenjie.album.task.LocalImageLoader.java

/**
 * According to the ImageView obtains appropriate width and height of compression.
 *
 * @param imageView {@link ImageView}.//from  w  ww . j  a v  a2s  . co  m
 * @param viewSizes ViewSize.
 */
public static void measureSize(ImageView imageView, int[] viewSizes) {
    final DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics();
    final LayoutParams params = imageView.getLayoutParams();
    if (params == null) {
        viewSizes[0] = displayMetrics.widthPixels;
        viewSizes[1] = displayMetrics.heightPixels;
    } else {
        viewSizes[0] = params.width == LayoutParams.WRAP_CONTENT ? 0 : imageView.getMeasuredWidth(); // Get actual image width
        viewSizes[1] = params.height == LayoutParams.WRAP_CONTENT ? 0 : imageView.getMeasuredWidth(); // Get actual image
        // height

        if (viewSizes[0] <= 0)
            viewSizes[0] = displayMetrics.widthPixels; // Get layout width parameter
        if (viewSizes[1] <= 0)
            viewSizes[1] = displayMetrics.heightPixels; // Get layout height parameter
    }
}

From source file:com.grarak.kerneladiutor.utils.ViewUtils.java

public static void loadImagefromUrl(String url, ImageView imageView, int maxWidth, int maxHeight) {
    CustomTarget target = new CustomTarget(imageView, maxWidth, maxHeight);
    mProtectedFromGarbageCollectorTargets.add(target);
    Picasso.with(imageView.getContext()).load(url).into(target);
}

From source file:com.doctoror.fuckoffmusicplayer.presentation.util.BindingAdapters.java

@BindingAdapter({ "src", "tintNormal", "useActivatedSrcTint" })
public static void setUseActivatedSrcTint(@NonNull final ImageView imageView, @Nullable final Drawable src,
        @ColorInt final int tintNormal, final boolean useActivatedSrcTint) {
    if (!useActivatedSrcTint || src == null) {
        imageView.setImageDrawable(src);
    } else {/*w  w  w  .j  av  a2  s .c  o m*/
        imageView.setImageDrawable(
                DrawableUtils.getTintedDrawable(src, activatedTint(imageView.getContext(), tintNormal)));
    }
}

From source file:it.jaschke.alexandria.model.view.BookDetailViewModel.java

/**
 * Loads the book's cover image from the specified URI into the
 * {@link ImageView}. This method is used by the Data Binding Library.
 *
 * @param view {@link ImageView} to place the image into.
 * @param coverUri where the image should be retrieved from.
 *///from  w w  w .  j av  a2 s  . c  o m
@BindingAdapter({ "bind:coverUri" })
public static void loadBookCoverImage(ImageView view, String coverUri) {
    Context context = view.getContext();
    Picasso.with(context).load(coverUri) // TODO: Add placeholder and error images
            .into(view);
}

From source file:com.example.android.supportv7.graphics.ImageLoader.java

static void loadMediaStoreThumbnail(final ImageView imageView, final long id, final Listener listener) {

    final Bitmap cachedValue = CACHE.get(id);
    if (cachedValue != null) {
        // If the image is already in the cache, display the image,
        // call the listener now and return
        imageView.setImageBitmap(cachedValue);
        if (listener != null) {
            listener.onImageLoaded(cachedValue);
        }/*w w  w.  j a  v a2s  . c  o  m*/
        return;
    }

    AsyncTaskCompat.executeParallel(new AsyncTask<Void, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(Void... params) {
            return MediaStore.Images.Thumbnails.getThumbnail(imageView.getContext().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MINI_KIND, null);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);

            if (bitmap != null) {
                // Add the image to the memory cache first
                CACHE.put(id, bitmap);

                if (listener != null) {
                    listener.onImageLoaded(bitmap);
                }
            }
        }
    });
}

From source file:mx.com.adolfogarcia.popularmovies.model.view.MovieDetailViewModel.java

/**
 * Loads the movie's poster image from the specified URI into the
 * {@link ImageView}. This method is used by the Data Binding Library.
 *
 * @param view {@link ImageView} to place the image into.
 * @param posterUri where the image should be retrieved from.
 *///from   w  w  w .  java 2s.co  m
@BindingAdapter({ "bind:posterUri" })
public static void loadPosterImage(ImageView view, String posterUri) {
    Context context = view.getContext();
    int posterPixelWidth = context.getResources().getDimensionPixelSize(R.dimen.movie_poster_thumbnail_width);
    int posterPixelHeight = context.getResources().getDimensionPixelSize(R.dimen.movie_poster_thumbnail_height);
    Picasso.with(context).load(posterUri).resize(posterPixelWidth, posterPixelHeight)
            .placeholder(R.anim.poster_loading).error(R.drawable.logo_the_movie_db_180dp).into(view);
}