Example usage for android.view View draw

List of usage examples for android.view View draw

Introduction

In this page you can find the example usage for android.view View draw.

Prototype

@CallSuper
public void draw(Canvas canvas) 

Source Link

Document

Manually render this view (and all of its children) to the given Canvas.

Usage

From source file:org.telegram.ui.GroupCreateActivity.java

public Emoji.XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    text.setText(Utilities.formatName(user.first_name, user.last_name));

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);/* w w  w.ja va2  s.  c om*/
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    Emoji.XImageSpan span = new Emoji.XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.id, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}

From source file:cc.softwarefactory.lokki.android.fragments.MapViewFragment.java

public Bitmap createDrawableFromView(View view) {

    Log.e(TAG, "createDrawableFromView");
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.buildDrawingCache();// www . j  av a  2s. com
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

From source file:org.openqa.selendroid.server.model.DefaultSelendroidDriver.java

@Override
@SuppressWarnings("deprecation")
public byte[] takeScreenshot() {
    ViewHierarchyAnalyzer viewAnalyzer = ViewHierarchyAnalyzer.getDefaultInstance();

    // TODO ddary review later, but with getRecentDecorView() it seems to work better
    // long drawingTime = 0;
    // View container = null;
    // for (View view : viewAnalyzer.getTopLevelViews()) {
    // if (view != null && view.isShown() && view.hasWindowFocus()
    // && view.getDrawingTime() > drawingTime) {
    // container = view;
    // drawingTime = view.getDrawingTime();
    // }/* w ww  . j ava2s.com*/
    // }
    // final View mainView = container;
    final View mainView = viewAnalyzer.getRecentDecorView();
    if (mainView == null) {
        throw new SelendroidException("No open windows.");
    }
    done = false;
    long end = System.currentTimeMillis() + serverInstrumentation.getAndroidWait().getTimeoutInMillis();
    final byte[][] rawPng = new byte[1][1];
    ServerInstrumentation.getInstance().getCurrentActivity().runOnUiThread(new Runnable() {
        public void run() {
            synchronized (syncObject) {
                Display display = serverInstrumentation.getCurrentActivity().getWindowManager()
                        .getDefaultDisplay();
                Point size = new Point();
                try {
                    display.getSize(size);
                } catch (NoSuchMethodError ignore) { // Older than api level 13
                    size.x = display.getWidth();
                    size.y = display.getHeight();
                }

                // Get root view
                View view = mainView.getRootView();

                // Create the bitmap to use to draw the screenshot
                final Bitmap bitmap = Bitmap.createBitmap(size.x, size.y, Bitmap.Config.ARGB_8888);
                final Canvas canvas = new Canvas(bitmap);

                // Get current theme to know which background to use
                final Activity activity = serverInstrumentation.getCurrentActivity();
                final Theme theme = activity.getTheme();
                final TypedArray ta = theme
                        .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
                final int res = ta.getResourceId(0, 0);
                final Drawable background = activity.getResources().getDrawable(res);

                // Draw background
                background.draw(canvas);

                // Draw views
                view.draw(canvas);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
                    throw new RuntimeException("Error while compressing screenshot image.");
                }
                try {
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    throw new RuntimeException("I/O Error while capturing screenshot: " + e.getMessage());
                } finally {
                    Closeable closeable = (Closeable) stream;
                    try {
                        if (closeable != null) {
                            closeable.close();
                        }
                    } catch (IOException ioe) {
                        // ignore
                    }
                }
                rawPng[0] = stream.toByteArray();
                mainView.destroyDrawingCache();
                done = true;
                syncObject.notify();
            }
        }
    });

    waitForDone(end, serverInstrumentation.getAndroidWait().getTimeoutInMillis(), "Failed to take screenshot.");
    return rawPng[0];
}

From source file:com.dk.view.FolderDrawerLayout.java

public static Bitmap drawViewToBitmap(Bitmap dest, View view, int width, int height, int downSampling,
        Drawable drawable) {/*from  ww  w.  j  a  v  a  2s .  com*/
    float scale = 1f / downSampling;
    int heightCopy = view.getHeight();
    // view.layout(0, 0, width, height);
    int bmpWidth = (int) (width * scale);
    int bmpHeight = (int) (height * scale);
    if (dest == null || dest.getWidth() != bmpWidth || dest.getHeight() != bmpHeight) {
        dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas c = new Canvas(dest);
    drawable.setBounds(new Rect(0, 0, width, height));
    drawable.draw(c);
    if (downSampling > 1) {
        c.scale(scale, scale);
    }
    view.draw(c);
    // view.layout(0, 0, width, heightCopy);
    return dest;
}

From source file:com.yahala.ui.GroupCreateActivity.java

public XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    String name = Utilities.formatName(user.first_name, user.last_name);
    if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
        name = PhoneFormat.getInstance().format("+" + user.phone);
    }/*from   w w  w  .j a va  2 s  . c om*/
    text.setText(name + ", ");

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    XImageSpan span = new XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.jid, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}

From source file:com.bigpigs.fragments.SearchFragment.java

private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {

    View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.custom_marker, null);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.marker_icon);
    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(),
            customMarkerView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);// w w w.  j a  v a 2  s  . c o m
    customMarkerView.draw(canvas);
    return returnedBitmap;
}

From source file:io.selendroid.server.model.DefaultSelendroidDriver.java

@Override
@SuppressWarnings("deprecation")
public byte[] takeScreenshot() {
    ViewHierarchyAnalyzer viewAnalyzer = ViewHierarchyAnalyzer.getDefaultInstance();

    // TODO ddary review later, but with getRecentDecorView() it seems to work better
    // long drawingTime = 0;
    // View container = null;
    // for (View view : viewAnalyzer.getTopLevelViews()) {
    // if (view != null && view.isShown() && view.hasWindowFocus()
    // && view.getDrawingTime() > drawingTime) {
    // container = view;
    // drawingTime = view.getDrawingTime();
    // }//  ww w  . ja v a 2 s.  c o  m
    // }
    // final View mainView = container;
    final View mainView = viewAnalyzer.getRecentDecorView();
    if (mainView == null) {
        throw new SelendroidException("No open windows.");
    }
    done = false;
    long end = System.currentTimeMillis() + serverInstrumentation.getAndroidWait().getTimeoutInMillis();
    final byte[][] rawPng = new byte[1][1];
    ServerInstrumentation.getInstance().getCurrentActivity().runOnUiThread(new Runnable() {
        public void run() {
            synchronized (syncObject) {
                Display display = serverInstrumentation.getCurrentActivity().getWindowManager()
                        .getDefaultDisplay();
                Point size = new Point();
                try {
                    display.getSize(size);
                } catch (NoSuchMethodError ignore) { // Older than api level 13
                    size.x = display.getWidth();
                    size.y = display.getHeight();
                }

                // Get root view
                View view = mainView.getRootView();

                // Create the bitmap to use to draw the screenshot
                final Bitmap bitmap = Bitmap.createBitmap(size.x, size.y, Bitmap.Config.ARGB_8888);
                final Canvas canvas = new Canvas(bitmap);

                // Get current theme to know which background to use
                final Activity activity = serverInstrumentation.getCurrentActivity();
                final Theme theme = activity.getTheme();
                final TypedArray ta = theme
                        .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
                final int res = ta.getResourceId(0, 0);
                final Drawable background = activity.getResources().getDrawable(res);

                // Draw background
                background.draw(canvas);

                // Draw views
                view.draw(canvas);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                if (!bitmap.compress(Bitmap.CompressFormat.PNG, 70, stream)) {
                    throw new RuntimeException("Error while compressing screenshot image.");
                }
                try {
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    throw new RuntimeException("I/O Error while capturing screenshot: " + e.getMessage());
                } finally {
                    Closeable closeable = (Closeable) stream;
                    try {
                        if (closeable != null) {
                            closeable.close();
                        }
                    } catch (IOException ioe) {
                        // ignore
                    }
                }
                rawPng[0] = stream.toByteArray();
                mainView.destroyDrawingCache();
                done = true;
                syncObject.notify();
            }
        }
    });

    waitForDone(end, serverInstrumentation.getAndroidWait().getTimeoutInMillis(), "Failed to take screenshot.");
    return rawPng[0];
}

From source file:com.fvd.nimbus.BrowseActivity.java

public void screenCapture(/*boolean full*/) {
    View v = super.findViewById(R.id.wvWrapper);
    /*if(full)  v = super.findViewById(R.id.wv);
    else v = super.findViewById(R.id.wvWrapper);*/
    if (v != null) {
        Bitmap bm = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
        v.draw(new Canvas(bm));

        if (bm != null) {
            try {
                /*String filename=String.valueOf(System.currentTimeMillis())+"-tmp.png";
                File file = new File(appSettings.getInstance(null).getSavingPath(),filename);              
                file.createNewFile();//from w ww  .  j  a va  2 s . c o  m
                FileOutputStream ostream = new FileOutputStream(file);
                bm.compress(CompressFormat.PNG, 100, ostream);
                ostream.flush();
                ostream.close();*/
                String filename = appSettings.saveTempBitmap(bm);
                if (filename != null && filename.length() > 0) {
                    Intent iPaint = new Intent();
                    /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
                         bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                         byte[] byteArray = stream.toByteArray();*/
                    //iPaint.putExtra("shot", byteArray);
                    Toast.makeText(getApplicationContext(), "Saved as " + filename, Toast.LENGTH_LONG).show();
                    iPaint.putExtra("temp", true);
                    iPaint.putExtra("path", filename);
                    iPaint.putExtra("domain", getDomainName(lastUrl));
                    iPaint.setClassName("com.fvd.nimbus", "com.fvd.nimbus.PaintActivity");
                    startActivity(iPaint);
                    //overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
                    overridePendingTransition(R.anim.carbon_slide_in, R.anim.carbon_slide_out);
                }

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        } else
            Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_LONG).show();

    } else
        Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_LONG).show();
}

From source file:co.ceryle.segmentedbutton.SegmentedButtonGroup.java

/**
 * Crate a bitmap from the given view//from   w  w  w.  j  a va 2  s  .co m
 */
private Bitmap getViewBitmap(View view) {
    // setContainerAttrs();

    //Get the dimensions of the view so we can re-layout the view at its current size
    //and create a bitmap of the same size
    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

    //Cause the view to re-layout
    view.measure(measuredWidth, measuredHeight);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    //Create a bitmap backed Canvas to draw the view into
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
    view.draw(c);
    return b;
}

From source file:com.android.leanlauncher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from  w ww  .  jav a 2  s .c  o m
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);
    }
    destCanvas.restore();
}