Example usage for android.graphics.drawable BitmapDrawable setDither

List of usage examples for android.graphics.drawable BitmapDrawable setDither

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable setDither.

Prototype

@Override
    public void setDither(boolean dither) 

Source Link

Usage

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//from ww  w. j  ava 2 s .c o  m

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        bitmapDrawable.setAntiAlias(true);
        bitmapDrawable.setDither(true);
        bitmapDrawable.setTargetDensity(Integer.MAX_VALUE);
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:net.sourceforge.servestream.fragment.NowPlayingFragment.java

private void updateNowPlaying() {
    if (mNowPlayingView == null) {
        return;//from   w  ww  . ja v  a 2  s  . c o  m
    }
    try {
        if (true && mService != null && mService.getAudioId() != -1) {
            Drawable d = null;

            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            if (preferences.getBoolean(PreferenceConstants.RETRIEVE_ALBUM_ART, false)) {
                long id = mService.getAudioId();
                if (id >= 0) {
                    Bitmap b = BitmapFactory.decodeResource(getActivity().getResources(),
                            R.drawable.albumart_mp_unknown_list);
                    BitmapDrawable defaultAlbumIcon = new BitmapDrawable(getActivity().getResources(), b);
                    // no filter or dither, it's a lot faster and we can't tell the difference
                    defaultAlbumIcon.setFilterBitmap(false);
                    defaultAlbumIcon.setDither(false);

                    d = MusicUtils.getCachedArtwork(getActivity(), mService.getAudioId(), defaultAlbumIcon,
                            true);
                }
            }

            if (d == null) {
                mCoverart.setVisibility(View.GONE);
            } else {
                mCoverart.setVisibility(View.VISIBLE);
                mCoverart.setImageDrawable(d);
            }

            mTitle.setSelected(true);
            mArtist.setSelected(true);

            CharSequence trackName = mService.getTrackName();
            CharSequence artistName = mService.getArtistName();

            if (trackName == null || trackName.equals(Media.UNKNOWN_STRING)) {
                mTitle.setText(R.string.widget_one_track_info_unavailable);
            } else {
                mTitle.setText(trackName);
            }

            if (artistName == null || artistName.equals(Media.UNKNOWN_STRING)) {
                artistName = mService.getMediaUri();
            }

            mArtist.setText(artistName);

            if (mPreviousButton != null) {
                mPreviousButton.setImageResource(R.drawable.btn_player_prev);
                mPreviousButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            mService.prev();
                        } catch (RemoteException e) {
                        }
                    }
                });
            }

            mPauseButton.setVisibility(View.VISIBLE);

            if (mService.isPlaying()) {
                mPauseButton.setImageResource(R.drawable.btn_playerpreview_pause);
            } else {
                mPauseButton.setImageResource(R.drawable.btn_playerpreview_play);
            }

            mPauseButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    try {
                        if (mService.isPlaying()) {
                            mService.pause();
                        } else {
                            mService.play();
                        }
                    } catch (RemoteException e) {
                    }
                }
            });

            if (mNextButton != null) {
                mNextButton.setImageResource(R.drawable.btn_player_next);
                mNextButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            mService.next();
                        } catch (RemoteException e) {
                        }
                    }
                });
            }

            if (mNowPlayingView.getVisibility() != View.VISIBLE) {
                Animation fade_in = AnimationUtils.loadAnimation(getActivity(), R.anim.player_in);
                mNowPlayingView.startAnimation(fade_in);
            }

            mNowPlayingView.setVisibility(View.VISIBLE);
            mNowPlayingView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Context c = v.getContext();
                    c.startActivity(new Intent(c, MediaPlayerActivity.class));
                }
            });

            return;
        }
    } catch (RemoteException ex) {
    }
    Animation fade_out = AnimationUtils.loadAnimation(getActivity(), R.anim.player_out);
    mNowPlayingView.startAnimation(fade_out);
    mNowPlayingView.setVisibility(View.GONE);
}