Example usage for android.widget ImageView setBackground

List of usage examples for android.widget ImageView setBackground

Introduction

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

Prototype

public void setBackground(Drawable background) 

Source Link

Document

Set the background to a given Drawable, or remove the background.

Usage

From source file:org.naturenet.ui.MainActivity.java

/**
 * Sets the gallery of recent images when the user selects 'add observation' button.
 *///from w w w. ja va  2  s  . c om
public void setGallery() {
    Picasso.with(MainActivity.this).cancelTag(ImageGalleryAdapter.class.getSimpleName());
    recentImageGallery = getRecentImagesUris();

    if (recentImageGallery.size() != 0) {
        gridview.setAdapter(new ImageGalleryAdapter(this, recentImageGallery));

        //Here we handle clicks to the recent images. Let user select as many images as they want to submit.
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ImageView iv = (ImageView) v.findViewById(R.id.gallery_iv);

                //if the image the user selects hasn't been selected yet
                if (!selectedImages.contains(recentImageGallery.get(position))) {
                    //add the clicked image to the selectedImages List
                    selectedImages.add(recentImageGallery.get(position));
                    iv.setBackground(
                            ContextCompat.getDrawable(MainActivity.this, R.drawable.border_selected_image));
                    select.setVisibility(View.VISIBLE);
                    //here we handle the case of selecting an image that's already been selected
                } else if (selectedImages.contains(recentImageGallery.get(position))) {
                    selectedImages.remove(recentImageGallery.get(position));
                    iv.setBackgroundResource(0);
                }

                //check to see if there are no selected images. if so, make select button 'unselectable'
                if (selectedImages.size() == 0)
                    select.setVisibility(View.GONE);

            }
        });
    }
}

From source file:kuta.adrian.cv.displayingbitmaps.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be
 * set on the ImageView./* ww w  .jav a2  s.  com*/
 *
 * @param imageView
 * @param drawable
 */
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    try {
        ViewGroup viewGroup = (ViewGroup) imageView.getParent();
        if (viewGroup instanceof FrameLayout) {
            FrameLayout parent = (FrameLayout) viewGroup;
            View view;
            if ((view = parent.getChildAt(0)) instanceof ProgressBar)
                parent.removeView(view);
        }
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "onPostExecute, progressBar not found", e);
    }

    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final drawable
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(ContextCompat.getColor(imageView.getContext(), android.R.color.transparent)),
                drawable });
        // Set background to loading bitmap
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));
        else
            imageView.setBackground(new BitmapDrawable(mResources, mLoadingBitmap));

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}

From source file:us.theparamountgroup.android.inventory.ShellCursorAdapter.java

/**
 * This method binds the shell data (in the current row pointed to by cursor) to the given
 * list item layout. For example, the name for the current shell can be set on the name TextView
 * in the list item layout./*from   w w  w.j  a v  a 2  s  .c  o m*/
 *
 * @param view    Existing view, returned earlier by newView() method
 * @param context app context
 * @param cursor  The cursor from which to get the data. The cursor is already moved to the
 *                correct row.
 */

@Override
public void bindView(View view, final Context context, Cursor cursor) {

    // Find individual views that we want to modify in the list item layout
    TextView nameTextView = (TextView) view.findViewById(R.id.name);
    TextView colorTextView = (TextView) view.findViewById(R.id.color);
    final TextView quantityTextView = (TextView) view.findViewById(R.id.quantity);
    TextView priceTextView = (TextView) view.findViewById(R.id.price);
    ImageView photoImageView = (ImageView) view.findViewById(R.id.thumbnail);
    Button saleButton = (Button) view.findViewById(R.id.sale_button);
    final String idColumn = cursor.getString(cursor.getColumnIndexOrThrow(ShellContract.ShellEntry._ID));
    final String nameColumn = cursor
            .getString(cursor.getColumnIndexOrThrow(ShellContract.ShellEntry.COLUMN_SHELL_NAME));
    final Uri currentProductUri = ContentUris.withAppendedId(ShellContract.ShellEntry.CONTENT_URI,
            Long.parseLong(idColumn));
    // Find the columns of shell attributes that we're interested in
    int nameColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_NAME);
    int colorColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_COLOR);
    int photoColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_PHOTO);
    int quantityColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_QUANTITY);
    int priceColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_PRICE);
    int thumbnailColumnIndex = cursor.getColumnIndex(ShellContract.ShellEntry.COLUMN_SHELL_THUMBNAIL);

    // Read the shell attributes from the Cursor for the current shell

    final String shellName = cursor.getString(nameColumnIndex);
    String shellColor = cursor.getString(colorColumnIndex);
    String shellQuantity = cursor.getString(quantityColumnIndex);
    String shellPrice = cursor.getString(priceColumnIndex);
    String photo = cursor.getString(photoColumnIndex);

    // check if there is photo saved for item and assign the saved thumbnail to photoImageView
    if (!photo.isEmpty()) {
        try {
            byte[] thumbnail = cursor.getBlob(thumbnailColumnIndex);
            Bitmap thumbImage = getImage(thumbnail);
            photoImageView.setBackground(null);
            int cornerRadius = 10;
            RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(context.getResources(), thumbImage);
            dr.setCornerRadius(cornerRadius);
            photoImageView.setImageDrawable(dr);

        } catch (Exception e) {
            Log.e("ThumbnailUtils", "Problem extracting thumbnail", e);
            e.printStackTrace();
        }
    }

    // If the shell color is empty string or null, then use some default text
    // that says "Unknown color", so the TextView isn't blank.
    if (TextUtils.isEmpty(shellColor)) {
        shellColor = context.getString(R.string.unknown_color);
    }
    ;

    saleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int quantity;
            if (quantityTextView.getText().toString().isEmpty()) {
                quantity = 0;
            } else {
                quantity = Integer.parseInt(quantityTextView.getText().toString());
            }
            if (quantity > 0) {
                quantity = quantity - 1;
                quantityTextView.setText(String.valueOf(quantity));

                ContentValues values = new ContentValues();
                // values.put(ShellContract.ShellEntry.COLUMN_SHELL_NAME, shellName);
                values.put(ShellContract.ShellEntry.COLUMN_SHELL_QUANTITY, quantity);
                //values.put(ProductEntry.COLUMN_PRODUCT_PRICE, priceColumn);
                //values.put(ProductEntry.COLUMN_PRODUCT_PHOTO, photoColumn);

                int rowsAffected = context.getContentResolver().update(currentProductUri, values, null, null);
                if (rowsAffected == 0) {
                    // Toast.makeText(v.getContext(), v.getContext().getString("error updating"), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(v.getContext(), "Sale Product " + nameColumn, Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(v.getContext(), "Order Product", Toast.LENGTH_SHORT).show();
            }
        }
    });

    // Update the TextViews with the attributes for the current shell
    nameTextView.setText(shellName);
    colorTextView.setText(shellColor);
    quantityTextView.setText(shellQuantity);
    priceTextView.setText("$" + shellPrice);
}

From source file:com.musenkishi.atelier.Atelier.java

private static void applyColorToView(final ImageView imageView, int color, boolean fromCache,
        boolean shouldMask) {
    if (fromCache) {
        if (shouldMask) {
            if (imageView.getDrawable() != null) {
                imageView.getDrawable().mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            } else if (imageView.getBackground() != null) {
                imageView.getBackground().mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            }//from   www . ja  va2  s. c  o  m
        } else {
            imageView.setBackgroundColor(color);
        }
    } else {
        if (shouldMask) {
            Integer colorFrom;
            ValueAnimator.AnimatorUpdateListener imageAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    if (imageView.getDrawable() != null) {
                        imageView.getDrawable().mutate().setColorFilter(
                                (Integer) valueAnimator.getAnimatedValue(), PorterDuff.Mode.MULTIPLY);
                    } else if (imageView.getBackground() != null) {
                        imageView.getBackground().mutate().setColorFilter(
                                (Integer) valueAnimator.getAnimatedValue(), PorterDuff.Mode.MULTIPLY);
                    }
                }
            };
            ValueAnimator.AnimatorUpdateListener animatorUpdateListener;

            PaletteTag paletteTag = (PaletteTag) imageView.getTag(viewTagKey);
            animatorUpdateListener = imageAnimatorUpdateListener;
            colorFrom = paletteTag.getColor();
            imageView.setTag(viewTagKey, new PaletteTag(paletteTag.getId(), color));

            Integer colorTo = color;
            ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
            colorAnimation.addUpdateListener(animatorUpdateListener);
            colorAnimation.setDuration(300);
            colorAnimation.start();
        } else {
            Drawable preDrawable;

            if (imageView.getBackground() == null) {
                preDrawable = new ColorDrawable(Color.TRANSPARENT);
            } else {
                preDrawable = imageView.getBackground();
            }

            TransitionDrawable transitionDrawable = new TransitionDrawable(
                    new Drawable[] { preDrawable, new ColorDrawable(color) });
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                imageView.setBackground(transitionDrawable);
            } else {
                imageView.setBackgroundDrawable(transitionDrawable);
            }
            transitionDrawable.startTransition(300);
        }
    }
}

From source file:lu.fisch.canze.activities.MainActivity.java

private void setBluetoothState(int btState) {
    if (bluetoothMenutItem != null) {
        final ImageView imageView = (ImageView) bluetoothMenutItem.getActionView()
                .findViewById(R.id.animated_menu_item_action);

        // stop the animation if there is one running
        AnimationDrawable frameAnimation;
        if (imageView.getBackground() instanceof AnimationDrawable) {
            frameAnimation = (AnimationDrawable) imageView.getBackground();
            if (frameAnimation.isRunning())
                frameAnimation.stop();//  www  .j a  v  a 2 s . com
        }

        switch (btState) {
        case BLUETOOTH_DISCONNECTED:
            imageView.setBackgroundResource(R.mipmap.bluetooth_none);
            break;
        case BLUETOOTH_CONNECTED:
            imageView.setBackgroundResource(R.mipmap.bluetooth_3);
            break;
        case BLUETOOTH_SEARCH:
            runOnUiThread(new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
                    AnimationDrawable drawable = (AnimationDrawable) ContextCompat
                            .getDrawable(getApplicationContext(), R.anim.animation_bluetooth);
                    // Use setBackgroundDrawable() for API 14 and 15 and setBackground() for API 16+:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        imageView.setBackground(drawable);
                    } else {
                        //noinspection deprecation
                        imageView.setBackgroundDrawable(drawable);
                    }
                    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
                    frameAnimation.start();
                }
            });
            break;
        default:
            break;
        }
    }
}