Setting Background with setBackgroungDrawable function in Android - android

Okey I have a problem with setting a picture for the background. At first I just set the background from 'properities' box. I copy the .jpg file to my drawable folder and just click on the background properity from properities box then select the file from the resources folder. That was okey and it worked but then i wanted to make the background blurry and I found a class that is wrote for that, called BlurBuilder and I put that class to my project. Everything is okey to this point, I added the class but when I tried to apply the funciton from that class as shown in an example like that:
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
editor says 'cannot resolve symbol 'view' '.
I tryied to change the second line as;
View.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
But this time android studio says 'Non-static method 'setBackgroudDrawable(android.graphic.drawables.Drawable) cannot be referenced from a static context
Here's the BlurBuilder class I used:
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}}
Thanks in advance.
EDİT: I want to make my activity's background a blurry image.

You call a non-static function i.e setDrawableBackground from a non-static context which means to say that you need to create an instance of some View for example:
ImageView view = (ImageView) findViewById(R.id.imageview1);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
Once done now you can call any function that view supports.

Got it,
Add the following in XML (main_activity.xml or whatever is your file):
<ImageView
android:id="#+id/whole_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
Add the following in there [in Main Activity] : this will force scale the image to display screen size and use the whole display to show the image.
/* where you set the activity */
setContentView(R.layout.your_main_activity);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap blurredBitmap = BlurBuilder.blur(MainActivity.this,myBackgroundFile);
Bitmap bmp = Bitmap.createScaledBitmap(blurredBitmap,size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.whole_background);
iv_background.setImageBitmap(bmp);

Related

How can I add a blur effect to a background in an app?

How can I add a blur effect to a background in an Android app? I would like to know some source with the resources and the steps to add this effect. I understand that I have to add a filter, but I don't know where I am going to add it Please can someone help me? I will put the layout of the section that I want to change, As you can see, it has a normal background, but I want to put the blur effect on that section
<?xml version="1.0" encoding="utf-8" ?>
Here are some libraries for you:
https://github.com/Manabu-GT/EtsyBlur
https://github.com/Manabu-GT/GlassView
Usually, we call this feature Glass View, so you can also google it.
You can try to use RenderScript to blur the Bitmap of image:
public Bitmap blurBitmap(Bitmap bitmap,Context context){
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(context);
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
}
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
public Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return bitmap;
}
public void blurImage(View imageView, Context context){
Drawable drawable = imageView.getBackground();
Bitmap bitmap = drawableToBitmap(drawable);
Bitmap out = blurBitmap(bitmap,context);
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(),
out);
imageView.setBackground(bitmapDrawable);
}
Use it with a view id is view in a Activity:
View view = findViewById(R.id.view);
blurImage(view,this);

Blur effect on background

I need to show something like this in an ImageView
Do you know a method to set an image in an ImageView like so?
Image in background is bluerd and image on it is with blur effect
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageBitmap(bitmap);
I need put in bitmap my picture and fuzzy picture in background.
this only sets one picture and I need put some fuzzy background
I know how put only one image in 100% quality.
But I don't know how set the background image to this picture.
I don't know how this "filter" name and I could not Google for that.
From https://stackoverflow.com/a/36193733:
I recently came across Renderscript API.
//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;
public Bitmap blur(Bitmap image) {
if (null == image) return null;
Bitmap outputBitmap = Bitmap.createBitmap(image);
final RenderScript renderScript = RenderScript.create(this);
Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
//Intrinsic Gausian blur filter
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
Use the above code snippet in the image view as shown below.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);
imageView.setImageBitmap(blurredBitmap);
Dont forget to add below lines in build.gradle file
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
You can use View Pager and Add ImageView in each page, that way you can switch between images,
Set your fuzzy/blurry image as the background, using
android:background = "#drawable/myFuzzyImage"
Set your clear image as you would normally, just like you have, using the setImageBitmap, or using an ImageView in the xml file of your activity layout.

How to display bitmap resource vs. using shape drawing commands?

How can I change my Bitmap from drawCircle into an imported image in drawable?
I have this code:
canvas.drawCircle((currentX * totalCellWidth)+(cellWidth/2),
(currentY * totalCellHeight)+(cellWidth/2),
(cellWidth*0.45f),ball);
...which produces this output:
I tried this change to substitute a bitmap resource for the circle:
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(b, (currentX * totalCellWidth)+(cellWidth/2),
(currentY * totalCellHeight)+(cellWidth/2),
ball);
...but it doesn't seem to work.
If you are trying to change the size of the bitmap.
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// assign value for the new target size
int newsizeX = width;
int newsizeY = height;
resizedbitmap = Bitmap.createScaledBitmap(resizedbitmap, width, height, true);
canvas.drawBitmap(resizedbitmap, *location in X*, *location in Y*, paint);
it should be working perfectly :)
Is your ic-launcher in your drawable folder? Because my ic-launcher bitmaps are in my drawable-hdpi/ldpi/mdpi/etc folders. If it is, try changing R.drawable.ic_launcher to R.drawable-hdpi/ldpi/mdpi/etc.ic_launcher

Android water mark

I have some question about water mark within android code!
Following code showed my idea about WaterMark!
However,It does not work normally.
e.g. only the image end with .png can be watered mark
Is there a scheme about water mark(.jpeg, .jpg, .wbmp, .bmp, .png or others)
protected static Bitmap getDrmPicture(Context context,String path){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originMap = BitmapFactory.decodeFile (path,options);
Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.close);
InputStream input;
byte[] b;
Bitmap waterMark = null;
try {
input = context.getResources().openRawResource(R.drawable.lock);
b = new byte[input.available()];
input.read(b);
waterMark = DecodeUtils.requestDecode(jc, b, null);
}catch(IOException e){
}
int w = originMap.getWidth();
int h = originMap.getHeight();
int ww = waterMark.getWidth();
int wh = waterMark.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888;);
Canvas cv = new Canvas(newb);
cv.drawBitmap(originMap, 0, 0, null);
cv.drawBitmap(waterMark, w - ww, h - wh, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}
Thanks !
This is the code I use to apply watermark to a jpeg, it should work for you too,
public Bitmap applyWatermarkColorFilter(Drawable drawable) {
Bitmap image = ((BitmapDrawable)drawable).getBitmap();
Bitmap result = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(image, 0, 0, null);
Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark);
canvas.drawBitmap(watermark, image.getWidth()/2 - watermark.getWidth()/2,
image.getHeight()/2 - watermark.getHeight()/2,
null);
return result;
}
Basically after this u have to use Bitmap.compress(<arguments>) to get a jpg out of it.
Din't try for the other formats. May be it might be possible if you can extract the Bitmap out of them like how we do for jpg and png.
https://stackoverflow.com/questions/6756975/draw-multi-line-text-to-canvas
Measure height of multiline text
To center text vertically we need to know text height. Instantiate StaticLayout with text width according to your needs, for us it is simple the width of Bitmap/Canvas minus 16dp padding. The getHeight() then returns height of text.
Positioning text on Canvas
There are four simple steps to position text on Canvas:
Save the current matrix and clip with Canvas.save().
Apply translation to Canvas matrix with Canvas.translate(x,y).
Draw StaticLayout on Canvas with StaticLayout.draw(canvas).
Revert matrix translation with Canvas.restore() method.

How to clip and fill a canvas using an alpha mask

I have some .png icons that are alpha masks. I need to render them as an drawable image using the Android SDK.
On the iPhone, I use the following to get this result, converting the "image" alpha mask to the 'imageMasked' image using black as a fill:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, thumbWidth,
thumbHeight, 8, 4*thumbWidth, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect frame = CGRectMake(0,0,thumbWidth,thumbHeight);
CGContextClipToMask(context, frame, [image CGImage]);
CGContextFillRect(context, frame);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
How do I accomplish the above in Android SDK?
I've started to write the following:
Drawable image = myPngImage;
final int width = image.getMinimumWidth();
final int height = image.getMinimumHeight();
Bitmap imageMasked = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(iconMasked);
image.draw(canvas); ???
I'm not finding how to do the clipping on imageMasked using image.
Solved:
Drawable icon = An_Icon_That_Is_An_Alpha_Mask;
int width = icon.getIntrinsicWidth();
int height = icon.getIntrinsicHeight();
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bm);
icon.setBounds(new Rect(0,0,width,height));
icon.draw(canvas);

Categories

Resources