Example usage for android.widget ImageView setTag

List of usage examples for android.widget ImageView setTag

Introduction

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

Prototype

public void setTag(final Object tag) 

Source Link

Document

Sets the tag associated with this view.

Usage

From source file:com.google.android.apps.santatracker.rocketsleigh.RocketSleighActivity.java

private void addCaveObstacles(int screens) {
    int totalSlots = screens * SLOTS_PER_SCREEN;
    for (int i = 0; i < totalSlots;) {
        // Any given "slot" has a 1 in 3 chance of having an obstacle
        if (mRandom.nextInt(2) == 0) {
            View view = mInflater.inflate(R.layout.obstacle_layout, null);
            ImageView top = (ImageView) view.findViewById(R.id.top_view);
            ImageView bottom = (ImageView) view.findViewById(R.id.bottom_view);
            ImageView back = (ImageView) view.findViewById(R.id.back_view);

            // Which obstacle?
            int width = 0;
            if ((mCaveObstacleIndex % 20) == 0) {
                ObstacleLoadTask task = new ObstacleLoadTask(getResources(), CAVE_OBSTACLES, mCaveObstacles,
                        mCaveObstacleList, mCaveObstacleIndex + 20, 2, mScaleX, mScaleY);
                task.execute();/*from   w  w  w  .  j a  va 2s.c o  m*/
            }
            int obstacle = mCaveObstacleList.get(mCaveObstacleIndex++);
            if (mCaveObstacleIndex >= mCaveObstacleList.size()) {
                mCaveObstacleIndex = 0;
            }
            //                int obstacle = mRandom.nextInt((CAVE_OBSTACLES.length/2));
            int topIndex = obstacle * 2;
            int bottomIndex = topIndex + 1;
            back.setVisibility(View.GONE);

            int currentObstacle = 0; // Same values as mLastObstacle
            if (CAVE_OBSTACLES[topIndex] != -1) {
                currentObstacle |= 1;
                Bitmap bmp = null;
                synchronized (mCaveObstacles) {
                    bmp = mCaveObstacles.get(CAVE_OBSTACLES[topIndex]);
                }
                while (bmp == null) {
                    synchronized (mCaveObstacles) {
                        bmp = mCaveObstacles.get(CAVE_OBSTACLES[topIndex]);
                        if (bmp == null) {
                            try {
                                mCaveObstacles.wait();
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                }
                width = bmp.getWidth();
                top.setImageBitmap(bmp);
            } else {
                top.setVisibility(View.GONE);
            }

            if (CAVE_OBSTACLES[bottomIndex] != -1) {
                currentObstacle |= 2;
                Bitmap bmp = null;
                synchronized (mCaveObstacles) {
                    bmp = mCaveObstacles.get(CAVE_OBSTACLES[bottomIndex]);
                }
                while (bmp == null) {
                    synchronized (mCaveObstacles) {
                        bmp = mCaveObstacles.get(CAVE_OBSTACLES[bottomIndex]);
                        if (bmp == null) {
                            try {
                                mCaveObstacles.wait();
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                }
                if (bmp.getWidth() > width) {
                    width = bmp.getWidth();
                }
                bottom.setImageBitmap(bmp);
                if (CAVE_OBSTACLES[bottomIndex] == R.drawable.img_mammoth) {
                    // Special case...
                    bottom.setTag(true);
                }
            } else {
                bottom.setVisibility(View.GONE);
            }
            int slots = (width / mSlotWidth);
            slots += 2;

            // If last obstacle had a top and this is a bottom or vice versa, insert a space
            if ((mLastObstacle & 0x1) > 0) {
                if ((currentObstacle & 0x2) > 0) {
                    addSpaceOrPresent(mSlotWidth);
                    i++;
                }
            } else if ((mLastObstacle & 0x2) > 0) {
                if ((currentObstacle & 0x1) > 0) {
                    addSpaceOrPresent(mSlotWidth);
                    i++;
                }
            }

            // If the new obstacle is too wide for the remaining space, skip it and fill spacer instead
            if ((i + slots) > totalSlots) {
                addSpaceOrPresent(mSlotWidth * (totalSlots - i));
                i = totalSlots;
            } else {
                mLastObstacle = currentObstacle;
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(slots * mSlotWidth,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(lp);
                mObstacleLayout.addView(view);
                i += slots;
            }
        } else {
            addSpaceOrPresent(mSlotWidth);
            i++;
        }
    }

    // Account for rounding errors in mSlotWidth
    int extra = ((screens * mScreenWidth) - (totalSlots * mSlotWidth));
    if (extra > 0) {
        // Add filler to ensure sync with background/foreground scrolls!
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(extra,
                LinearLayout.LayoutParams.MATCH_PARENT);
        View view = new View(this);
        mObstacleLayout.addView(view, lp);
    }
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

public void showPreviews(final View anchor, int start, int end) {
    if (mWorkspace != null && mWorkspace.getChildCount() > 0) {
        if (newPreviews) {
            showingPreviews = true;// w w  w .j a v  a2  s.c o  m
            hideDesktop(true);
            mWorkspace.lock();
            mWorkspace.openSense(true);
        } else {
            // check first if it's already open
            final PopupWindow window = (PopupWindow) anchor.getTag(R.id.TAG_PREVIEW);
            if (window != null)
                return;
            Resources resources = getResources();

            PersonaWorkspace personaWorkspace = mWorkspace;
            PersonaCellLayout cell = ((PersonaCellLayout) personaWorkspace.getChildAt(start));
            float max;
            ViewGroup preview;
            max = personaWorkspace.getChildCount();
            preview = new LinearLayout(this);

            Rect r = new Rect();
            // ADW: seems sometimes this throws an out of memory error....
            // so...
            try {
                resources.getDrawable(R.drawable.pr_preview_background).getPadding(r);
            } catch (OutOfMemoryError e) {
            }
            int extraW = (int) ((r.left + r.right) * max);
            int extraH = r.top + r.bottom;

            int aW = cell.getWidth() - extraW;
            float w = aW / max;

            int width = cell.getWidth();
            int height = cell.getHeight();
            // width -= (x + cell.getRightPadding());
            // height -= (y + cell.getBottomPadding());
            if (width != 0 && height != 0) {
                showingPreviews = true;
                float scale = w / width;

                int count = end - start;

                final float sWidth = width * scale;
                float sHeight = height * scale;

                PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
                ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);

                for (int i = start; i < end; i++) {
                    ImageView image = new ImageView(this);
                    cell = (PersonaCellLayout) personaWorkspace.getChildAt(i);
                    Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight, Bitmap.Config.ARGB_8888);
                    cell.setDrawingCacheEnabled(false);
                    Canvas c = new Canvas(bitmap);
                    c.scale(scale, scale);
                    c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
                    cell.dispatchDraw(c);

                    image.setBackgroundDrawable(resources.getDrawable(R.drawable.pr_preview_background));
                    image.setImageBitmap(bitmap);
                    image.setTag(i);
                    image.setOnClickListener(handler);
                    image.setOnFocusChangeListener(handler);
                    image.setFocusable(true);
                    if (i == mWorkspace.getCurrentScreen())
                        image.requestFocus();

                    preview.addView(image, LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);

                    bitmaps.add(bitmap);
                }

                PopupWindow p = new PopupWindow(this);
                p.setContentView(preview);
                p.setWidth((int) (sWidth * count + extraW));
                p.setHeight((int) (sHeight + extraH));
                p.setAnimationStyle(R.style.AnimationPreview);
                p.setOutsideTouchable(true);
                p.setFocusable(true);
                p.setBackgroundDrawable(new ColorDrawable(0));
                p.showAsDropDown(anchor, 0, 0);
                p.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    public void onDismiss() {
                        dismissPreview(anchor);
                    }
                });
                anchor.setTag(R.id.TAG_PREVIEW, p);
                anchor.setTag(R.id.workspace, preview);
                anchor.setTag(R.id.icon, bitmaps);
            }
        }
    }
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

/**
 * ADW: Create a copy of an folder icon with a reflection
 * //  w  ww. j a  v a  2 s . c om
 * @param layoutResId
 * @param parent
 * @param info
 * @return
 */
View createSmallFolder(int layoutResId, ViewGroup parent, PersonaUserFolderInfo info) {
    ImageView favorite = (ImageView) mInflater.inflate(layoutResId, parent, false);

    final Resources resources = getResources();
    // Drawable d = resources.getDrawable(R.drawable.ic_launcher_folder);
    Drawable d = null;
    if (PersonaAlmostNexusSettingsHelper.getThemeIcons(this)) {
        String packageName = PersonaAlmostNexusSettingsHelper.getThemePackageName(this, THEME_DEFAULT);
        if (packageName.equals(THEME_DEFAULT)) {
            d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
        } else {
            d = PersonaFolderIcon.loadFolderFromTheme(this, getPackageManager(), packageName,
                    "ic_launcher_folder");
            if (d == null) {
                d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
            }
        }
    } else {
        d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
    }
    d = PersonaUtilities.drawReflection(d, this);
    favorite.setImageDrawable(d);
    PersonaLog.d("personalauncher", "------settag done in createsmallfolder-------");
    favorite.setTag(info);
    favorite.setOnClickListener(this);
    return favorite;
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

/**
 * ADW: Create a copy of an PersonaLiveFolder icon with a reflection
 * // w ww. j a  va  2 s.com
 * @param layoutResId
 * @param parent
 * @param info
 * @return
 */
View createSmallLiveFolder(int layoutResId, ViewGroup parent, PersonaLiveFolderInfo info) {
    ImageView favorite = (ImageView) mInflater.inflate(layoutResId, parent, false);

    final Resources resources = getResources();
    Drawable d = info.icon;
    if (d == null) {
        if (PersonaAlmostNexusSettingsHelper.getThemeIcons(this)) {
            // Drawable d =
            // resources.getDrawable(R.drawable.ic_launcher_folder);
            String packageName = PersonaAlmostNexusSettingsHelper.getThemePackageName(this, THEME_DEFAULT);
            if (packageName.equals(THEME_DEFAULT)) {
                d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
            } else {
                d = PersonaFolderIcon.loadFolderFromTheme(this, getPackageManager(), packageName,
                        "ic_launcher_folder");
                if (d == null) {
                    d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
                }
            }
        } else {
            d = resources.getDrawable(R.drawable.pr_ic_launcher_folder);
        }
        info.filtered = true;
    }
    d = PersonaUtilities.drawReflection(d, this);
    favorite.setImageDrawable(d);
    PersonaLog.d("personalauncher", "------settag done in createsmalllivefolder-------");
    favorite.setTag(info);
    favorite.setOnClickListener(this);
    return favorite;
}

From source file:com.nttec.everychan.ui.presentation.BoardFragment.java

@SuppressLint("InlinedApi")
private void openGridGallery() {
    final int tnSize = resources.getDimensionPixelSize(R.dimen.post_thumbnail_size);

    class GridGalleryAdapter extends ArrayAdapter<Triple<AttachmentModel, String, String>>
            implements View.OnClickListener, AbsListView.OnScrollListener {
        private final GridView view;
        private boolean selectingMode = false;
        private boolean[] isSelected = null;
        private volatile boolean isBusy = false;

        public GridGalleryAdapter(GridView view, List<Triple<AttachmentModel, String, String>> list) {
            super(activity, 0, list);
            this.view = view;
            this.isSelected = new boolean[list.size()];
        }// w  w w . j a va 2 s .  c  o m

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                if (isBusy)
                    setNonBusy();
                isBusy = false;
            } else
                isBusy = true;
        }

        private void setNonBusy() {
            if (!downloadThumbnails())
                return;
            for (int i = 0; i < view.getChildCount(); ++i) {
                View v = view.getChildAt(i);
                Object tnTag = v.findViewById(R.id.post_thumbnail_image).getTag();
                if (tnTag == null || tnTag == Boolean.FALSE)
                    fill(view.getPositionForView(v), v, false);
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new FrameLayout(activity);
                convertView.setLayoutParams(new AbsListView.LayoutParams(tnSize, tnSize));
                ImageView tnImage = new ImageView(activity);
                tnImage.setLayoutParams(new FrameLayout.LayoutParams(tnSize, tnSize, Gravity.CENTER));
                tnImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                tnImage.setId(R.id.post_thumbnail_image);
                ((FrameLayout) convertView).addView(tnImage);
            }
            convertView.setTag(getItem(position).getLeft());
            safeRegisterForContextMenu(convertView);
            convertView.setOnClickListener(this);
            fill(position, convertView, isBusy);
            if (isSelected[position]) {
                /*ImageView overlay = new ImageView(activity);
                overlay.setImageResource(android.R.drawable.checkbox_on_background);*/
                FrameLayout overlay = new FrameLayout(activity);
                overlay.setBackgroundColor(Color.argb(128, 0, 255, 0));
                if (((FrameLayout) convertView).getChildCount() < 2)
                    ((FrameLayout) convertView).addView(overlay);

            } else {
                if (((FrameLayout) convertView).getChildCount() > 1)
                    ((FrameLayout) convertView).removeViewAt(1);
            }
            return convertView;
        }

        private void safeRegisterForContextMenu(View view) {
            try {
                view.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                    @Override
                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                        if (presentationModel == null) {
                            Fragment currentFragment = MainApplication
                                    .getInstance().tabsSwitcher.currentFragment;
                            if (currentFragment instanceof BoardFragment) {
                                currentFragment.onCreateContextMenu(menu, v, menuInfo);
                            }
                        } else {
                            BoardFragment.this.onCreateContextMenu(menu, v, menuInfo);
                        }
                    }
                });
            } catch (Exception e) {
                Logger.e(TAG, e);
            }
        }

        @Override
        public void onClick(View v) {
            if (selectingMode) {
                int position = view.getPositionForView(v);
                isSelected[position] = !isSelected[position];
                notifyDataSetChanged();
            } else {
                BoardFragment fragment = BoardFragment.this;
                if (presentationModel == null) {
                    Fragment currentFragment = MainApplication.getInstance().tabsSwitcher.currentFragment;
                    if (currentFragment instanceof BoardFragment)
                        fragment = (BoardFragment) currentFragment;
                }
                fragment.openAttachment((AttachmentModel) v.getTag());
            }
        }

        private void fill(int position, View view, boolean isBusy) {
            AttachmentModel attachment = getItem(position).getLeft();
            String attachmentHash = getItem(position).getMiddle();
            ImageView tnImage = (ImageView) view.findViewById(R.id.post_thumbnail_image);
            if (attachment.thumbnail == null || attachment.thumbnail.length() == 0) {
                tnImage.setTag(Boolean.TRUE);
                tnImage.setImageResource(Attachments.getDefaultThumbnailResId(attachment.type));
                return;
            }
            tnImage.setTag(Boolean.FALSE);
            CancellableTask imagesDownloadTask = BoardFragment.this.imagesDownloadTask;
            ExecutorService imagesDownloadExecutor = BoardFragment.this.imagesDownloadExecutor;
            if (presentationModel == null) {
                Fragment currentFragment = MainApplication.getInstance().tabsSwitcher.currentFragment;
                if (currentFragment instanceof BoardFragment) {
                    imagesDownloadTask = ((BoardFragment) currentFragment).imagesDownloadTask;
                    imagesDownloadExecutor = ((BoardFragment) currentFragment).imagesDownloadExecutor;
                }
            }
            bitmapCache.asyncGet(attachmentHash, attachment.thumbnail, tnSize, chan, localFile,
                    imagesDownloadTask, tnImage, imagesDownloadExecutor, Async.UI_HANDLER,
                    downloadThumbnails() && !isBusy,
                    downloadThumbnails() ? (isBusy ? 0 : R.drawable.thumbnail_error)
                            : Attachments.getDefaultThumbnailResId(attachment.type));
        }

        public void setSelectingMode(boolean selectingMode) {
            this.selectingMode = selectingMode;
            if (!selectingMode) {
                Arrays.fill(isSelected, false);
                notifyDataSetChanged();
            }
        }

        public void selectAll() {
            if (selectingMode) {
                Arrays.fill(isSelected, true);
                notifyDataSetChanged();
            }
        }

        public void downloadSelected(final Runnable onFinish) {
            final Dialog progressDialog = ProgressDialog.show(activity,
                    resources.getString(R.string.grid_gallery_dlg_title),
                    resources.getString(R.string.grid_gallery_dlg_message), true, false);
            Async.runAsync(new Runnable() {
                @Override
                public void run() {
                    BoardFragment fragment = BoardFragment.this;
                    if (fragment.presentationModel == null) {
                        Fragment currentFragment = MainApplication.getInstance().tabsSwitcher.currentFragment;
                        if (currentFragment instanceof BoardFragment)
                            fragment = (BoardFragment) currentFragment;
                    }
                    boolean flag = false;
                    for (int i = 0; i < isSelected.length; ++i)
                        if (isSelected[i])
                            if (!fragment.downloadFile(getItem(i).getLeft(), true))
                                flag = true;
                    final boolean toast = flag;
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (toast)
                                Toast.makeText(activity, R.string.notification_download_exists_or_in_queue,
                                        Toast.LENGTH_LONG).show();
                            progressDialog.dismiss();
                            onFinish.run();
                        }
                    });
                }
            });
        }
    }

    try {
        List<Triple<AttachmentModel, String, String>> list = presentationModel.getAttachments();
        if (list == null) {
            Toast.makeText(activity, R.string.notifacation_updating_now, Toast.LENGTH_LONG).show();
            return;
        }

        GridView grid = new GridView(activity);
        final GridGalleryAdapter gridAdapter = new GridGalleryAdapter(grid, list);
        grid.setNumColumns(GridView.AUTO_FIT);
        grid.setColumnWidth(tnSize);
        int spacing = (int) (resources.getDisplayMetrics().density * 5 + 0.5f);
        grid.setVerticalSpacing(spacing);
        grid.setHorizontalSpacing(spacing);
        grid.setPadding(spacing, spacing, spacing, spacing);
        grid.setAdapter(gridAdapter);
        grid.setOnScrollListener(gridAdapter);
        grid.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f));

        final Button btnToSelecting = new Button(activity);
        btnToSelecting.setText(R.string.grid_gallery_select);
        CompatibilityUtils.setTextAppearance(btnToSelecting, android.R.style.TextAppearance_Small);
        btnToSelecting.setSingleLine();
        btnToSelecting.setVisibility(View.VISIBLE);
        btnToSelecting.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        final LinearLayout layoutSelectingButtons = new LinearLayout(activity);
        layoutSelectingButtons.setOrientation(LinearLayout.HORIZONTAL);
        layoutSelectingButtons.setWeightSum(10f);
        Button btnDownload = new Button(activity);
        btnDownload.setLayoutParams(
                new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 3.25f));
        btnDownload.setText(R.string.grid_gallery_download);
        CompatibilityUtils.setTextAppearance(btnDownload, android.R.style.TextAppearance_Small);
        btnDownload.setSingleLine();
        Button btnSelectAll = new Button(activity);
        btnSelectAll.setLayoutParams(
                new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 3.75f));
        btnSelectAll.setText(android.R.string.selectAll);
        CompatibilityUtils.setTextAppearance(btnSelectAll, android.R.style.TextAppearance_Small);
        btnSelectAll.setSingleLine();
        Button btnCancel = new Button(activity);
        btnCancel.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 3f));
        btnCancel.setText(android.R.string.cancel);
        CompatibilityUtils.setTextAppearance(btnCancel, android.R.style.TextAppearance_Small);
        btnCancel.setSingleLine();
        layoutSelectingButtons.addView(btnDownload);
        layoutSelectingButtons.addView(btnSelectAll);
        layoutSelectingButtons.addView(btnCancel);
        layoutSelectingButtons.setVisibility(View.GONE);
        layoutSelectingButtons.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        btnToSelecting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnToSelecting.setVisibility(View.GONE);
                layoutSelectingButtons.setVisibility(View.VISIBLE);
                gridAdapter.setSelectingMode(true);
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnToSelecting.setVisibility(View.VISIBLE);
                layoutSelectingButtons.setVisibility(View.GONE);
                gridAdapter.setSelectingMode(false);
            }
        });

        btnSelectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridAdapter.selectAll();
            }
        });

        btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridAdapter.downloadSelected(new Runnable() {
                    @Override
                    public void run() {
                        btnToSelecting.setVisibility(View.VISIBLE);
                        layoutSelectingButtons.setVisibility(View.GONE);
                        gridAdapter.setSelectingMode(false);
                    }
                });
            }
        });

        LinearLayout dlgLayout = new LinearLayout(activity);
        dlgLayout.setOrientation(LinearLayout.VERTICAL);
        dlgLayout.addView(btnToSelecting);
        dlgLayout.addView(layoutSelectingButtons);
        dlgLayout.addView(grid);

        Dialog gridDialog = new Dialog(activity);
        gridDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        gridDialog.setContentView(dlgLayout);
        gridDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        gridDialog.show();
    } catch (OutOfMemoryError oom) {
        MainApplication.freeMemory();
        Logger.e(TAG, oom);
        Toast.makeText(activity, R.string.error_out_of_memory, Toast.LENGTH_LONG).show();
    }
}