I need to do something which exactly the opposite of Bitmap.extractAlpha: Apply an alpha map (which loaded from a file) onto a RGB bitmap (which also loaded from a file).
Yes! Just like "CGImageCreateWithMask" in iOS!
The Square blog had a tutorial about this just last week: http://corner.squareup.com/2013/01/transparent-jpegs.html :).
I dont know what exactly CGImageCreateWithMask does, but if you want another picture to serve as the alpha channel for your Bitmap, you can create one as described in this question, which combines four images. I haven't tried it now, but I think for two colors it would look something like this:
Paint colorPaint = new Paint();
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, colorPaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);
// save result somewhere
You can also always just work on the raw pixel data of a bitmap using Bitmap.getPixels
Related
I want to make look like grid background as a full screen, .
...by using android plot library. Can anyone suggest a good implementation of that.
I'm going to change bitmapshader by creating bitmap, to pass
RectF rect = plot.getGraph().getGridRect();
BitmapShader myShader = new BitmapShader(
Bitmap.createScaledBitmap(
BitmapFactory.decodeResource(
getResources(),
R.drawable.graph_background),
1,
(int) rect.height(),
false),
Shader.TileMode.REPEAT,
Shader.TileMode.REPEAT);
Matrix m = new Matrix();
m.setTranslate(rect.left, rect.top);
myShader.setLocalMatrix(m);
plot.getGraph().getGridBackgroundPaint().setShader(myShader);
this give error for (int) rect.height(), null pointer exception for this
Without a more precise description of what you're trying to do the best I can do is to provide a general answer to how you accomplish your goal. Here's how you would display a grid of 25x25 px boxes:
plot.setDomainStep(StepMode.INCREMENT_BY_PIXELS, 25);
plot.setRangeStep(StepMode.INCREMENT_BY_PIXELS, 25);
plot.getGraph().getDomainGridLinePaint().setColor(Color.BLUE);
plot.getGraph().getRangeGridLinePaint().setColor(Color.BLUE);
You said in your comments that you wanted to make the boxes as small as possible, so if you really want to do that, you could specify a size of 1 by 1 instead.
I'm creating a app in android for mugs like this
I know how to do everything except wrapping an image around an object. It would be a simple task to do if I would only have to stack an image on top of the other, if they were flat, but if it is a round object, as this mug is, it's kinda tricky.
I thought of creating a obj object file in blender and render the image on it in OpenGL or Unity. The obj file size goes to 5MB. And i cannot use this much big file in my app.
Is there any alternate good approach for it? If there please share a reference or example for it
Use in this manner
the work around of it by doing a masking over the image
ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null);
mImageView.setImageBitmap(result);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.drawable.background_frame);
As mentioned here Masking(crop) image in frame
I suppose that this is the right way, but using pure OpenGL is difficult. I used LibGDX for simple 3d modeling.
I have a bitmap object.
I have a Region object that represents a small portion of the bitmap;
I want to remove drawing from the bitmap object of that particular region and make that portion transparent..
How to do it? any help....
I am using android api-level 8..
You can simply make a pixel transparent by using mBitmap.setPixel (100,100,Color.TRANSPARENT);, so basic idea is to iterate over all the pixel to make it transparent, but if you have to iterate over too many pixels, it might be slow.
OR
You can use PorterDuffXferMode to make a portion transparent,
For an example create a paint object as mentioned below and pass it to the canvas:
Paint mPaint = new Paint();
mPaint.setXferMode(new PorterDuffXferMode(PorterDuff.Mode.CLEAR));
You can pass it to the canvas as described below:
Canvas c = new Canvas(mBitmap);
c.drawCircle(cx, cy, radius, paint);
It is for the circle but hope you will get the hint to do it for the custom region as per your need.
If still it is not working then you might have to disable Hardware Acceleration for that particular View. For more information, refer this Google DOC.
Hope this will give you some hint.
let's say i have this bitmap, which is a random shape painted all black, and say i want to be bale to change it color, does my bitmap have to be all painted white first or is there something else to it?
If you're using Canvas the way to alter the bitmap's color is to alter the bitmap itself. The steps involved are as follows:
Say you want to load an existing Bitmap you have somewhere and you want to tint it red somehow.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
After that you want to modify the bitmap's pixels before you paint it onto the Canvas. You create an int array that will holds all your pixels.
int[] pixels;
bitmap.getPixels(pixels, 0, 0, 0, 0, width, height);
After that you need to modify the array (say, adding to the red component). However, right now all we have is int values inside a pixel array. R,G and B are all packed inside. How to retrieve them?
int red = Color.red(pixels[n]);
int green = Color.green(pixels[n]);
int blue= Color.blue(pixels[n]);
Then you modify the pixel's value by whatever you want, you could put it in a loop or however you like, and then put it back to the pixels array. Also, RGB values go from 0-255 because they are 8-bit values.
Right after that you would put them back using exactly the opposite function.
bitmap.setPixels(pixels, 0, 0, 0, 0, width, height);
And then you're ready to go calling Canvas.drawBitmap();
Keep in mind that this process ought to be slow if you do it frequently, besides Canvas is a slow way of doing thing's if you're interested in real-time apps such as games.
Hope it helped!
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.