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.admin.viewpageexample.SlidingTab.SlidingTabLayout.java

private void populateTabStrip() {
    final ViewpagerAdapter adapter = (ViewpagerAdapter) mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;//w w  w. j  av  a 2s .co  m
        ImageView tabIconView = null;
        if (tabView == null) {
            tabView = createDefaultImageView(getContext());
        }
        if (tabIconView == null && ImageView.class.isInstance(tabView)) {
            tabIconView = (ImageView) tabView;
        }

        tabIconView.setImageDrawable(getResources().getDrawable(adapter.getDrawableId(i)));
        if (mViewPager.getCurrentItem() == i) {
            tabIconView.setSelected(true);
        }
        //            tabView.setOnClickListener(tabClickListener);
        final int index_i = i;

        tabView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mViewPager.setCurrentItem(index_i);

            }
        });

        mTabStrip.addView(tabView);
        //tabTitleView.setTextColor(getResources().getColorStateList(R.color.selector));

    }
}

From source file:com.velia_systems.menuphoto.connection.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *//*from   www  .  j ava  2s .co  m*/
private void forceDownload(String url, ImageView imageView, Bitmap bitmap, ProgressBar progress) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {
        switch (mode) {
        case NO_ASYNC_TASK:
            bitmap = downloadBitmap(url);
            //                    Log.d("ImageDownloader", "Sizes: " + bitmap.getWidth() + "x" + bitmap.getHeight());
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            int destHeight = 100 * height / width;

            Bitmap bmp1 = Bitmap.createScaledBitmap(bitmap, 100, destHeight, false);
            bitmap.recycle();

            addBitmapToCache(url, bmp1);
            imageView.setImageBitmap(bmp1);
            progress.setVisibility(View.GONE);
            break;

        case NO_DOWNLOADED_DRAWABLE:
            imageView.setMinimumHeight(50);
            BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, progress);
            task.execute(url);
            break;

        case CORRECT:
            task = new BitmapDownloaderTask(imageView, progress);
            DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumHeight(50);
            task.execute(url);
            break;
        }
    }
}

From source file:com.poloniumarts.utils.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not
 * used. Kept private at the moment as its interest is not clear.
 *///  w w  w.j a v  a  2s  . c  om
private void forceDownload(String url, ImageView imageView) {
    // State sanity: url is guaranteed to never be null in
    // DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    task = new BitmapDownloaderTask(imageView);
    DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
    imageView.setImageDrawable(downloadedDrawable);
    task.execute(url);

}

From source file:com.binomed.showtime.android.util.images.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used. Kept private at the moment as its interest is not clear.
 *///  ww w. ja v a  2s  . c  o  m
private void forceDownload(String url, ImageView imageView, Context context) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    Bitmap image = getFileDrawable(url);
    if (image != null) {
        imageView.setImageBitmap(image);
    } else if (cancelPotentialDownload(url, imageView)) {
        switch (mode) {
        case NO_ASYNC_TASK:
            Bitmap bitmap = downloadBitmap(url);
            addBitmapToCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
            break;

        case NO_DOWNLOADED_DRAWABLE:
            imageView.setMinimumHeight(156);
            BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
            task.execute(url);
            break;

        case CORRECT:
            task = new BitmapDownloaderTask(imageView);
            DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task, context);
            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumHeight(156);
            task.execute(url);
            break;
        }
    }
}

From source file:com.life.wuhan.util.ImageDownloader.java

private void forceDownload(String url, ImageView imageView) {
    if (url == null) {
        imageView.setImageDrawable(null);
        return;//  ww  w . ja  va2s  .  com
    }

    if (cancelPotentialDownload(url, imageView)) {
        switch (mode) {
        case NO_ASYNC_TASK:
            Bitmap bitmap = downloadBitmap(url);
            addBitmapToCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
            break;

        case NO_DOWNLOADED_DRAWABLE:
            imageView.setMinimumHeight(156);
            BitmapDownloaderTask task = null;

            task = new BitmapDownloaderTask(imageView);

            task.execute(url);
            break;

        case CORRECT:

            task = new BitmapDownloaderTask(imageView);
            DownloadedDrawable downloadedDrawable = new DownloadedDrawable(imageView.getResources(), task);
            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumHeight(156);
            task.execute(url);
            break;
        }
    }
}

From source file:se.dw.okhttpwrapper.ImageRequest.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *//*from ww w .  j  a va 2 s  . c om*/
private void forceDownload(String url, ImageView imageView) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {

        switch (mode) {
        case NO_ASYNC_TASK:
            Bitmap bitmap = downloadBitmap(url);
            addBitmapToCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
            break;

        case CORRECT:
            BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
            DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
            imageView.setImageDrawable(downloadedDrawable);
            imageView.setMinimumHeight(156);
            task.execute(url);
            break;
        }

    }
}

From source file:android.support.v17.leanback.supportleanbackshowcase.cards.TextCardView.java

public void updateUi(Card card) {
    TextView extraText = (TextView) findViewById(R.id.extra_text);
    TextView primaryText = (TextView) findViewById(R.id.primary_text);
    final ImageView imageView = (ImageView) findViewById(R.id.main_image);

    extraText.setText(card.getExtraText());
    primaryText.setText(card.getTitle());

    // Create a rounded drawable.
    int resourceId = card.getLocalImageResourceId(getContext());
    Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), resourceId);
    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap);
    drawable.setAntiAlias(true);/*from   w ww . ja va  2s  .  c om*/
    drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
    imageView.setImageDrawable(drawable);
}

From source file:kr.co.WhenWhereWho3.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *//*from w  w w . j a va 2  s  .c o  m*/
private void forceDownload(String url, ImageView imageView) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    //      set ( forground  null  )
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    //         
    if (cancelPotentialDownload(url, imageView)) {
        //     AsyncTack  
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
        //        Drawble .
        //       task 
        DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
        //        (   )   .
        imageView.setImageDrawable(downloadedDrawable);
        //      
        imageView.setMinimumHeight(60);
        //   task .
        task.execute(url);
    }
}

From source file:com.userhook.view.UHMessageView.java

protected void loadMessage(Map<String, Object> params) {

    if (meta.getDisplayType().equals(UHMessageMeta.TYPE_IMAGE)) {

        if (meta.getButton1() != null && meta.getButton1().getImage() != null
                && meta.getButton1().getImage().getUrl() != null) {

            AsyncTask task = new AsyncTask<Object, Void, Drawable>() {

                @Override/*from  ww w .  j  a v  a 2s.  co m*/
                protected Drawable doInBackground(Object... params) {
                    Drawable drawable = null;

                    try {

                        URL url = new URL((String) params[0]);

                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        InputStream is = conn.getInputStream();

                        drawable = Drawable.createFromStream(is, "src");

                        int height = drawable.getIntrinsicHeight();
                        int width = drawable.getIntrinsicWidth();

                        drawable.setBounds(0, 0, width, height);

                    } catch (Exception e) {
                        Log.e(UserHook.TAG, "error download message image", e);
                    }

                    return drawable;
                }

                @Override
                protected void onPostExecute(Drawable result) {

                    if (result != null) {

                        // size image to fit inside the view
                        int screenHeight = getResources().getDisplayMetrics().heightPixels;
                        int screenWidth = getResources().getDisplayMetrics().widthPixels;

                        int heightGutter = 40;
                        int widthGutter = 40;

                        int screenSpaceHeight = screenHeight - heightGutter * 2;
                        int screenSpaceWidth = screenWidth - widthGutter * 2;

                        float height = result.getIntrinsicHeight();
                        float width = result.getIntrinsicWidth();
                        float aspect = height / width;

                        if (height > screenSpaceHeight) {
                            height = screenHeight;
                            width = height / aspect;
                        }

                        if (width > screenSpaceWidth) {
                            width = screenSpaceWidth;
                            height = width * aspect;
                        }

                        ImageView imageView = new ImageView(getContext());
                        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        imageView.setImageDrawable(result);

                        LayoutParams layoutParams = new LayoutParams((int) width, (int) height);
                        layoutParams.addRule(CENTER_IN_PARENT);
                        addView(imageView, layoutParams);

                        // add click handler to image
                        imageView.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (meta.getButton1() != null) {
                                    clickedButton(meta.getButton1());
                                }
                            }
                        });

                        contentLoaded = true;

                        if (showAfterLoad) {
                            showDialog();
                        }

                    }
                }
            };

            task.execute(meta.getButton1().getImage().getUrl());

        }

    } else if (UHMessageTemplate.getInstance().hasTemplate(meta.getDisplayType())) {

        String html = UHMessageTemplate.getInstance().renderTemplate(meta);

        loadWebViewContent(html);

        if (showAfterLoad) {
            showDialog();
        }

    } else {

        UHPostAsyncTask asyncTask = new UHPostAsyncTask(params, new UHAsyncTask.UHAsyncTaskListener() {
            @Override
            public void onSuccess(String result) {

                if (result != null) {

                    loadWebViewContent(result);

                }

                if (showAfterLoad) {
                    showDialog();
                }
            }
        });

        asyncTask.execute(UserHook.UH_HOST_URL + UH_MESSAGE_PATH);

    }

}

From source file:com.ndn.menurandom.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *//*from   w  ww  .  ja va2  s  . c om*/
private void forceDownload(String url, ImageView imageView) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
        DownloadedDrawable downloadedDrawable = null;
        if (mContext != null) {
            Resources res = mContext.getResources();
            Drawable d = res.getDrawable(mDefaultImageResourceId);
            if (d instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
                downloadedDrawable = new DownloadedDrawable(res, bitmap, task);
            }
        }

        imageView.setImageDrawable(downloadedDrawable);
        imageView.setMinimumHeight(156);
        task.execute(url);
    }
}