Example usage for android.widget ImageView setImageBitmap

List of usage examples for android.widget ImageView setImageBitmap

Introduction

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

Prototype

@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) 

Source Link

Document

Sets a Bitmap as the content of this ImageView.

Usage

From source file:com.scoreloop.client.android.ui.util.ImageDownloader.java

/**
 * Download the specified image from the Internet and binds it to the provided ImageView. The
 * binding is immediate if the image is found in the cache and will be done asynchronously
 * otherwise. A null bitmap will be associated to the ImageView if an error occurs.
 *
 * @param url The URL of the image to download.
 * @param imageView The ImageView to bind the downloaded image to.
 *//*from   www  .ja  va 2 s.c  o  m*/
public static void downloadImage(String url, Drawable drawable, ImageView imageView) {
    if (url == null) {
        return;
    }
    if (imageDownloader == null) {
        imageDownloader = new ImageDownloader();
    }
    imageDownloader.resetPurgeTimer();
    Bitmap bitmap = imageDownloader.getBitmapFromCache(url);
    if (bitmap == null) {
        imageDownloader.forceDownload(url, drawable, imageView);
    } else {
        cancelPotentialDownload(url, imageView);
        imageView.setImageBitmap(bitmap);
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.model.DbObject.java

/**
 * @param v the view to bind/*  w w  w . j  a va2s. c  o m*/
 * @param context standard activity context
 * @param c the cursor source for the object in the db object table.
 * Must include _id in the projection.
 * 
 * @param allowInteractions controls whether the bound view is
 * allowed to intercept touch events and do its own processing.
 */
public static void bindView(View v, final Context context, Cursor cursor, boolean allowInteractions) {
    TextView nameText = (TextView) v.findViewById(R.id.name_text);
    ViewGroup frame = (ViewGroup) v.findViewById(R.id.object_content);
    frame.removeAllViews();

    // make sure we have all the columns we need
    Long objId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_ID));
    String[] projection = null;
    String selection = DbObj.COL_ID + " = ?";
    String[] selectionArgs = new String[] { Long.toString(objId) };
    String sortOrder = null;
    Cursor c = context.getContentResolver().query(DbObj.OBJ_URI, projection, selection, selectionArgs,
            sortOrder);
    if (!c.moveToFirst()) {
        Log.w(TAG, "could not find obj " + objId);
        c.close();
        return;
    }
    DbObj obj = App.instance().getMusubi().objForCursor(c);
    if (obj == null) {
        nameText.setText("Failed to access database.");
        Log.e("DbObject", "cursor was null for bindView of DbObject");
        return;
    }
    DbUser sender = obj.getSender();
    Long timestamp = c.getLong(c.getColumnIndexOrThrow(DbObj.COL_TIMESTAMP));
    Long hash = obj.getHash();
    short deleted = c.getShort(c.getColumnIndexOrThrow(DELETED));
    String feedName = obj.getFeedName();
    String type = obj.getType();
    Date date = new Date(timestamp);
    c.close();
    c = null;

    if (sender == null) {
        nameText.setText("Message from unknown contact.");
        return;
    }
    nameText.setText(sender.getName());

    final ImageView icon = (ImageView) v.findViewById(R.id.icon);
    if (sViewProfileAction == null) {
        sViewProfileAction = new OnClickViewProfile((Activity) context);
    }
    icon.setTag(sender.getLocalId());
    if (allowInteractions) {
        icon.setOnClickListener(sViewProfileAction);
        v.setTag(objId);
    }
    icon.setImageBitmap(sender.getPicture());

    if (deleted == 1) {
        v.setBackgroundColor(sDeletedColor);
    } else {
        v.setBackgroundColor(Color.TRANSPARENT);
    }

    TextView timeText = (TextView) v.findViewById(R.id.time_text);
    timeText.setText(RelativeDate.getRelativeDate(date));

    frame.setTag(objId); // TODO: error prone! This is database id
    frame.setTag(R.id.object_entry, cursor.getPosition()); // this is cursor id
    FeedRenderer renderer = DbObjects.getFeedRenderer(type);
    if (renderer != null) {
        renderer.render(context, frame, obj, allowInteractions);
    }

    if (!allowInteractions) {
        v.findViewById(R.id.obj_attachments_icon).setVisibility(View.GONE);
        v.findViewById(R.id.obj_attachments).setVisibility(View.GONE);
    } else {
        if (!MusubiBaseActivity.isDeveloperModeEnabled(context)) {
            v.findViewById(R.id.obj_attachments_icon).setVisibility(View.GONE);
            v.findViewById(R.id.obj_attachments).setVisibility(View.GONE);
        } else {
            ImageView attachmentCountButton = (ImageView) v.findViewById(R.id.obj_attachments_icon);
            TextView attachmentCountText = (TextView) v.findViewById(R.id.obj_attachments);
            attachmentCountButton.setVisibility(View.VISIBLE);

            if (hash == 0) {
                attachmentCountButton.setVisibility(View.GONE);
            } else {
                //int color = DbObject.colorFor(hash);
                boolean selfPost = false;
                DBHelper helper = new DBHelper(context);
                try {
                    Cursor attachments = obj.getSubfeed().query("type=?", new String[] { LikeObj.TYPE });
                    try {
                        attachmentCountText.setText("+" + attachments.getCount());

                        if (attachments.moveToFirst()) {
                            while (!attachments.isAfterLast()) {
                                if (attachments.getInt(attachments.getColumnIndex(CONTACT_ID)) == -666) {
                                    selfPost = true;
                                    break;
                                }
                                attachments.moveToNext();

                            }
                        }
                    } finally {
                        attachments.close();
                    }
                } finally {
                    helper.close();
                }
                if (selfPost) {
                    attachmentCountButton.setImageResource(R.drawable.ic_menu_love_red);
                } else {
                    attachmentCountButton.setImageResource(R.drawable.ic_menu_love);
                }
                attachmentCountText.setTag(R.id.object_entry, hash);
                attachmentCountText.setTag(R.id.feed_label, Feed.uriForName(feedName));
                attachmentCountText.setOnClickListener(LikeListener.getInstance(context));
            }
        }
    }
}

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

/**
 * Sets a {@link android.graphics.Bitmap} to an {@link android.widget.ImageView} using a
 * fade-in animation. If there is a {@link android.graphics.drawable.Drawable} already set on
 * the ImageView then use that as the image to fade from. Otherwise fade in from a transparent
 * Drawable.//w w  w  .j  a  va 2 s .c o  m
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void setImageBitmap(final ImageView imageView, final Bitmap bitmap, Resources resources,
        boolean fadeIn) {

    // If we're fading in and on HC MR1+
    if (fadeIn && UIUtils.hasHoneycombMR1()) {
        // Use ViewPropertyAnimator to run a simple fade in + fade out animation to update the
        // ImageView
        imageView.animate().scaleY(0.95f).scaleX(0.95f).alpha(0f)
                .setDuration(imageView.getDrawable() == null ? 0 : HALF_FADE_IN_TIME)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        imageView.setImageBitmap(bitmap);
                        imageView.animate().alpha(1f).scaleY(1f).scaleX(1f).setDuration(HALF_FADE_IN_TIME)
                                .setListener(null);
                    }
                });
    } else if (fadeIn) {
        // Otherwise use a TransitionDrawable to fade in
        Drawable initialDrawable;
        if (imageView.getDrawable() != null) {
            initialDrawable = imageView.getDrawable();
        } else {
            initialDrawable = transparentDrawable;
        }
        BitmapDrawable bitmapDrawable = new BitmapDrawable(resources, bitmap);
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { initialDrawable, bitmapDrawable });
        imageView.setImageDrawable(td);
        td.startTransition(UIUtils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageBitmap(bitmap);
    }
}

From source file:Main.java

static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{
    if (sourceBitmap != null) {
        float R = Color.red(rgbcolor);
        float G = Color.green(rgbcolor);
        float B = Color.blue(rgbcolor);

        Log.v("R:G:B", R + ":" + G + ":" + B); //

        float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color
                0, G / 255f, 0, 0, 0 // G color
                , 0, 0, B / 255f, 0, 0 // B color
                , 0, 0, 0, 1f, 0f };/*from  w w w  . j a  v  a 2  s  .  co  m*/

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); // Remove Colour

        colorMatrix.set(colorTransform);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);

        Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        view.setImageBitmap(mutableBitmap);
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    }
}

From source file:com.android.volley.toolbox.ImageLoader.java

/**
 * The default implementation of ImageListener which handles basic functionality
 * of showing a default image until the network response is received, at which point
 * it will switch to either the actual image or the error image.
 * @param view The imageView that the listener is associated with.
 * @param defaultImageResId Default image resource ID to use, or 0 if it doesn't exist.
 * @param errorImageResId Error image resource ID to use, or 0 if it doesn't exist.
 *//*from  www.j a va2s  .co m*/
public static ImageListener getImageListener(final ImageView view, final int defaultImageResId,
        final int errorImageResId) {
    return new ImageListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (errorImageResId != 0) {
                view.setImageResource(errorImageResId);
            }
        }

        @Override
        public void onResponse(ImageContainer response, boolean isImmediate) {
            if (response.getBitmap() != null) {
                view.setImageBitmap(response.getBitmap());
            } else if (defaultImageResId != 0) {
                view.setImageResource(defaultImageResId);
            }
        }
    };
}

From source file:Main.java

public static void unbindImageView(ImageView imageView) {
    if (imageView == null) {
        return;/* w ww .  j  a  va2 s  .c  o  m*/
    }

    if (imageView.getBackground() != null) {
        imageView.getBackground().setCallback(null);
    }

    if (imageView.getDrawable() == null)
        return;
    if (!(imageView.getDrawable() instanceof BitmapDrawable))
        return;
    if (((BitmapDrawable) imageView.getDrawable()).getBitmap() == null)
        return;

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    try {
        if (drawable != null && imageView.getTag() != null && !drawable.getBitmap().isRecycled()) {
            if (!imageView.getTag().toString().equalsIgnoreCase("resource") && !usingMemoryCache) {
                drawable.getBitmap().recycle();
            }
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    drawable.setCallback(null);
    imageView.setImageBitmap(null);
    imageView.setImageDrawable(null);
}

From source file:com.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java

private static void setBmAnimate(ImageView iv, Bitmap bm, Bitmap preset, int fallback, int animation,
        float ratio, float anchor, int source) {
    bm = filter(iv, bm, fallback);//  ww w.ja  v a2  s.co m
    if (bm == null) {
        iv.setImageBitmap(null);
        return;
    }
    Drawable d = makeDrawable(iv, bm, ratio, anchor);
    Animation anim = null;
    if (fadeIn(animation, source)) {
        if (preset == null) {
            anim = new AlphaAnimation(0, 1);
            anim.setInterpolator(new DecelerateInterpolator());
            anim.setDuration(FADE_DUR);
        } else {
            Drawable pd = makeDrawable(iv, preset, ratio, anchor);
            Drawable[] ds = new Drawable[] { pd, d };
            TransitionDrawable td = new TransitionDrawable(ds);
            td.setCrossFadeEnabled(true);
            td.startTransition(FADE_DUR);
            d = td;
        }
    } else if (animation > 0) {
        anim = AnimationUtils.loadAnimation(iv.getContext(), animation);
    }
    iv.setImageDrawable(d);
    if (anim != null) {
        anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
        iv.startAnimation(anim);
    } else {
        iv.setAnimation(null);
    }
}

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imageView = (ImageView) findViewById(R.id.imageViewThumbnail);
    imageView.setImageBitmap(loadSampledResource(R.drawable.image_large, 100, 100));
}

From source file:MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_RESULT && resultCode == RESULT_OK) {
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(mLastPhotoURI.getPath()));

        if (data != null) {
            imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));

            try {
                imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(),
                        Uri.parse(data.toUri(Intent.URI_ALLOW_UNSAFE))));
            } catch (IOException e) {
                e.printStackTrace();// w ww.j a v  a  2  s. com
            }
        }

    }
}

From source file:com.icanvass.fragments.ScreenSlidePageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);

    // Set the title view to show the page number.
    //        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
    //                getString("step", mPageNumber + 1));
    ImageView iv = (ImageView) rootView.findViewById(R.id.tutimage);
    iv.setImageBitmap(null);
    iv.setImageResource(images[mPageNumber]);

    return rootView;
}