BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable); Bitmap bitmap = bitmapDrawable .getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
/** * Get the compressed image with specific quality from URL */ public Drawable fetchCompressedDrawable(Resources res, String urlString) { try { InputStream is = fetch(urlString); Bitmap original = new BitmapDrawable(res, is).getBitmap(); ByteArrayOutputStream out = new ByteArrayOutputStream(); original.compress(Bitmap.CompressFormat.JPEG, qualityImage, out); original.recycle(); is.close(); Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); out.close(); scale = getScale(decoded); BitmapDrawable b = new BitmapDrawable(res, decoded); b.setBounds(0, 0, (int) (b.getIntrinsicWidth() * scale), (int) (b.getIntrinsicHeight() * scale)); return b; } catch (Exception e) { return null; } }
@Override public void draw(Canvas canvas) { if (!animating) { super.draw(canvas); } else { float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION; if (normalized >= 1f) { animating = false; placeholder = null; super.draw(canvas); } else { if (placeholder != null) { placeholder.draw(canvas); } // setAlpha will call invalidateSelf and drive the animation. int partialAlpha = (int) (alpha * normalized); super.setAlpha(partialAlpha); super.draw(canvas); super.setAlpha(alpha); } } if (debugging) { drawDebugIndicator(canvas); } }
private BitmapDrawable createFloatingBitmap(View v) { floatingItemStatingBounds = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); floatingItemBounds = new Rect(floatingItemStatingBounds); Bitmap bitmap = Bitmap.createBitmap(floatingItemStatingBounds.width(), floatingItemStatingBounds.height(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); v.draw(canvas); BitmapDrawable retDrawable = new BitmapDrawable(v.getResources(), bitmap); retDrawable.setBounds(floatingItemBounds); return retDrawable; }
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) { RenderScript rs = RenderScript.create(context); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte); Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options); final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate); final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType()); final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs)); script.setRadius(8f); script.setInput(input); script.forEach(output); output.copyTo(blurTemplate); return new BitmapDrawable(context.getResources(), blurTemplate); }
private void initView(){ contentView= LayoutInflater.from(mContext).inflate(R.layout.layout_time_picker,null); cancelBtn=(Button)contentView.findViewById(R.id.btn_cancel); cancelBtn.setTextColor(colorCancel); cancelBtn.setTextSize(btnTextsize); confirmBtn=(Button)contentView.findViewById(R.id.btn_confirm); confirmBtn.setTextColor(colorConfirm); confirmBtn.setTextSize(btnTextsize); hourLoopView = (LoopView) contentView.findViewById(R.id.picker_hour); minuteLoopView = (LoopView) contentView.findViewById(R.id.picker_minute); meridianLoopView = (LoopView) contentView.findViewById(R.id.picker_meridian); setFocusable(true); setBackgroundDrawable(new BitmapDrawable()); setAnimationStyle(R.style.FadeInPopWin); setContentView(contentView);
Drawable drawable = ((ImageView) view).getDrawable(); if (drawable instanceof BitmapDrawable) { if (view.getHeight() / displayDensity <= ICON_MAX_EDGE_LENGTH && view.getWidth() / displayDensity <= ICON_MAX_EDGE_LENGTH) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); if (bitmap != null) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT); json.put(ICON_BITMAP, encoded);
private static Drawable createCloseCrossIcon(Context context, int width, int height, int color, int inset) { Bitmap canvasBitmap = Bitmap.createBitmap(width + inset * 2, height + inset * 2, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(canvasBitmap); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStrokeWidth(3); paint.setColor(color); paint.setAntiAlias(true); Path path = new Path(); path.setFillType(Path.FillType.EVEN_ODD); path.moveTo(inset, inset); path.lineTo(width + inset, height + inset); path.moveTo(width + inset, inset); path.lineTo(inset, height + inset); path.close(); canvas.drawPath(path, paint); return new BitmapDrawable(context.getResources(), canvasBitmap); }
@Override public Object instantiateItem(ViewGroup container, int position) { View view = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.recommend_page_item, container, false); ImageView imageView = (ImageView)view.findViewById(R.id.image); TextView textView = (TextView)view.findViewById(R.id.image_desc); textView.setText(TextIds[position]); imageView.setImageResource(ResIds[position]); Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); Bitmap newimage = getRoundCornerImage(image,50); ImageView imageView2 = new ImageView(view.getContext()); imageView2.setImageBitmap(newimage); container.addView(view); return view; }
BitmapDrawable bmpDrawable = imgView.getDrawable(); Bitmap bmp = bmpDrawable.getBitmap(); ByteArrayOutputStream out = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormatJPEG, 100, out); int size = out.count; if( out.count > 1024 *1024 * 1024 ) { // Greater than 1 MB }
mBitmap = ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatchDrawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); mBitmap = bitmap; mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(mIndicatorColor); mPaint.setStyle(Style.FILL);
/** * 给 {@link View} 设置高斯模糊背景图片 * * @param context * @param bkg * @param view */ public static void blur(Context context, Bitmap bkg, View view) { long startMs = System.currentTimeMillis(); float radius = 15; float scaleFactor = 8; //放大到整个view的大小 bkg = DrawableProvider.getReSizeBitmap(bkg, view.getMeasuredWidth(), view.getMeasuredHeight()); Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / scaleFactor) , (int) (view.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor); canvas.scale(1 / scaleFactor, 1 / scaleFactor); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(bkg, 0, 0, paint); overlay = FastBlur.doBlur(overlay, (int) radius, true); view.setBackgroundDrawable(new BitmapDrawable(context.getResources(), overlay)); Log.w("test", "cost " + (System.currentTimeMillis() - startMs) + "ms"); }
Bitmap foggyWindowBmp = foggyWindow.getBitmap(); foggyWindowBmp.getWidth(), foggyWindowBmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas tempCanvas = new Canvas(tempBitmap); tempCanvas.drawBitmap(foggyWindowBmp, 0, 0, null); Paint p = new Paint(); p.setShader(gradient); p.setColor(0xFF000000); p.setXfermode(new PorterDuffXfermode(Mode.DST_OUT)); tempCanvas.drawCircle(circleX, circleY, radius, p); canvas.drawBitmap(tempBitmap, 0, 0, null);
View layout = layoutInflater.inflate(R.layout.sort_popup_layout, viewGroup); changeSortPopUp.setBackgroundDrawable(new BitmapDrawable()); Button close = (Button) layout.findViewById(R.id.close); close.setOnClickListener(new OnClickListener() {
Paint badgePaint = new Paint(); Rect canvasBounds = new Rect(); TextPaint badgeTextPaint = new TextPaint(); badgePaint.setFlags(Paint.ANTI_ALIAS_FLAG); badgeTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG); badgeTextPaint.setTextAlign(Paint.Align.CENTER); badgePaint.setColor(brand.defaultTextColor(context)); badgeTextPaint.setColor(brand.defaultFill(context)); Bitmap canvasBitmap = Bitmap.createBitmap(width + rectLength, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(canvasBitmap); canvas.getClipBounds(canvasBounds); rect.bottom = canvasBounds.height(); canvas.drawCircle(firstCircleDx, circleDy, circleRadius, badgePaint); canvas.drawRect(rect, badgePaint); canvas.drawCircle(secondCircleDx, circleDy, circleRadius, badgePaint); badgeTextPaint); return new BitmapDrawable(context.getResources(), canvasBitmap);
/** * @param x 图像的宽度 * @param y 图像的高度 * @param image 源图片 * @param outerRadiusRat 圆角的大小 * @return 圆角图片 */ public static Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) { // 根据源文件新建一个darwable对象 Drawable imageDrawable = new BitmapDrawable(image); // 新建一个新的输出图片 Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); // 新建一个矩形 RectF outerRect = new RectF(0, 0, x, y); // 产生一个红色的圆角矩形 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint); // 将源图片绘制到这个圆角矩形上 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, x, y); canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); return output; }
private void captureValues(@NonNull TransitionValues transitionValues) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { return; } View view = transitionValues.view; if (view.getWidth() <= 0 || view.getHeight() <= 0) { return; } Rect bounds = new Rect(0, 0, view.getWidth(), view.getHeight()); if (mFadeBehavior != FADE_BEHAVIOR_REVEAL) { bounds.offset(view.getLeft(), view.getTop()); } transitionValues.values.put(PROPNAME_BOUNDS, bounds); Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); if (view instanceof TextureView) { bitmap = ((TextureView) view).getBitmap(); } else { Canvas c = new Canvas(bitmap); view.draw(c); } transitionValues.values.put(PROPNAME_BITMAP, bitmap); BitmapDrawable drawable = new BitmapDrawable(view.getResources(), bitmap); // TODO: lrtb will be wrong if the view has transXY set drawable.setBounds(bounds); transitionValues.values.put(PROPNAME_DRAWABLE, drawable); }
int backImageInd = r.nextInt(backImages.size()); Bitmap backImage = backImages.get(backImageInd); int w = backImage.getWidth(); int h = backImage.getHeight(); Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); canvas.drawBitmap(backImage, 0, 0, null); Bitmap scaledBitmap = Bitmap.createScaledBitmap(((BitmapDrawable) defaultBitmap).getBitmap(), (int) (w * factor), (int) (h * factor), false); Bitmap mutableMask = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas maskCanvas = new Canvas(mutableMask); maskCanvas.drawBitmap(maskImage, 0, 0, new Paint()); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); canvas.drawBitmap(scaledBitmap, (w - scaledBitmap.getWidth()) / 2, (h - scaledBitmap.getHeight()) / 2, null); canvas.drawBitmap(mutableMask, 0, 0, paint); return new BitmapDrawable(iconPackres, result);
paint = new Paint(); paint.setAntiAlias(true); paintBorder = new Paint(); setBorderColor(Color.WHITE); paintBorder.setAntiAlias(true); image = bitmapDrawable.getBitmap(); shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); paint.setShader(shader); int circleCenter = viewWidth / 2; canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder); canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);