Altering the opacity of part of a View - android

I would like to get the following effect. I have an image on my screen. When a navigation drawer (or another obstructing view) comes over the top of my image, I want to instead of having it cover the image, show the negative of the image.
The pictures below may help to show what I mean.
Any ideas on how to accomplish this? I'd like to get the same effect for the menu icon.

The way I would go about this is:
Enable the drawing cache on the view you want to apply the negative:
view.setDrawingCacheEnabled(true);
Get a snapshot of the view when the drawer is clicked or dragged with :
Bitmap bmp = view.getDrawingCache();
Finally override the onDraw() method of the drawer and draw the bitmap on it. Apply a shader to do the "negative" effect. See: http://chiuki.github.io/android-shaders-filters/#/

canvas.drawColor(Color.WHITE);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.your_drawable);
Bitmap bm = bd.getBitmap();
Paint paint = new Paint();
Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).
paint.setAlpha(0);
canvas.drawBitmap(bm, 0, 0, paint);

Related

Android change bitmap transparency on canvas

I want to be able to slowly make a bitmap image more and more transparent on my canvas.
Currently I am drawing the .png file (stored in drawables) like this:
//I setup the Bitmap in my constructor.
heartSymb = BitmapFactory.decodeResource(getResources(),R.drawable.heartsymbol);
//This is in on draw.
canvas.drawBitmap(heartSymb,0,0,null);
How would I be able to slowly change the bitmap's transparency until it becomes fully transparent?
You can use a Paint object to modify the alpha of the bitmap to be drawn:
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(heartSymb, 0, 0, alphaPaint);
Then you just have to modify alpha value and perform update periodically, maybe by using a Handler.

How to add rainbow gradient to an Image in android

I want to add Transparent Rainbow or Spectrum Gradient to Bitmap.
In photoshop it is very easy. Now I want to do it programmatically. I have gone through a lot of research. But gradient has only one starting color and one ending. How could I add the rainbow color(multiple color) Gradient in Android.
Or is there a way to add that effect to bitmap in Android? Like given
for this you can use a translucent image with the desired gradient and put it on top of your image view in a FrameLayout. then capture bitmap of the view.
once you get your layout ready use this answer to capture bitmap from it
try this to convert a view (framelayout) into a bitmap:
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

SeekBar: getting empty bitmap from Drawable until set it to ImageView OR how to get resulting Bitmap from 9patch

So I want to set a Bitmap created from drawable into a SeekBar's progress. And I make it so:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.seekbar_bg_full);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // I assume here drawable must be drawn but its not
// canvas.drawBitmap(bmp, 0 , 0, null); // does nothing as 4 me
// encode/decode to detach bitmap from 9patch
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0, baos);
final byte[] bytes = baos.toByteArray();
bmp.recycle();
bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
// ClipDrawable is intented to be used as progressDrawable in SeekBar
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
// if not set this drawable to an ImageView then no progress will be shown by SeekBar at all
//ImageView imgFake = (ImageView) findViewById(R.id.fakeImageView);
//imgFake.setImageDrawable(progressDrawable);
mySeekBar.setProgressDrawable(progressDrawable);
width and height are valid values here (like 460 and 30). As you can see there are 2 code lines about ImageView are commented. This ImageView persists on a layout and its visibility is INVISIBLE. If I comment those 2 lines like shown then there will be no visible progress, like drawable is empty or transparent. Looks like this ImageView makes drawable to really draw itself. But I don't like to use a fake ImageView just to make the "magic" happen so the question is - how to make it work without this fake ImageView.
Please don't suggest me ways how to properly set SeekBar progress like:
ClipDrawable progressDrawable = new ClipDrawable(getResources().getDrawable(R.drawable.seekbar_bg_full), Gravity.LEFT, ClipDrawable.HORIZONTAL);
mySeekBar.setProgressDrawable(progressDrawable);
or xml selectors ways or any altrnative ways since I know about it already and my quiestion is not really about it. I just need to make it work my way.
I just need to make my bitmap or canvas or whatever really drawn.
A bit more details if you want (optional to read). The problem is about the drawable seekbar_bg_full - its a 9-patch png. And all need is to obtain a not NinePatchDrawable-linked resulting Bitmap. Assume I have a 460x30px view with 9patch image set as src or background and the 9patch image is stretched just like it should to. So I need to get a Bitmap this view contains and this Bitmap should not be linked somehow to a 9patch. Thats why I encode bitmap to an byte-array and then decode it back - its just to get rid of 9patch. If there is a more esay way to get a resulting bitmap from 9patch (some magic around NinePatchDrawable) - I would like to know about it.
Ok, I figured out how to get rid of fake ImageView and make drawable to draw itself: all I have to do is to call setBounds() method on a drawable:
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressDrawable.setBounds(0, 0, width, height);
mySeekBar.setProgressDrawable(progressDrawable);
Now I don't have to use ImageView, at last!
However my code is a really long story to get rid of 9patch features in drawable.

how to change the color of an image to black and white in android

In my application i want the image which is captured through the camera to appear as a pure black and white image , as i want the captured image image to be printed later..
I tried many codes to convert the into a black and white image but still the image comes as a gray scale image the pixels other than the black ones should become white.
The code that i am using is given as below :
public static Bitmap blackNwhite(Bitmap bitmap)
{
Bitmap bmpMonochrome = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bmpMonochrome;
}
The image that i get as the output is
which is not how i wanted as it is slightly greyish is color..
I want the image to be displayed as follows :
how can i achieve this????? Please help !!!
You can go for following link:
What is the fastest way to convert an image to pure black and white in C#?
Using exact ColorMatrix, best results can be obtained.

Trouble making parts of a bitmap transparent

I'm developing an android app where i need to capture text and save it as a transparent image. Capturing the text has been done but making a transparent png file is where i'm stuck as i'm not familiar with image pixel manipulation at all. Here's what I have so far... i first create a blank bitmap and fill it with a white background, then i set the paint's transparency to 0 (full transparency) and then draw the source bitmap into the destination bitmap using the XOR modes.. but when i run the app all i see is a blank white image. i'll be glad if someone points out what i'm doing wrong and how to fix it. Thanks in advance.
b = Bitmap.createBitmap(tw, th,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Rect dest = new Rect(0,0,b.getWidth(),b.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, b.getWidth(), b.getHeight(), paint);
paint.setAlpha(0);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(bmp,null,dest,paint);
Have you looked at : How to change a bitmap's opacity?
Seems like
paint.setAlpha(0);
won't do anything as you need to set the alpha channel to something greater than 0...
Use:
Color.argb(0,0,0,0)
The first parameter is the alpha. Set it to 0 for complete transparency.

Categories

Resources