canvas = new Canvas(); if (mDensity > 0) { canvas.setDensity(mDensity);
canvas.setDensity(density); }else canvas.setBitmap(bitmap);
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inScaled = false; // Load source grayscale bitmap Bitmap grayscale = BitmapFactory.decodeResource(getResources(), R.drawable.my_grayscale_mask, options); // Place for alpha mask. It's specifically ARGB_8888 not ALPHA_8, // ALPHA_8 for some reason didn't work out for me. Bitmap alpha = Bitmap.createBitmap(grayscale.getWidth(), grayscale.getHeight(), Bitmap.Config.ARGB_8888); float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}; Paint grayToAlpha = new Paint(); grayToAlpha.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix))); Canvas alphaCanvas = new Canvas(alpha); // Make sure nothing gets scaled during drawing alphaCanvas.setDensity(Bitmap.DENSITY_NONE); // Draw grayscale bitmap on to alpha canvas, using color filter that // takes alpha from red channel alphaCanvas.drawBitmap(grayscale, 0, 0, grayToAlpha); // Bitmap alpha now has usable alpha channel!
@Override public void setDensity(int density) { delegate.setDensity(density); }
@Override public void setDensity(int density) { delegate.setDensity(density); }
/** * <p>Specifies the density for this Canvas' backing bitmap. This modifies * the target density of the canvas itself, as well as the density of its * backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}. * * @param density The new target density of the canvas, which is used * to determine the scaling factor when drawing a bitmap into it. Use * {@link Bitmap#DENSITY_NONE} to disable bitmap scaling. * * @see #getDensity() * @see Bitmap#setDensity(int) */ @Override public void setDensity(int density) { dest.setDensity(density); if (debug) { Log.d(TAG, "setDensity(" + density + ")"); } }
@Override public void setDensity(int density) { getWrappedCanvas().setDensity(density); }
@Override protected void onDraw(Canvas canvas) { grayToAlpha.setColorFilter(null); canvas.drawBitmap(grayscale, 0, 0, grayToAlpha); canvas.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix))); // alphaCanvas = new Canvas(alpha); // TODO Auto-generated method stub //super.onDraw(canvas); // Make sure nothing gets scaled during drawing canvas.setDensity(Bitmap.DENSITY_NONE); // Draw grayscale bitmap on to alpha canvas, using color filter that // takes alpha from red channel canvas.drawBitmap(grayscale, 0, 0, grayToAlpha); // Bitmap alpha now has usable alpha channel! }
final BitmapDrawable getSVG_Drawable (int svgResourceID, int width, int height) { // Get a Picture from the SVG in res/raw. final SVG vector = SVGParser.getSVGFromResource(getResources(), svgResourceID); final DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); // Redraw the picture to a new size. final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas cnv = new Canvas(bmp); cnv.setDensity((int) (metrics.xdpi)); cnv.drawPicture(vector.getPicture(), new Rect(0, 0, width, height)); final BitmapDrawable drw = new BitmapDrawable(getResources(), bmp); // Return the drawable. return drw; }
c.setDensity(Bitmap.DENSITY_NONE);