Question about changing alpha value of a specified bitmap region in Android - android

I'm having trouble setting an alpha value of a region path drawn on a bitmap, using SurfaceView.
I'm creating my bitmap and erase with a black color (and 255 as alpha)
bitmap = Bitmap.createBitmap(480, 724, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(0xFF000000);
Canvas canvas = new Canvas(bitmap);
Next, in my onDraw method I draw a path on the bitmap with his own alpha, white color as stroke, black as fill color and 1 as alpha value :
Paint border = new Paint();
border.setStyle(Paint.Style.STROKE);
border.setStrokeWidth(1);
border.setStrokeCap(Paint.Cap.BUTT);
border.setStrokeJoin(Paint.Join.MITER);
border.setColor(Color.WHITE);
Paint inside = new Paint();
inside.setAntiAlias(false);
inside.setStyle(Paint.Style.FILL);
int alpha = 1<<24;
inside.setColor(alpha);
canvas.drawBitmap(bitmap,0,0, null);
canvas.drawPath(path, border);
canvas.drawPath(path, inside);
Finally, in my onTouchEvent method, i'm doing this :
int index = bitmap.getPixel((int)event.getX(), (int)event.getY());
int alpha = index >>> 24;
Log.v("TAG", " Region path alpha :"+ String.valueOf(alpha));
So, when I click inside the path region, I'm expecting to have 1 (0x01) as alpha value,
but instead I'm still have 255 (0xFF). What's wrong with that ? Thanks in advance.

I am trying this in usual Java, and getting no errors ! probably you should try debugging the value of index. Output the value of index and check, or just try
Color.alpha(index)
http://developer.android.com/reference/android/graphics/Color.html#alpha(int)

Related

Is there a way of customizing each Google Maps marker in Android Studio?

I am building a google maps app. I have different objects with coordinates but each object has a unique int value wich i would like to be shown next to a marker.
For example for an object with specific coorinates and value 123, i would like to put on the map(at those coords) the marker and next to it the value 123.
I've been doing some research and the only way I found plausible is to use an Android API to create your own bitmap image from a base image and some string that is "attached" and use that for the marker icon.
Is there a better way of doing that ?
On the same topic, cand you show the title of every marker on your map at the same time ?
https://stackoverflow.com/a/14812104
Please see the link. The snipet is used to add text on the maker which can also be customized.
Yes #kisslory you can fully customize each marker to suit your need.
While setting the bitmap for each marker you can create a new bitmap with from a given resource using the method below.
public static Bitmap drawTextToBitmap(Context gContext,
int gResId,
String gText) {
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap =
BitmapFactory.decodeResource(resources, gResId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(61, 61, 61));
// text size in pixels
paint.setTextSize((int) (14 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;
canvas.drawText(gText, x, y, paint);
return bitmap;
}

Overlap Paths in Canvas Android

I am trying to draw a shape which is made of a number of paths:-
multiple Grey line path like this one
Path path1 = new Path();
path1.moveTo(2 * w, 2 * h);
path1.lineTo(0, 2 * h - perpendicular);
pathColorList.add(Pair.create(path1, Color.GRAY));
and Blue line path like this one
Path path2 = new Path();
path2.moveTo(0, 2 * h - perpendicular);
path2.lineTo(w * 2, 2 * h - 2 * perpendicular);
pathColorList.add(Pair.create(path2, Color.BLUE));
pathColorList is list of paths and their paint colors.
List<Pair<Path, Integer>> pathColorList = new ArrayList<Pair<Path, Integer>>();
The Problem is I need Blue path always on top of Grey path. Even if Grey path overlap Blue path; Overlap region must be Blue instead of Grey so it seems like Blue band is still on Top of grey band.
What I have tried:-
One way to do is draw all grey path then draw all blue path . But shape I am trying to plot have multiple grey and blue overlaps which need to plot in an order for path animation so I cant do like this .
I tried using PorterDuffXfermodeon Grey paths but none of the PorterDuffXfermode seems to work
for (Pair pathColor
: pathColorList) {
if (pathColor.second == Color.GRAY) {
if (shader == null) {
shader = new LinearGradient(0, 0, 0, getWidth(),
Color.DKGRAY, Color.LTGRAY, Shader.TileMode.MIRROR);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setColor(pathColor.second);
}
paint.setShader(shader);
} else {
paint.setShader(null);
paint.setColor(pathColor.second);
paint.setXfermode(null);
}
canvas.drawPath(pathColor.first, paint);
}
I am unable to figure out what I am doing wrong. Any help will be appreciated.
Try this...
1.- Put you colors in an array ordered by your color preferences.
public static final int COLORS[]={Color.BLUE,Color.RED,Color.YELLOW, Color.MAGENTA,Color.GREEN,Color.CYAN};
2.- The object Path have a int variable keeping the color Path.
3.- Before paint you have a List containg the Path objects
4.- Before paint order this Path list by color attribute and with the color preferences criteria described in 1
Good luck!!!!

unable to transparent pixels of a bitmap image

I am working with bitmap images whose transparent parts are colored in magenta (in some languages it is possible to set a color as transparent). I try to transparent pixels which are in magenta in the original bitmap image.
I load the bitmap from SD-card:
Bitmap bitmap = BitmapFactory.decodeFile(myImagePath);
copy it to another bitmap to make it mutable:
Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_8888,true);
Then scan it pixel by pixel to find pixels in magenta and try to change their transparency.
for(int x=0;x<bitmap2.getWidth();x++){
for(int y=0;y<bitmap2.getHeight();y++){
if(bitmap2.getPixel(x, y)==Color.rgb(0xff, 0x00, 0xff))
{
int alpha = 0x00;
bitmap2.setPixel(x, y , Color.argb(alpha,0xff,0xff,0xff)); // changing the transparency of pixel(x,y)
}
}
}
But those pixels which I expect to become transparent are converted to black. By changing the alpha, I found that the final color varies from the mentioned color in argb() (without mentioning the alpha) to black. For instance, Color.argb(0xff,0xff,0xff,0xff) gets white, Color.argb(0x80,0xff,0xff,0xff) gets gray and Color.argb(0x00,0xff,0xff,0xff) gets black.
I don't undrestand what's wrong.
Could it be possible that there is no alpha channel and I should first set/define it? if yes, how?
EDIT1:
According to the comment of Der Gol...lum I have modified my code:
Paint mPaint = new Paint();
mPaint.setAlpha(0);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mPaint.setAntiAlias(true);
Bitmap bitmap = BitmapFactory.decodeFile(myBackImagePath).copy(Bitmap.Config.ARGB_8888 , true);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, mPaint);
if(bitmap.getPixel(0, 0)==Color.rgb(0xff, 0x00, 0xff))
{
for(int x=0;x<bitmap.getWidth();x++){
for(int y=0;y<bitmap.getHeight();y++){
if(bitmap.getPixel(x, y)==Color.rgb(0xff, 0x00, 0xff))
{
bitmap.setPixel(x, y,Color.TRANSPARENT);
}
}
}
But the result is more or less the same. Using different PorterDuffModes causes either transparency of the entire bitmap or make the targeted pixels black:
Would anybody have any idea?
I could finally find the problem.
My png images had no alpha channel or maybe their alpha channel were not activated.
what I did to solve this problem is to add:
bitmap.setHasAlpha(true);
and it works how I expected.

Android Color (setColor in Paint) Needs Negative Integer?

I'm simply trying to paint/draw to the Canvas in Android. However, when I set the color using hex values or using the setARGB method, it doesn't work. But when I use Color.x (eg, Color.GREEN) it works. Here's the code:
Bitmap image = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
Paint paintBackground = new Paint();
int green = Color.argb(0, 0, 255, 0); // 65280 (Won't work)
green = 0x0000ff00; // 65280 (Won't work)
paintBackground.setARGB(0, 0, 255, 0);
green = paintBackground.getColor(); // 65280 (Won't work)
green = Color.GREEN; // -16711936 (Works!)
paintBackground.setColor(green);
green = paintBackground.getColor(); // -16711936
paintBackground.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, bitmapWidth, bitmapHeight, paintBackground);
So basically Color.GREEN returns -16711936 - and this WORKS.
However, the hex value is 65280 - and this DOES NOT WORK. That is, it doesn't paint the green rectangle.
I need to use hex values because I need to set the color to 0x00ffff00 here and then later to a different hex value.
Does Android Color (setColor in Paint) Need a Negative Integer?
The problem is that 0x0000ff00 is not green, but fully transparent green. Fully opaque green would be 0xff00ff00 which is, as you have already noticed, -16711936. Similarly, when using setARGB you need to specify 255 for alpha for the color to be fully opaque.
Color holds 4 fields, alpha, red, green and blue. Whenever anything is mostly opaque it is negative. 50.2% transparent green is positive (0x7F00FF00/2,130,771,712) and 49.8% transparent green is negative (0x8000FF00/-2,147,418,368)
You can also call Color.rgb(0, 255, 0). With rgb() alpha is by default 255, fully opaque.

Bitmap Shader with alpha channel. (Alpha is drawing black..?)

I get my bitmap, use it as a shader tile mode.
The PNG is mostly alpha except for the shape outline to draw.
Except it draws the outline, but is surrounded by black, not seethrough (alpha).
pnt.reset();
if(backgroundColor == 1)
{
pnt.setColor(myColor);
pnt.setStyle(Paint.Style.FILL);
}
m_canvas.drawPath(path, pnt);
//fillBMP = getBitmapFromAsset(m_context, "brush.png");
fillBMP = BitmapFactory.decodeFile(mySDPath + "brush.png");
fillBMPshader = new BitmapShader(fillBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
pnt.setShader(fillBMPshader);
m_canvas.drawPath(path, pnt);
Example below of the brush on left. But result it draws on right.
You should set XferMode on your Paint object. More specifically you got to use PorterDuffXferMode MULTIPLY.
Here is a similar question : Android color overlay - PorterDuff modes

Categories

Resources