List of usage examples for android.graphics.drawable Drawable draw
public abstract void draw(@NonNull Canvas canvas);
From source file:com.pablog178.pdfcreator.android.PdfcreatorModule.java
private void generateiTextPDFfunction(HashMap args) { if (args.containsKey("fileName")) { Object fileName = args.get("fileName"); if (fileName instanceof String) { this.fileName = (String) fileName; Log.i(PROXY_NAME, "fileName: " + this.fileName); }//from www . java2 s . c om } else return; if (args.containsKey("view")) { Object viewObject = args.get("view"); if (viewObject instanceof TiViewProxy) { TiViewProxy viewProxy = (TiViewProxy) viewObject; this.view = viewProxy.getOrCreateView(); if (this.view == null) { Log.e(PROXY_NAME, "NO VIEW was created!!"); return; } Log.i(PROXY_NAME, "view: " + this.view.toString()); } } else return; if (args.containsKey("quality")) { this.quality = TiConvert.toInt(args.get("quality")); } if (args.containsKey("pageSize")) { Object pageSize = args.get("pageSize"); if (pageSize instanceof String) { if (pageSize.equals("letter")) { this.pageSize = PageSize.LETTER; } else if (pageSize.equals("A4")) { this.pageSize = PageSize.A4; } else { this.pageSize = PageSize.LETTER; } } } TiBaseFile file = TiFileFactory.createTitaniumFile(this.fileName, true); Log.i(PROXY_NAME, "file full path: " + file.nativePath()); try { Resources appResources = app.getResources(); OutputStream outputStream = file.getOutputStream(); final int MARGIN = 0; final float PDF_WIDTH = this.pageSize.getWidth() - MARGIN * 2; // A4: 595 //Letter: 612 final float PDF_HEIGHT = this.pageSize.getHeight() - MARGIN * 2; // A4: 842 //Letter: 792 final int DEFAULT_VIEW_WIDTH = 980; final int DEFAULT_VIEW_HEIGHT = 1384; int viewWidth = DEFAULT_VIEW_WIDTH; int viewHeight = DEFAULT_VIEW_HEIGHT; Document pdfDocument = new Document(this.pageSize, MARGIN, MARGIN, MARGIN, MARGIN); PdfWriter docWriter = PdfWriter.getInstance(pdfDocument, outputStream); Log.i(PROXY_NAME, "PDF_WIDTH: " + PDF_WIDTH); Log.i(PROXY_NAME, "PDF_HEIGHT: " + PDF_HEIGHT); WebView view = (WebView) this.view.getNativeView(); if (TiApplication.isUIThread()) { viewWidth = view.capturePicture().getWidth(); viewHeight = view.capturePicture().getHeight(); if (viewWidth <= 0) { viewWidth = DEFAULT_VIEW_WIDTH; } if (viewHeight <= 0) { viewHeight = DEFAULT_VIEW_HEIGHT; } } else { Log.e(PROXY_NAME, "NO UI THREAD"); viewWidth = DEFAULT_VIEW_WIDTH; viewHeight = DEFAULT_VIEW_HEIGHT; } view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Log.i(PROXY_NAME, "viewWidth: " + viewWidth); Log.i(PROXY_NAME, "viewHeight: " + viewHeight); float scaleFactorWidth = 1 / ((float) viewWidth / PDF_WIDTH); float scaleFactorHeight = 1 / ((float) viewHeight / PDF_HEIGHT); Log.i(PROXY_NAME, "scaleFactorWidth: " + scaleFactorWidth); Log.i(PROXY_NAME, "scaleFactorHeight: " + scaleFactorHeight); Bitmap viewBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888); Canvas viewCanvas = new Canvas(viewBitmap); // Paint paintAntialias = new Paint(); // paintAntialias.setAntiAlias(true); // paintAntialias.setFilterBitmap(true); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(viewCanvas); } else { viewCanvas.drawColor(Color.WHITE); } view.draw(viewCanvas); TiBaseFile pdfImg = createTempFile(); // ByteArrayOutputStream stream = new ByteArrayOutputStream(32); // viewBitmap.compress(Bitmap.CompressFormat.JPEG, this.quality, stream); viewBitmap.compress(Bitmap.CompressFormat.JPEG, this.quality, pdfImg.getOutputStream()); FileInputStream pdfImgInputStream = new FileInputStream(pdfImg.getNativeFile()); byte[] pdfImgBytes = IOUtils.toByteArray(pdfImgInputStream); pdfImgInputStream.close(); // ByteBuffer buffer = ByteBuffer.allocate(viewBitmap.getByteCount()); // viewBitmap.copyPixelsToBuffer(buffer); pdfDocument.open(); float yFactor = viewHeight * scaleFactorWidth; int pageNumber = 1; do { if (pageNumber > 1) { pdfDocument.newPage(); } pageNumber++; yFactor -= PDF_HEIGHT; Image pageImage = Image.getInstance(pdfImgBytes, true); // Image pageImage = Image.getInstance(buffer.array()); pageImage.scalePercent(scaleFactorWidth * 100); pageImage.setAbsolutePosition(0f, -yFactor); pdfDocument.add(pageImage); Log.i(PROXY_NAME, "yFactor: " + yFactor); } while (yFactor > 0); pdfDocument.close(); sendCompleteEvent(); } catch (Exception exception) { Log.e(PROXY_NAME, "Error: " + exception.toString()); sendErrorEvent(exception.toString()); } }
From source file:com.joanzapata.PDFViewActivity.java
public byte[] getBitmapFromView(View view) { // Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); // Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); // Get the view's background Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) // has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else/* ww w .ja va 2 s .co m*/ // does not have background drawable, then draw white background on // the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); ByteArrayOutputStream stream = new ByteArrayOutputStream(); returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); return byteArray; }
From source file:io.mapsquare.osmcontributor.utils.core.ArpiInitializer.java
/** * Pre-compute the different PoiTypes bitmaps icons for the ArpiGL fragment and view. *//*w w w .j a v a 2 s . c o m*/ public void precomputeArpiBitmaps() { try { if (!ArpiGlInstaller.getInstance(application.getApplicationContext()).isInstalled()) { ArpiGlInstaller.getInstance(application.getApplicationContext()).install(); Map<Long, PoiType> poiTypes = poiManager.loadPoiTypes(); for (Map.Entry<Long, PoiType> entry : poiTypes.entrySet()) { Integer id = bitmapHandler.getIconDrawableId(entry.getValue()); if (id != null && id > 0) { Drawable d = application.getApplicationContext().getResources().getDrawableForDensity(id, DisplayMetrics.DENSITY_XXHIGH); int width = d.getIntrinsicWidth(); int height = d.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); d.setBounds(0, 0, width, height); d.draw(c); File dest = new File(application.getApplicationContext().getFilesDir(), ArpiGlInstaller.INSTALLATION_DIR + "/" + ArpiGlInstaller.TEXTURE_ICONS_SUBDIR + "/" + entry.getValue().getIcon() + ".png"); dest.getParentFile().mkdirs(); if (dest.exists()) { dest.delete(); } dest.createNewFile(); OutputStream stream = new FileOutputStream(dest); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); stream.close(); bitmap.recycle(); } } } } catch (IOException | JSONException e) { Timber.e("Error while initializing ArpiGl library: {}", e.getMessage()); } }
From source file:de.mrapp.android.util.BitmapUtil.java
/** * Creates and returns a bitmap from a specific drawable. * * @param drawable/* www . jav a2s . c om*/ * The drawable, which should be converted, as an instance of the class {@link * Drawable}. The drawable may not be null * @return The bitmap, which has been created, as an instance of the class {@link Bitmap} */ public static Bitmap drawableToBitmap(@NonNull final Drawable drawable) { ensureNotNull(drawable, "The drawable may not be null"); if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } Bitmap bitmap; if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); } 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:com.pax.view.keyboard.CustomKeyboardView.java
@Override public void onDraw(Canvas canvas) { List<Keyboard.Key> keys = getKeyboard().getKeys(); for (Keyboard.Key key : keys) { canvas.save();//w w w .ja v a2 s .c om int offsetY = 0; if (key.y == 0) { offsetY = 1; } int initDrawY = key.y + offsetY; Rect rect = new Rect(key.x, initDrawY, key.x + key.width, key.y + key.height); canvas.clipRect(rect); Drawable drawable = null; if (null != key.codes && key.codes.length != 0) { int primaryCode = key.codes[0]; if (primaryCode < 0) { drawable = mOpKeyBgDrawable; } else { drawable = mKeyBgDrawable; } } if (null != drawable && null == key.icon) { int[] state = key.getCurrentDrawableState(); drawable.setState(state); drawable.setBounds(rect); drawable.draw(canvas); } paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(50); paint.setColor(Color.BLACK); if (key.icon != null) { int[] state = key.getCurrentDrawableState(); key.icon.setState(state); key.icon.setBounds(rect); key.icon.draw(canvas); } if (key.label != null) { canvas.drawText(key.label.toString(), key.x + (key.width / 2), initDrawY + (key.height + paint.getTextSize() - paint.descent()) / 2, paint); } canvas.restore(); } }
From source file:at.flack.MailMainActivity.java
public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) { Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mutableBitmap); drawable.setBounds(0, 0, widthPixels, heightPixels); drawable.draw(canvas); return mutableBitmap; }
From source file:com.hippo.gl.glrenderer.XmlResourceTexture.java
@Override protected Bitmap onGetBitmap() { Drawable drawable = ContextCompat.getDrawable(mContext, mResId); int width = mWidth; int height = mHeight; if (width <= 0) { width = drawable.getIntrinsicWidth(); }// w w w .ja va2s . c om if (height <= 0) { height = drawable.getIntrinsicHeight(); } if (width <= 0) { width = 1; } if (height <= 0) { height = 1; } drawable.setBounds(0, 0, width, height); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.draw(canvas); return bitmap; }
From source file:uk.ac.hutton.ics.buntata.activity.LogEntryActivity.java
private Bitmap bitmapDescriptorFromVector(int vectorResId) { Drawable vectorDrawable = ContextCompat.getDrawable(this, vectorResId); vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight()); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);// ww w.jav a2s.com Canvas canvas = new Canvas(bitmap); vectorDrawable.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(); // }//from w w w .j a va 2s . co 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, 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.android.contacts.common.list.ShortcutIntentBuilder.java
private Bitmap generateQuickContactIcon(Drawable photo) { // Setup the drawing classes Bitmap bitmap = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); // Copy in the photo Rect dst = new Rect(0, 0, mIconSize, mIconSize); photo.setBounds(dst);//from w w w . ja v a 2 s. c o m photo.draw(canvas); // Draw the icon with a rounded border RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(mResources, bitmap); roundedDrawable.setAntiAlias(true); roundedDrawable.setCornerRadius(mIconSize / 2); Bitmap roundedBitmap = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888); canvas.setBitmap(roundedBitmap); roundedDrawable.setBounds(dst); roundedDrawable.draw(canvas); canvas.setBitmap(null); return roundedBitmap; }