Example usage for android.widget ImageView getDrawable

List of usage examples for android.widget ImageView getDrawable

Introduction

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

Prototype

public Drawable getDrawable() 

Source Link

Document

Gets the current Drawable, or null if no Drawable has been assigned.

Usage

From source file:com.doomy.padlock.MainActivity.java

private void openAboutDialog() {
    LayoutInflater mLayoutInflater = LayoutInflater.from(this);
    View mView = mLayoutInflater.inflate(R.layout.view_about, null);

    ImageView mImageViewMrDoomy = (ImageView) mView.findViewById(R.id.imageViewMrDoomy);
    ImageView mImageViewStudio = (ImageView) mView.findViewById(R.id.imageViewStudio);
    ImageView mImageViewGitHub = (ImageView) mView.findViewById(R.id.imageViewGitHub);
    Drawable mMrDoomy = mImageViewMrDoomy.getDrawable();
    Drawable mStudio = mImageViewStudio.getDrawable();
    Drawable mGitHub = mImageViewGitHub.getDrawable();
    mMrDoomy.setColorFilter(getResources().getColor(R.color.orangeDark), PorterDuff.Mode.SRC_ATOP);
    mStudio.setColorFilter(getResources().getColor(R.color.orange), PorterDuff.Mode.SRC_ATOP);
    mGitHub.setColorFilter(getResources().getColor(R.color.greyMaterialDark), PorterDuff.Mode.SRC_ATOP);

    mImageViewGitHub.setOnClickListener(new View.OnClickListener() {
        @Override/*from www  . j  av  a  2 s  .c  o m*/
        public void onClick(View v) {
            Intent mIntent = new Intent();
            mIntent.setAction(Intent.ACTION_VIEW);
            mIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            mIntent.setData(Uri.parse(getString(R.string.url)));
            startActivity(mIntent);
        }
    });

    AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(MainActivity.this, R.style.DialogTheme);

    mAlertDialog.setTitle(getString(R.string.about));
    mAlertDialog.setView(mView);
    mAlertDialog.setPositiveButton(getString(R.string.okay), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    mAlertDialog.show();
}

From source file:com.aapbd.utils.image.CacheImageDownloader.java

private void scaleImage1(ImageView view) {
    // Get the ImageView and its bitmap

    final Drawable drawing = view.getDrawable();
    if (drawing == null) {
        return; // Checking for null & return, as suggested in comments
    }/*from www  .jav  a 2s  . c om*/
    final Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    final int bounding = dpToPx(view.getContext(), 300);
    Log.i("Test", "original width = " + Integer.toString(width));
    Log.i("Test", "original height = " + Integer.toString(height));
    Log.i("Test", "bounding = " + Integer.toString(bounding));

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    final float xScale = ((float) bounding) / width;
    final float yScale = ((float) bounding) / height;
    final float scale = (xScale <= yScale) ? xScale : yScale;
    Log.i("Test", "xScale = " + Float.toString(xScale));
    Log.i("Test", "yScale = " + Float.toString(yScale));
    Log.i("Test", "scale = " + Float.toString(scale));

    // Create a matrix for the scaling and add the scaling data
    final Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    final Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth(); // re-use
    height = scaledBitmap.getHeight(); // re-use
    final BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    Log.i("Test", "scaled width = " + Integer.toString(width));
    Log.i("Test", "scaled height = " + Integer.toString(height));

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);

    Log.i("Test", "done");
}

From source file:com.support.android.designlibdemo.activities.CampaignDetailActivity.java

public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;/*w  w  w  .  j a  v  a  2s  .  c  om*/
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }

    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;

}

From source file:com.he5ed.lib.cloudprovider.picker.CloudPickerActivity.java

/**
 * @hide/* w  w w  .  j av  a  2  s. c om*/
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save empty view state skip if not visible
    LinearLayout emptyView = (LinearLayout) findViewById(R.id.error_view);
    outState.putInt("empty_view_visibility", emptyView.getVisibility());
    if (emptyView.getVisibility() != View.VISIBLE)
        return;

    ImageView icon = (ImageView) findViewById(R.id.empty_icon_image_view);
    TextView title = (TextView) findViewById(R.id.empty_title_text_view);
    TextView detail = (TextView) findViewById(R.id.empty_detail_text_view);

    Bitmap bitmap = ((BitmapDrawable) icon.getDrawable()).getBitmap();
    outState.putParcelable("icon_drawable", bitmap);
    outState.putCharSequence("title_text", title.getText());
    outState.putCharSequence("detail_text", detail.getText());
}

From source file:com.amrutpatil.makeanote.NotesActivity.java

private void edit(View view) {
    Intent intent = new Intent(NotesActivity.this, NoteDetailActivity.class);
    String id = ((TextView) view.findViewById(R.id.id_note_custom_home)).getText().toString();
    intent.putExtra(AppConstant.ID, id);

    //If it is a custom note list
    LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.home_list);
    int isList = linearLayout.getVisibility();
    if (isList == View.VISIBLE) {
        Intent intent1 = new Intent(NotesActivity.this, NoteCustomList.class);
        intent1.putExtra(AppConstant.LIST, AppConstant.TRUE);
    }/*  w w  w  .  ja  v a 2 s  .  c om*/

    ImageView tempImageView = (ImageView) view.findViewById(R.id.image_note_custom_home);
    if (tempImageView.getDrawable() != null) {
        mSendingImage = ((BitmapDrawable) tempImageView.getDrawable()).getBitmap();
    }
    startActivity(intent);
}

From source file:arun.com.popularmovies.fragments.MovieGridFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.movies_fragment, container, false);
    mMoviesGrid = (RecyclerView) rootView.findViewById(R.id.movies_grid_recyclerview);
    mMoviesGrid.setLayoutManager(new StaggeredGridLayoutManager(2, 1));

    MovieFetcherTask fetcherTask = new MovieFetcherTask(
            getArguments().getString(ARG_SORT_TYPE, MovieFetcherTask.SORT_TYPE_POPULAR));

    fetcherTask.setProgressListener(new MovieFetcherTask.ProgressListener() {
        @Override//from w ww .j a  v  a 2s .  c  o  m
        public void onFetchingStart() {
        }

        @Override
        public void onFetchingComplete(MovieResults movieResults) {
            if (movieResults != null && getActivity() != null) {
                Log.d(TAG, "Fetching complete, rendering grid");
                mAdapter = new GridAdapter(getActivity().getApplicationContext(), movieResults.getResults());

                mAdapter.setOnItemClickListener(new GridAdapter.ItemClickListener() {
                    @Override
                    public void onMovieClicked(int position, Movie movie, ImageView movieImage) {
                        // create the detail activity intent
                        Intent movieDetailActivity = new Intent(getActivity(), MovieDetailActivity.class);
                        // add movie information
                        movieDetailActivity.putExtra("movie_info", movie);

                        // store the poster image for transition
                        mPosterCache.clear();
                        mPosterCache.put(1, movieImage.getDrawable());

                        getActivity().startActivity(movieDetailActivity, ActivityOptionsCompat
                                .makeSceneTransitionAnimation(getActivity(), movieImage, "robot").toBundle());
                    }
                });
                mMoviesGrid.setAdapter(mAdapter);
            } else {
                Toast.makeText(getActivity(), getActivity().getString(R.string.error), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }).execute();
    return rootView;
}

From source file:com.ayuget.redface.ui.activity.BaseDrawerActivity.java

private void styleNavDrawerItem(View view, DrawerItem drawerItem, boolean selected) {
    if (drawerItem.isSeparator()) {
        // Nothing to style !
        return;/*from   w w  w . j  a va2s.  c o m*/
    }

    ImageView itemIcon = (ImageView) view.findViewById(R.id.item_icon);

    if (itemIcon != null) {
        UiUtils.setDrawableColor(itemIcon.getDrawable(), UiUtils.getReplyToolbarIconsColor(this));
    }

    // todo
}

From source file:com.spit.matrix15.DetailActivity.java

@SuppressWarnings("ConstantConditions")
@Override/*from   w w  w  . ja  va2s.c  o  m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initActivityTransitions();
    setContentView(R.layout.activity_detail);

    ViewCompat.setTransitionName(findViewById(R.id.app_bar_layout), EXTRA_IMAGE);
    supportPostponeEnterTransition();

    setSupportActionBar((Toolbar) findViewById(R.id.detail_toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final String itemTitle = getIntent().getStringExtra(EXTRA_TITLE);
    final Event event = Event.find(Event.class, "event_name = ?", itemTitle).get(0);

    FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (event.isFavorite == null) {
                event.isFavorite = "false";
            }
            if (!event.isFavorite.equals("true")) {
                event.isFavorite = "true";
                event.save();
                Snackbar.make(findViewById(android.R.id.content), itemTitle + " added to favorites",
                        Snackbar.LENGTH_SHORT).show();
            } else {
                event.isFavorite = "false";
                event.save();
                Snackbar.make(findViewById(android.R.id.content), itemTitle + " removed from favorites",
                        Snackbar.LENGTH_SHORT).show();
            }
        }
    });

    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbarLayout.setTitle(itemTitle);
    collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));

    descriptionText = (TextView) findViewById(R.id.description);
    descriptionText.setText(event.eventDescription);

    priceText = (TextView) findViewById(R.id.txt_reg_fee);
    if (Integer.parseInt(event.fee) != 0)
        priceText.setText("\u20B9 " + event.fee);
    else
        priceText.setText("FREE");

    venueText = (TextView) findViewById(R.id.txt_venue);
    if (event.venue != null)
        venueText.setText(event.venue);

    highlightText1 = (TextView) findViewById(R.id.txt_hglt1);
    if (event.eventHighlight1 != null)
        highlightText1.setText(getString(R.string.bullet) + " " + event.eventHighlight1);

    highlightText2 = (TextView) findViewById(R.id.txt_hglt2);
    if (event.eventHighlight2 != null) {
        highlightText2.setText(getString(R.string.bullet) + " " + event.eventHighlight2);
        highlightText2.setVisibility(View.VISIBLE);
    }

    highlightText3 = (TextView) findViewById(R.id.txt_hglt3);
    if (event.eventHighlight3 != null) {
        highlightText3.setText(getString(R.string.bullet) + " " + event.eventHighlight3);
        highlightText3.setVisibility(View.VISIBLE);
    }

    contactText = (TextView) findViewById(R.id.contacts);
    contactText.setText("Contact: " + event.contact1 + ", " + event.contact2);

    emailText = (TextView) findViewById(R.id.email);
    emailText.setText("Email: " + event.email);

    final ImageView image = (ImageView) findViewById(R.id.image);
    Picasso.with(this).load(getIntent().getStringExtra(EXTRA_IMAGE)).placeholder(R.drawable.snackbar_background)
            .fit().into(image, new Callback() {
                @Override
                public void onSuccess() {
                    Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                        public void onGenerated(Palette palette) {
                            applyPalette(palette);
                        }
                    });
                }

                @Override
                public void onError() {
                    Log.d("P", "Couldn't load " + getIntent().getStringExtra(EXTRA_IMAGE));
                    Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
                    image.setImageBitmap(bitmap);
                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                        public void onGenerated(Palette palette) {
                            applyPalette(palette);
                        }
                    });
                }
            });

    TextView title = (TextView) findViewById(R.id.title);
    title.setText(itemTitle);
}

From source file:com.taobao.weex.ui.component.WXImage.java

@WXComponentProp(name = Constants.Name.SRC)
public void setSrc(String src) {
    if (src == null) {
        return;/*  www  .j a  v a 2  s .  c  om*/
    }

    ImageView image = getHostView();
    if ("".equals(src) && image != null) {
        image.setImageDrawable(null);
        return;
    }

    if (image != null) {
        if (image.getDrawable() != null) {
            image.setImageDrawable(null);
        }
    }

    this.mSrc = src;
    WXSDKInstance instance = getInstance();
    Uri rewrited = instance.rewriteUri(Uri.parse(src), URIAdapter.IMAGE);

    if (Constants.Scheme.LOCAL.equals(rewrited.getScheme())) {
        setLocalSrc(rewrited);
    } else {
        int blur = 0;
        if (getDomObject() != null) {
            String blurStr = getDomObject().getStyles().getBlur();
            blur = parseBlurRadius(blurStr);
        }
        setRemoteSrc(rewrited, blur);
    }
}

From source file:com.intel.xdk.base.Base.java

public void showSplashScreen() {
    cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            //treat displays larger than 6" as tablets
            DisplayMetrics dm = new DisplayMetrics();
            cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
            double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
            double screenInches = Math.sqrt(x + y);
            if (screenInches > 6) {
                isTablet = true;/* ww w.  j  av  a  2s . c o  m*/
            }

            //used for calculating status bar height
            int deviceWidth = dm.widthPixels;
            int deviceHeight = dm.heightPixels;
            if (deviceWidth > deviceHeight) {
                deviceWidth = dm.heightPixels;
                deviceHeight = dm.widthPixels;
            }

            //get the splash screen image from asssets
            Bitmap bm = null;
            try {
                if (isTablet) {
                    bm = getBitmapFromAsset("www/intel.xdk.base/android/splash_screen_tablet.jpg");
                } else {
                    bm = getBitmapFromAsset("www/intel.xdk.base/android/splash_screen.jpg");
                }
            } catch (IOException ioe) {
            }

            //if the splash screen assets are missing, don't try to show the splash screen
            if (bm == null) {
                return;
            }

            if (Debug.isDebuggerConnected())
                Log.i("[intel.xdk]", "splash");

            ActivityInfo ai = null;
            int splashViewId = 0;
            try {
                ai = cordova.getActivity().getPackageManager().getActivityInfo(
                        cordova.getActivity().getComponentName(),
                        PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);

                if (isTablet) {
                    if (ai.screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                        splashViewId = cordova.getActivity().getResources().getIdentifier("splash_tablet_ls",
                                "layout", cordova.getActivity().getPackageName());
                    } else {
                        splashViewId = cordova.getActivity().getResources().getIdentifier("splash_tablet",
                                "layout", cordova.getActivity().getPackageName());
                    }
                } else {
                    if (ai.screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                        splashViewId = cordova.getActivity().getResources().getIdentifier("splash_ls", "layout",
                                cordova.getActivity().getPackageName());
                    } else {
                        splashViewId = cordova.getActivity().getResources().getIdentifier("splash", "layout",
                                cordova.getActivity().getPackageName());
                    }
                }
                LayoutInflater inflater = LayoutInflater.from(cordova.getActivity());
                splashView = inflater.inflate(splashViewId, null);

                //set the splash screen image
                //http://stackoverflow.com/questions/7776445/in-android-can-i-use-image-from-assets-in-layout-xml
                ImageView backgroundImage = (ImageView) splashView
                        .findViewById(cordova.getActivity().getResources().getIdentifier("background", "id",
                                cordova.getActivity().getPackageName()));
                backgroundImage.setImageBitmap(bm);
                ((ViewGroup) root.getParent()).addView(splashView);
                splashView.bringToFront();

                //hack to fix splash screen size when it is smaller than screen            
                ImageView splashImage = (ImageView) cordova.getActivity()
                        .findViewById(cordova.getActivity().getResources().getIdentifier("background", "id",
                                cordova.getActivity().getPackageName()));
                LayoutParams params = splashImage.getLayoutParams();
                Rect imgBounds = splashImage.getDrawable().getBounds();
                int splashHeight = params.height;
                int splashWidth = params.width;

                //make copies in case we have to switch for landscape - not sure if needed, this is a last minute hack
                int deviceWidthCopy, deviceHeightCopy;
                deviceWidthCopy = deviceWidth;
                deviceHeightCopy = deviceHeight;
                if (ai.screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    int temp = deviceWidthCopy;
                    deviceWidthCopy = deviceHeightCopy;
                    deviceHeightCopy = temp;
                }
                if (splashHeight < deviceHeightCopy || splashWidth < deviceWidthCopy) {
                    float scaleH = (float) deviceHeightCopy / splashHeight;
                    float scaleW = (float) deviceWidthCopy / splashWidth;
                    float scale = Math.max(scaleH, scaleW);
                    params.height *= scale;
                    params.width *= scale;
                    splashImage.setLayoutParams(params);
                }

                cordova.getActivity().setProgressBarIndeterminateVisibility(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}