Example usage for android.graphics.drawable Drawable setBounds

List of usage examples for android.graphics.drawable Drawable setBounds

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable setBounds.

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

Usage

From source file:com.woxthebox.draglistview.DragItemRecyclerView.java

private void init() {
    mAutoScroller = new AutoScroller(getContext(), this);
    mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

    addItemDecoration(new ItemDecoration() {
        @Override/*from  w  w w  .  j ava  2s  . c  om*/
        public void onDraw(Canvas c, RecyclerView parent, State state) {
            super.onDraw(c, parent, state);
            drawDecoration(c, parent, mDropTargetBackgroundDrawable);
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, State state) {
            super.onDrawOver(c, parent, state);
            drawDecoration(c, parent, mDropTargetForegroundDrawable);
        }

        private void drawDecoration(Canvas c, RecyclerView parent, Drawable drawable) {
            if (mAdapter.getDropTargetId() == NO_ID || drawable == null) {
                return;
            }

            for (int i = 0; i < parent.getChildCount(); i++) {
                View item = parent.getChildAt(i);
                int pos = getChildAdapterPosition(item);
                if (pos != NO_POSITION && mAdapter.getItemId(pos) == mAdapter.getDropTargetId()) {
                    drawable.setBounds(item.getLeft(), item.getTop(), item.getRight(), item.getBottom());
                    drawable.draw(c);
                }
            }
        }
    });
}

From source file:com.android.contacts.DynamicShortcuts.java

private Bitmap getFallbackAvatar(String displayName, String lookupKey) {
    // Use a circular icon if we're not on O or higher.
    final boolean circularIcon = !BuildCompat.isAtLeastO();

    final ContactPhotoManager.DefaultImageRequest request = new ContactPhotoManager.DefaultImageRequest(
            displayName, lookupKey, circularIcon);
    if (BuildCompat.isAtLeastO()) {
        // On O, scale the image down to add the padding needed by AdaptiveIcons.
        request.scale = LetterTileDrawable.getAdaptiveIconScale();
    }//from  w  ww . j a v  a 2  s . c om
    final Drawable avatar = ContactPhotoManager.getDefaultAvatarDrawableForContact(mContext.getResources(),
            true, request);
    final Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888);
    // The avatar won't draw unless it thinks it is visible
    avatar.setVisible(true, true);
    final Canvas canvas = new Canvas(result);
    avatar.setBounds(0, 0, mIconSize, mIconSize);
    avatar.draw(canvas);
    return result;
}

From source file:com.tpos.widget.pagertab.PagerSlidingTabStrip.java

private void addIconTextTab(final int position, int resId, String title) {

    TextView tab = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_tabstrip_view, null);
    // new TextView(getContext());
    tab.setText(title);//from   ww w  .  j  a  v a2s .  co m
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();

    Drawable tDrawable = null;
    try {
        tDrawable = getResources().getDrawable(resId);
    } catch (NotFoundException e) {
        e.printStackTrace();
        tDrawable = null;
    }
    if (tDrawable != null) {
        tDrawable.setBounds(0, 0, (int) (tDrawable.getIntrinsicWidth() * 0.5),
                (int) (tDrawable.getIntrinsicHeight() * 0.5));
        tab.setCompoundDrawables(null, tDrawable, null, null);
    }

    addTab(position, tab);

}

From source file:com.metinkale.prayerapp.vakit.fragments.MainFragment.java

private void export(int csvpdf, LocalDate from, LocalDate to) throws IOException {
    File outputDir = getActivity().getCacheDir();
    if (!outputDir.exists())
        outputDir.mkdirs();/*from   www  . j a  va  2 s .c o m*/
    File outputFile = new File(outputDir, mTimes.getName().replace(" ", "_") + (csvpdf == 0 ? ".csv" : ".pdf"));
    if (outputDir.exists())
        outputFile.delete();
    FileOutputStream outputStream;

    outputStream = new FileOutputStream(outputFile);
    if (csvpdf == 0) {
        outputStream.write("Date;Fajr;Shuruq;Dhuhr;Asr;Maghrib;Ishaa\n".getBytes());

        do {
            outputStream.write((from.toString("yyyy-MM-dd") + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 0) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 1) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 2) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 3) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 4) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 5) + "\n").getBytes());
        } while (!(from = from.plusDays(1)).isAfter(to));
        outputStream.close();

    } else {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            PdfDocument document = new PdfDocument();

            PdfDocument.PageInfo pageInfo = null;
            int pw = 595;
            int ph = 842;
            pageInfo = new PdfDocument.PageInfo.Builder(pw, ph, 1).create();
            PdfDocument.Page page = document.startPage(pageInfo);
            Drawable launcher = Drawable.createFromStream(getActivity().getAssets().open("pdf/launcher.png"),
                    null);
            Drawable qr = Drawable.createFromStream(getActivity().getAssets().open("pdf/qrcode.png"), null);
            Drawable badge = Drawable.createFromStream(
                    getActivity().getAssets().open("pdf/badge_" + Prefs.getLanguage() + ".png"), null);

            launcher.setBounds(30, 30, 30 + 65, 30 + 65);
            qr.setBounds(pw - 30 - 65, 30 + 65 + 5, pw - 30, 30 + 65 + 5 + 65);
            int w = 100;
            int h = w * badge.getIntrinsicHeight() / badge.getIntrinsicWidth();
            badge.setBounds(pw - 30 - w, 30 + (60 / 2 - h / 2), pw - 30, 30 + (60 / 2 - h / 2) + h);

            Canvas canvas = page.getCanvas();

            Paint paint = new Paint();
            paint.setARGB(255, 0, 0, 0);
            paint.setTextSize(10);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("com.metinkale.prayer", pw - 30 - w / 2, 30 + (60 / 2 - h / 2) + h + 10, paint);

            launcher.draw(canvas);
            qr.draw(canvas);
            badge.draw(canvas);

            paint.setARGB(255, 61, 184, 230);
            canvas.drawRect(30, 30 + 60, pw - 30, 30 + 60 + 5, paint);

            if (mTimes.getSource().resId != 0) {
                Drawable source = getResources().getDrawable(mTimes.getSource().resId);

                h = 65;
                w = h * source.getIntrinsicWidth() / source.getIntrinsicHeight();
                source.setBounds(30, 30 + 65 + 5, 30 + w, 30 + 65 + 5 + h);
                source.draw(canvas);
            }

            paint.setARGB(255, 0, 0, 0);
            paint.setTextSize(40);
            paint.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(getText(R.string.appName).toString(), 30 + 65 + 5, 30 + 50, paint);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setFakeBoldText(true);
            canvas.drawText(mTimes.getName(), pw / 2.0f, 30 + 65 + 50, paint);

            paint.setTextSize(12);
            int y = 30 + 65 + 5 + 65 + 30;
            int p = 30;
            int cw = (pw - p - p) / 7;
            canvas.drawText(getString(R.string.date), 30 + (0.5f * cw), y, paint);
            canvas.drawText(Vakit.IMSAK.getString(), 30 + (1.5f * cw), y, paint);
            canvas.drawText(Vakit.GUNES.getString(), 30 + (2.5f * cw), y, paint);
            canvas.drawText(Vakit.OGLE.getString(), 30 + (3.5f * cw), y, paint);
            canvas.drawText(Vakit.IKINDI.getString(), 30 + (4.5f * cw), y, paint);
            canvas.drawText(Vakit.AKSAM.getString(), 30 + (5.5f * cw), y, paint);
            canvas.drawText(Vakit.YATSI.getString(), 30 + (6.5f * cw), y, paint);
            paint.setFakeBoldText(false);
            do {
                y += 20;
                canvas.drawText((from.toString("dd.MM.yyyy")), 30 + (0.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 0)), 30 + (1.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 1)), 30 + (2.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 2)), 30 + (3.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 3)), 30 + (4.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 4)), 30 + (5.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 5)), 30 + (6.5f * cw), y, paint);
            } while (!(from = from.plusDays(1)).isAfter(to));
            document.finishPage(page);

            document.writeTo(outputStream);

            // close the document
            document.close();

        } else {
            Toast.makeText(getActivity(), R.string.versionNotSupported, Toast.LENGTH_LONG).show();
        }
    }

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType(csvpdf == 0 ? "text/csv" : "application/pdf");

    Uri uri = FileProvider.getUriForFile(getActivity(), "com.metinkale.prayer.fileprovider", outputFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.export)));
}

From source file:org.addhen.smssync.presentation.view.ui.fragment.MessageFragment.java

private void drawSwipeListItemBackground(Canvas c, int dX, View itemView, int actionState) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = 1.0f - Math.abs(dX) / (float) itemView.getWidth();
        ViewHelper.setAlpha(itemView, alpha);
        ViewHelper.setTranslationX(itemView, dX);
        Drawable d;
        // Swiping right
        Paint p = new Paint();
        if (dX > 0) {
            d = ContextCompat.getDrawable(getAppContext(), R.drawable.swipe_right_list_item_background);
            d.setBounds(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom());
            c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX, (float) itemView.getBottom(),
                    p);/* w ww.ja v  a  2  s.co m*/
        } else { // Swiping left
            d = ContextCompat.getDrawable(getAppContext(), R.drawable.swipe_left_list_item_background);
            d.setBounds(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
            c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(),
                    (float) itemView.getBottom(), p);
        }
        d.draw(c);
    }
}

From source file:net.yanzm.mth.MaterialTabHost.java

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (getChildCount() == 0) {
        return;// www .j a va  2s.  co  m
    }

    final Drawable d = indicator;

    View tabView = tabWidget.getChildTabViewAt(position);
    if (tabView == null) {
        return;
    }

    View nextTabView = position + 1 < tabWidget.getTabCount() ? tabWidget.getChildTabViewAt(position + 1)
            : null;

    int tabWidth = tabView.getWidth();
    int nextTabWidth = nextTabView == null ? tabWidth : nextTabView.getWidth();

    int indicatorWidth = (int) (nextTabWidth * positionOffset + tabWidth * (1 - positionOffset));
    int indicatorLeft = (int) (getPaddingLeft() + tabView.getLeft() + positionOffset * tabWidth);

    int height = getHeight();
    d.setBounds(indicatorLeft, height - indicatorHeight, indicatorLeft + indicatorWidth, height);
    d.draw(canvas);
}

From source file:org.profeda.dictionary.SearchActivity.java

/**
 * Called when the activity is first created.
 *//*from   w ww  .j  a  va  2s  . c  om*/
@Override
public void onCreate(Bundle savedInstanceState) {
    final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(SearchActivity.this);

    if (pref.getBoolean("show_changelog", true) == true) {
        MessageBox.showChangeLog(this);
    }

    loadDictionaries(pref);

    if (pref.getBoolean("p_invert_colors", false) == true) {
        setTheme(R.style.Light);
    } else {
        setTheme(R.style.Dark);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dict = new Dict(this, pref.getString("dictFile", null));

    updateTitleBar();
    readMRU();

    final EditText searchbox = (EditText) findViewById(R.id.searchbox);
    final Button dictselectbutton = (Button) findViewById(R.id.dictselectbutton);
    final ListView listview = (ListView) findViewById(R.id.contentlist);

    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView l, View v, int position, long id) {
            ListAdapter la = ((ListView) l).getAdapter();
            if (la instanceof GenericStringAdapter) {
                final String data = (String) la.getItem(position);
                if (data.startsWith("> ")) {
                    searchbox.setText(data.substring(2));
                    doSearch(true);
                }
            } else if (la instanceof DictListAdapter) {
                final String[] data = (String[]) la.getItem(position);

                final List<Dict> langs = Dict.searchForLanguage(SearchActivity.this, dict.toLang, null);

                String[] menu = new String[langs.size() + 1];
                menu[0] = getString(R.string.copy_translation_to_clipboard);
                for (int i = 1; i < menu.length; i++)
                    menu[i] = String.format(getString(R.string.search_translation_in_language),
                            langs.get(i - 1).toLang);

                Matcher m = Pattern.compile("<b>(.*)<\\/b>.*").matcher(data[1]);
                if (!m.matches())
                    return;
                final String translation = m.group(1).trim();

                new AlertDialog.Builder(SearchActivity.this).setIcon(android.R.drawable.ic_dialog_info)
                        .setTitle(translation).setItems(menu, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                switch (arg1) {
                                case 0:
                                    ClipboardManager cm = (ClipboardManager) getSystemService(
                                            CLIPBOARD_SERVICE);
                                    cm.setText(translation);
                                    break;
                                default:
                                    dict = langs.get(arg1 - 1);
                                    readMRU();
                                    updateTitleBar();
                                    searchbox.setText(translation);
                                    doSearch(true);
                                }
                            }
                        }).show();
            }
        }
    });

    searchbox.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                    || event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                doSearch(true);
                return true;
            } else {
                return false;
            }
        }
    });

    final Drawable deleteIcon = getResources().getDrawable(R.drawable.textbox_clear);
    //deleteIcon.setBounds(0, 0, deleteIcon.getIntrinsicWidth(), deleteIcon.getIntrinsicHeight());
    deleteIcon.setBounds(0, 0, 51, 30); // wtf??? die Zahlen sind empirisch ermittelt... ab right>=52 springt die hhe des editText

    searchbox.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            searchbox.setCompoundDrawables(null, null, s.toString().equals("") ? null : deleteIcon, null);
            //searchbox.setCompoundDrawablesWithIntrinsicBounds(null, null, s.toString().equals("") ? null : deleteIcon, null);
            searchbox.setCompoundDrawablePadding(0);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (enableLivesearch && s.length() > 1 && dict.filePrefix != null && dictSearchInAction == false) {
                DictionarySearcher ds = new DictionarySearcher();
                ds.targetList = (ListView) findViewById(R.id.contentlist);
                ds.execute(dict.filePrefix, s.toString());
            }
            if (s.length() == 0) {
                displayMRU();
            }
        }
    });

    //searchbox.setCompoundDrawables(null, null, deleteIcon, null);
    searchbox.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (searchbox.getCompoundDrawables()[2] == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            if (event.getX() > searchbox.getWidth() - searchbox.getPaddingRight()
                    - deleteIcon.getIntrinsicWidth()) {
                searchbox.setText("");
                searchbox.setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
    });

    dictselectbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectDictionary();
        }
    });

    ((Button) findViewById(R.id.dictswapbutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            final List<Dict> langs = Dict.searchForLanguage(SearchActivity.this, dict.toLang, dict.fromLang);
            if (langs.size() > 0) {
                dict = langs.get(0);
                readMRU();
                updateTitleBar();
                searchbox.setText("");
            } else {
                MessageBox.alert(SearchActivity.this, getString(R.string.no_reverse_dict_found));
            }
        }
    });

}

From source file:com.tpos.widget.pagertab.PagerSlidingTabStrip.java

/**
 * Tab/* w ww.ja  v  a  2s.  com*/
 */
private void setSelectedTabIcon(int position, int resId) {
    // update steven
    updateTabStyles();
    View v = tabsContainer.getChildAt(position);
    if (v instanceof TextView) {
        Drawable tDrawable = null;
        try {
            tDrawable = getResources().getDrawable(resId);
        } catch (NotFoundException e) {
            e.printStackTrace();
            tDrawable = null;
        }
        if (tDrawable != null) {
            tDrawable.setBounds(0, 0, (int) (tDrawable.getIntrinsicWidth() * 0.5),
                    (int) (tDrawable.getIntrinsicHeight() * 0.5));
            ((TextView) v).setCompoundDrawables(null, tDrawable, null, null);
        }
        ((TextView) v).setTextColor(underlineColor);
    }
}

From source file:cn.longchou.wholesale.activity.VehicleDetailActivity.java

private void setAttention() {
     isAttention = cars.isFollow;/*from   w  ww .j a  v a 2 s.c o m*/

     if (!isAttention) {
         mAttention.setText("");
         Drawable drawable = getResources().getDrawable(R.drawable.car_detail_attention_no);
         drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
         mAttention.setCompoundDrawables(null, drawable, null, null);
     } else {
         mAttention.setText("?");
         Drawable drawable = getResources().getDrawable(R.drawable.car_detail_attention);
         drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
         mAttention.setCompoundDrawables(null, drawable, null, null);
     }

 }

From source file:com.android.launcher3.BubbleTextView.java

@Override
public void draw(Canvas canvas) {
    if (!mCustomShadowsEnabled) {
        super.draw(canvas);
        return;//from   w  w w. j  a  va2  s . co  m
    }

    final Drawable background = mBackground;
    if (background != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();

        if (mBackgroundSizeChanged) {
            background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
            mBackgroundSizeChanged = false;
        }

        if ((scrollX | scrollY) == 0) {
            background.draw(canvas);
        } else {
            canvas.translate(scrollX, scrollY);
            background.draw(canvas);
            canvas.translate(-scrollX, -scrollY);
        }
    }

    // If text is transparent, don't draw any shadow
    if (getCurrentTextColor() == ContextCompat.getColor(getContext(), android.R.color.transparent)) {
        getPaint().clearShadowLayer();
        super.draw(canvas);
        return;
    }

    // We enhance the shadow by drawing the shadow twice
    float density = getResources().getDisplayMetrics().density;
    getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
    super.draw(canvas);
    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), getScrollX() + getWidth(),
            getScrollY() + getHeight(), Region.Op.INTERSECT);
    getPaint().setShadowLayer(density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
    super.draw(canvas);
    canvas.restore();
}