Example usage for android.widget ImageView setImageDrawable

List of usage examples for android.widget ImageView setImageDrawable

Introduction

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

Prototype

public void setImageDrawable(@Nullable Drawable drawable) 

Source Link

Document

Sets a drawable as the content of this ImageView.

Usage

From source file:com.example.ray.firstapp.bottombar.BottomBar.java

private void updateItems(final BottomBarItemBase[] bottomBarItems) {
    if (mItemContainer == null) {
        initializeViews();// w  w  w.  jav a  2s  .  com
    }

    int index = 0;
    int biggestWidth = 0;

    mIsShiftingMode = MAX_FIXED_TAB_COUNT < bottomBarItems.length;

    if (!mIsDarkTheme && !mIgnoreNightMode && MiscUtils.isNightMode(mContext)) {
        mIsDarkTheme = true;
    }

    if (!mIsTabletMode && mIsShiftingMode) {
        mDefaultBackgroundColor = mCurrentBackgroundColor = mPrimaryColor;
        mBackgroundView.setBackgroundColor(mDefaultBackgroundColor);

        if (mContext instanceof Activity) {
            navBarMagic((Activity) mContext, this);
        }
    } else if (mIsDarkTheme) {
        darkThemeMagic();
    }

    View[] viewsToAdd = new View[bottomBarItems.length];

    for (BottomBarItemBase bottomBarItemBase : bottomBarItems) {
        int layoutResource;

        if (mIsShiftingMode && !mIsTabletMode) {
            layoutResource = R.layout.bb_bottom_bar_item_shifting;
        } else {
            layoutResource = mIsTabletMode ? R.layout.bb_bottom_bar_item_fixed_tablet
                    : R.layout.bb_bottom_bar_item_fixed;
        }

        View bottomBarTab = View.inflate(mContext, layoutResource, null);
        ImageView icon = (ImageView) bottomBarTab.findViewById(R.id.bb_bottom_bar_icon);

        icon.setImageDrawable(bottomBarItemBase.getIcon(mContext));

        if (!mIsTabletMode) {
            TextView title = (TextView) bottomBarTab.findViewById(R.id.bb_bottom_bar_title);
            title.setText(bottomBarItemBase.getTitle(mContext));

            if (mPendingTextAppearance != -1) {
                MiscUtils.setTextAppearance(title, mPendingTextAppearance);
            }

            if (mPendingTypeface != null) {
                title.setTypeface(mPendingTypeface);
            }
        }

        if (mIsDarkTheme || (!mIsTabletMode && mIsShiftingMode)) {
            icon.setColorFilter(mWhiteColor);
        }

        if (bottomBarItemBase instanceof BottomBarTab) {
            bottomBarTab.setId(((BottomBarTab) bottomBarItemBase).id);
        }

        if (index == mCurrentTabPosition) {
            selectTab(bottomBarTab, false);
        } else {
            unselectTab(bottomBarTab, false);
        }

        if (!mIsTabletMode) {
            if (bottomBarTab.getWidth() > biggestWidth) {
                biggestWidth = bottomBarTab.getWidth();
            }

            viewsToAdd[index] = bottomBarTab;
        } else {
            mItemContainer.addView(bottomBarTab);
        }

        bottomBarTab.setOnClickListener(this);
        bottomBarTab.setOnLongClickListener(this);
        index++;
    }

    if (!mIsTabletMode) {
        int proposedItemWidth = Math.min(MiscUtils.dpToPixel(mContext, mScreenWidth / bottomBarItems.length),
                mMaxFixedItemWidth);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(proposedItemWidth,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        for (View bottomBarView : viewsToAdd) {
            bottomBarView.setLayoutParams(params);
            mItemContainer.addView(bottomBarView);
        }
    }

    if (mPendingTextAppearance != -1) {
        mPendingTextAppearance = -1;
    }

    if (mPendingTypeface != null) {
        mPendingTypeface = null;
    }
}

From source file:com.conferenceengineer.android.iosched.util.ImageLoader.java

public ImageContainer get(String requestUrl, ImageView imageView, Drawable placeHolder, int maxWidth,
        int maxHeight) {

    // Find any old image load request pending on this ImageView (in case this view was
    // recycled)/*from  w w w  .  j av  a2  s . c  o m*/
    ImageContainer imageContainer = imageView.getTag() != null && imageView.getTag() instanceof ImageContainer
            ? (ImageContainer) imageView.getTag()
            : null;

    // Find image url from prior request
    String recycledImageUrl = imageContainer != null ? imageContainer.getRequestUrl() : null;

    // If the new requestUrl is null or the new requestUrl is different to the previous
    // recycled requestUrl
    if (requestUrl == null || !requestUrl.equals(recycledImageUrl)) {
        if (imageContainer != null) {
            // Cancel previous image request
            imageContainer.cancelRequest();
            imageView.setTag(null);
        }
        if (requestUrl != null) {
            // Queue new request to fetch image
            imageContainer = get(requestUrl, getImageListener(mResources, imageView, placeHolder, mFadeInImage),
                    maxWidth, maxHeight);
            // Store request in ImageView tag
            imageView.setTag(imageContainer);
        } else {
            imageView.setImageDrawable(placeHolder);
            imageView.setTag(null);
        }
    }

    return imageContainer;
}

From source file:cn.com.wo.bitmap.ImageWorker.java

public void loadImageByIndex(String url, ImageView imageView, int loadingIndex, boolean isBackground) {
    //      url = Images.getUrl();
    if (url == null || url.trim().length() == 0 || !url.startsWith("http"))
        return;/*from   w w w  .j av  a 2s.c o m*/
    if (ImageFetcher.isDebug)
        Log.i(ImageFetcher.TAG, "loadImage " + url + "; isBg=" + isBackground + "; " + (imageView == null));
    //    url = "http://58.254.132.169/picture/90755000/copyright/singer/2012032806/23280.jpg";

    BitmapDrawable value = null;

    if (mImageCache != null) {
        value = mImageCache.getBitmapFromMemCache(url);
    }

    if (value != null) {
        // Bitmap found in memory cache
        if (ImageFetcher.isDebug)
            Log.i(ImageFetcher.TAG, "value is null ?  " + (value == null));
        if (isBackground) {
            imageView.setBackgroundDrawable(null);
            imageView.setBackgroundDrawable(value);
        } else
            imageView.setImageDrawable(value);
    } else if (cancelPotentialWork(url, imageView, isBackground)) {
        if (ImageFetcher.isDebug)
            Log.i(ImageFetcher.TAG, "value is null ?  " + (value == null));
        imageView.setTag(loadingIndex);
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView, 0, isBackground);
        final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap[loadingIndex], task);
        //          imageView.setImageDrawable(asyncDrawable);
        if (isBackground) {
            imageView.setBackgroundDrawable(null);
            imageView.setBackgroundDrawable(asyncDrawable);
        } else
            imageView.setImageDrawable(asyncDrawable);

        // NOTE: This uses a custom version of AsyncTask that has been pulled from the
        // framework and slightly modified. Refer to the docs at the top of the class
        // for more info on what was changed.
        task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, url);
    }
}

From source file:com.example.android.recyclingbanks.MainActivity.java

private View prepareInfoView(final Marker marker) {
    // TODO change this to xml?
    //prepare InfoView programmatically
    final LinearLayout infoView = new LinearLayout(MainActivity.this);
    LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    infoView.setOrientation(LinearLayout.VERTICAL);
    // attach the above layout to the infoView
    infoView.setLayoutParams(infoViewParams);

    String markerLongitude = Double.toString(marker.getPosition().longitude);
    String markerLatitude = Double.toString(marker.getPosition().latitude);

    final String imageURL = "https://maps.googleapis.com/maps/api/streetview?size=" + "500x300&location="
            + markerLatitude + "," + markerLongitude + "&fov=120&heading=0&pitch=0";

    //create street view preview @ top
    ImageView streetViewPreviewIV = new ImageView(MainActivity.this);

    Log.wtf("comparing TAG", String.valueOf(marker.getTag()));

    if (marker.getTag() == null) {
        Log.i("prepareInfoView", "fetching image");
        Picasso.with(this).load(imageURL).fetch(new MarkerCallback(marker));
    } else {/*from  w ww  . j a  v a2 s  . c o m*/
        Log.wtf("prepareInfoView", "building info window");

        // this scales the image to match parents WIDTH?, but retain image's height??
        LinearLayout.LayoutParams streetViewImageViewParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        streetViewPreviewIV.setLayoutParams(streetViewImageViewParams);
        // TODO upon conversion to xml, the imageView needs these to scale image to box
        // android:scaleType="fitStart"
        // android:adjustViewBounds="true"
        Picasso.with(MainActivity.this).load(imageURL).into(streetViewPreviewIV);
        infoView.addView(streetViewPreviewIV);
        //Log.wtf("prepareInfoView, marker tag set?", String.valueOf(marker.getTag()));
        //Picasso.with(this).setLoggingEnabled(true);
    }

    // create text boxes
    LinearLayout subInfoView = new LinearLayout(MainActivity.this);
    LinearLayout.LayoutParams subInfoViewParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    subInfoView.setOrientation(LinearLayout.VERTICAL);
    subInfoView.setLayoutParams(subInfoViewParams);

    TextView titleTextView = new TextView(MainActivity.this);
    titleTextView.setText(marker.getTitle());
    TextView snippetTextView = new TextView(MainActivity.this);
    snippetTextView.setText(marker.getSnippet());
    subInfoView.addView(titleTextView);
    subInfoView.addView(snippetTextView);
    infoView.addView(subInfoView);

    // add the image on the right
    ImageView streetViewIcon = new ImageView(MainActivity.this);
    // this scales the image to match parents height.
    LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    streetViewIcon.setLayoutParams(imageViewParams);
    Drawable drawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_streetview);
    streetViewIcon.setImageDrawable(drawable);
    infoView.addView(streetViewIcon);
    //Picasso.with(this).load(imageURL).into(streetViewPreviewIV, new MarkerCallback(marker));

    return infoView;
}

From source file:com.baseproject.image.ImageWorker.java

/**
 * Called when the processing is complete and the final bitmap should be set
 * on the ImageView.//from   w ww  .  ja v a  2s. co  m
 * 
 * @param imageView
 * @param bitmap
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
    if (mFadeInBitmap) {
        if (imageView.getTag() instanceof String && "no_animation".equals(String.valueOf(imageView.getTag()))) {
            imageView.setImageBitmap(bitmap);
        } else {
            // Transition drawable with a transparent drwabale and the final
            // bitmap
            TransitionDrawable td = new TransitionDrawable(
                    new Drawable[] { new ColorDrawable(android.R.color.transparent),
                            new BitmapDrawable(mContext.getResources(), bitmap) });
            final WeakReference<TransitionDrawable> tdReference = new WeakReference<TransitionDrawable>(td);
            td = null;
            // Set background to loading bitmap
            final BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), mLoadingBitmap);
            if (Build.VERSION.SDK_INT >= 16) {
                imageView.setBackground(bd);
            } else {
                imageView.setBackgroundDrawable(bd);

            }
            if (null != tdReference.get()) {
                imageView.setImageDrawable(tdReference.get());
                tdReference.get().startTransition(FADE_IN_TIME);
            }
        }
    } else {
        imageView.setImageBitmap(bitmap);
    }

}

From source file:android.support.v17.leanback.widget.GuidedActionsStylist.java

private boolean setIcon(final ImageView iconView, GuidedAction action) {
    Drawable icon = null;/*from w  w  w . j  a  va 2  s  . c  o  m*/
    if (iconView != null) {
        Context context = iconView.getContext();
        icon = action.getIcon();
        if (icon != null) {
            // setImageDrawable resets the drawable's level unless we set the view level first.
            iconView.setImageLevel(icon.getLevel());
            iconView.setImageDrawable(icon);
            iconView.setVisibility(View.VISIBLE);
        } else {
            iconView.setVisibility(View.GONE);
        }
    }
    return icon != null;
}

From source file:com.android.volley.cache.SimpleImageLoader.java

public ImageContainer set(String requestUrl, ImageView imageView, Drawable placeHolder, int maxWidth,
        int maxHeight, Bitmap bitmap) {

    // Find any old image load request pending on this ImageView (in case this view was
    // recycled)//from  w w w.  j a  va 2  s  .  co  m
    ImageContainer imageContainer = imageView.getTag() != null && imageView.getTag() instanceof ImageContainer
            ? (ImageContainer) imageView.getTag()
            : null;

    // Find image url from prior request
    //String recycledImageUrl = imageContainer != null ? imageContainer.getRequestUrl() : null;

    if (imageContainer != null) {
        // Cancel previous image request
        imageContainer.cancelRequest();
        imageView.setTag(null);
    }
    if (requestUrl != null) {
        // Queue new request to fetch image
        imageContainer = set(requestUrl, getImageListener(getResources(), imageView, placeHolder, mFadeInImage),
                maxWidth, maxHeight, imageView.getScaleType(), bitmap);
        // Store request in ImageView tag
        imageView.setTag(imageContainer);
    } else {
        if (!(imageView instanceof PhotoView)) {
            imageView.setImageDrawable(placeHolder);
        }
        imageView.setTag(null);
    }

    return imageContainer;
}

From source file:com.crearo.gpslogger.GpsMainActivity.java

private void refreshProfileIcon(String profileName) {

    ImageView imgLetter = (ImageView) drawerHeader.getView().findViewById(R.id.profiletextletter);
    TextDrawable drawLetter = TextDrawable.builder().beginConfig().bold()
            .textColor(getResources().getColor(R.color.golden)).useFont(Typeface.SANS_SERIF).endConfig()
            .buildRound(profileName.substring(0, 1).toUpperCase(),
                    getResources().getColor(R.color.primaryColorLight));

    imgLetter.setImageDrawable(drawLetter);
}

From source file:com.harlan.jxust.ui.view.bottombar.BottomBar.java

private void updateItems(final BottomBarItemBase[] bottomBarItems) {
    if (mItemContainer == null) {
        initializeViews();//  w ww  .  j  a  v a  2s.com
    }

    int index = 0;
    int biggestWidth = 0;

    mIsShiftingMode = MAX_FIXED_TAB_COUNT < bottomBarItems.length;

    if (!mIsDarkTheme && !mIgnoreNightMode && MiscUtils.isNightMode(mContext)) {
        mIsDarkTheme = true;
    }

    if (mIsDarkTheme) {
        darkThemeMagic();
    } else if (!mIsTabletMode && mIsShiftingMode) {
        mDefaultBackgroundColor = mCurrentBackgroundColor = mPrimaryColor;
        mBackgroundView.setBackgroundColor(mDefaultBackgroundColor);

        if (mContext instanceof Activity) {
            navBarMagic((Activity) mContext, this);
        }
    }

    View[] viewsToAdd = new View[bottomBarItems.length];

    for (BottomBarItemBase bottomBarItemBase : bottomBarItems) {
        int layoutResource;

        if (mIsShiftingMode && !mIsTabletMode) {
            layoutResource = R.layout.bb_bottom_bar_item_shifting;
        } else {
            layoutResource = mIsTabletMode ? R.layout.bb_bottom_bar_item_fixed_tablet
                    : R.layout.bb_bottom_bar_item_fixed;
        }

        View bottomBarTab = View.inflate(mContext, layoutResource, null);
        ImageView icon = (ImageView) bottomBarTab.findViewById(R.id.bb_bottom_bar_icon);

        icon.setImageDrawable(bottomBarItemBase.getIcon(mContext));

        if (!mIsTabletMode) {
            TextView title = (TextView) bottomBarTab.findViewById(R.id.bb_bottom_bar_title);
            title.setText(bottomBarItemBase.getTitle(mContext));

            if (mPendingTextAppearance != -1) {
                MiscUtils.setTextAppearance(title, mPendingTextAppearance);
            }

            if (mPendingTypeface != null) {
                title.setTypeface(mPendingTypeface);
            }
        }

        if (mIsDarkTheme || (!mIsTabletMode && mIsShiftingMode)) {
            icon.setColorFilter(mWhiteColor);
        }

        if (bottomBarItemBase instanceof BottomBarTab) {
            bottomBarTab.setId(((BottomBarTab) bottomBarItemBase).id);
        }

        if (index == mCurrentTabPosition) {
            selectTab(bottomBarTab, false);
        } else {
            unselectTab(bottomBarTab, false);
        }

        if (!mIsTabletMode) {
            if (bottomBarTab.getWidth() > biggestWidth) {
                biggestWidth = bottomBarTab.getWidth();
            }

            viewsToAdd[index] = bottomBarTab;
        } else {
            mItemContainer.addView(bottomBarTab);
        }

        bottomBarTab.setOnClickListener(this);
        bottomBarTab.setOnLongClickListener(this);
        index++;
    }

    if (!mIsTabletMode) {
        int proposedItemWidth = Math.min(MiscUtils.dpToPixel(mContext, mScreenWidth / bottomBarItems.length),
                mMaxFixedItemWidth);

        mInActiveShiftingItemWidth = (int) (proposedItemWidth * 0.9);
        mActiveShiftingItemWidth = (int) (proposedItemWidth
                + (proposedItemWidth * (bottomBarItems.length * 0.1)));

        for (View bottomBarView : viewsToAdd) {
            LinearLayout.LayoutParams params;

            if (mIsShiftingMode && !mIgnoreShiftingResize) {
                if (TAG_BOTTOM_BAR_VIEW_ACTIVE.equals(bottomBarView.getTag())) {
                    params = new LinearLayout.LayoutParams(mActiveShiftingItemWidth,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                } else {
                    params = new LinearLayout.LayoutParams(mInActiveShiftingItemWidth,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                }
            } else {
                params = new LinearLayout.LayoutParams(proposedItemWidth,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
            }

            bottomBarView.setLayoutParams(params);
            mItemContainer.addView(bottomBarView);
        }
    }

    if (mPendingTextAppearance != -1) {
        mPendingTextAppearance = -1;
    }

    if (mPendingTypeface != null) {
        mPendingTypeface = null;
    }
}

From source file:com.koushikdutta.superuser.FragmentLog.java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //header = (LinearLayout) getActivity().findViewById(R.id.header);

    ImageView icon = (ImageView) getActivity().findViewById(R.id.icon);

    TextView title, subtitle, request, command;

    title = (TextView) getActivity().findViewById(R.id.title);
    subtitle = (TextView) getActivity().findViewById(R.id.subtitle);
    request = (TextView) getActivity().findViewById(R.id.request);
    command = (TextView) getActivity().findViewById(R.id.command);

    Intent intent = getActivity().getIntent();

    if (intent != null) {

        Bundle bundle = intent.getBundleExtra("bundle");

        if (bundle != null) {
            String cmd = bundle.getString("command");

            int uid = bundle.getInt("uid", -1);

            int desiredUid = bundle.getInt("desiredUid", -1);

            if (uid != -1 && desiredUid != -1)
                up = SuDatabaseHelper.get(getContext(), uid, desiredUid, cmd);
        }/*from  w  w  w. j av a2 s . c o m*/
    }

    if (up != null) {

        String app = up.username;
        if (app == null || app.length() == 0)
            app = String.valueOf(up.uid);

        icon.setImageDrawable(Util.loadPackageIcon(getActivity(), up.packageName));

        title.setTextColor(((ActivityLog) getActivity()).textToolbar);
        subtitle.setTextColor(((ActivityLog) getActivity()).textToolbar);
        request.setTextColor(((ActivityLog) getActivity()).textToolbar);
        command.setTextColor(((ActivityLog) getActivity()).textToolbar);

        title.setText(up.getName());

        subtitle.setText(up.packageName + ", " + app);

        request.setText("Requested UID: " + up.desiredUid);

        command.setText(
                "Command: " + (TextUtils.isEmpty(up.command) ? getString(R.string.all_commands) : up.command));

        //getListView().setSelector(android.R.color.transparent);

    } else {
        callback = (LogCallback) getActivity();
    }

    LinearLayout logParent = (LinearLayout) getActivity().findViewById(R.id.log);
    LinearLayout notiParent = (LinearLayout) getActivity().findViewById(R.id.noti);

    int accent = ThemeStore.accentColor(getContext());

    TintHelper.setTint(log, accent, false);
    TintHelper.setTint(notification, accent, false);

    if (up == null) {
        log.setChecked(Settings.getLogging(getActivity()));

        notiParent.setVisibility(View.GONE);
        notification.setChecked(false);

    } else {
        log.setChecked(up.logging);

        notification.setChecked(up.notification);
    }

    logParent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            log.setChecked(!log.isChecked());
        }
    });

    log.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (up == null) {
                Settings.setLogging(getActivity(), b);

            } else {
                up.logging = b;
                SuDatabaseHelper.setPolicy(getActivity(), up);
            }
        }
    });

    notiParent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            notification.setChecked(!notification.isChecked());
        }
    });

    notification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (up == null) {

            } else {
                up.notification = notification.isChecked();
                SuDatabaseHelper.setPolicy(getActivity(), up);
            }
        }
    });

    load();
    LocalBroadcastManager.getInstance(getContext()).registerReceiver(receiver,
            new IntentFilter(Common.INTENT_FILTER_LOG));
}