0

so I'm in a bit of a pickle. I know how to set the alpha value of a bitmap in android. What I don't know how to do is make is reversible. So, let's say someone wanted to set the alpha of an image to 50%, so they do. Now lets say they wanted to set it 75% (keep in mind, this is of the original image alpha value). Currently, what I have is a function that will set the alpha value of the current image, so it would be 75% of the 50% alpha value if that makes sense. How can I make it so that it accounts for the original image?

public Pixmap setAlpha(float newAlpha) { //integer between 0-100
    if (newAlpha != alpha) { //to check if the current alpha value of the image is equal to your desired alpha. to avoid always halving you alpha value
        float test = newAlpha/100.0f;
        float test2 = test * 255;
        alpha = test2;
        Bitmap newBM = Bitmap.createBitmap(backupImg.getBitmap().getWidth(),backupImg.getBitmap().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas cc = new Canvas(newBM);
        cc.drawARGB(0,0,0,0);
        Paint newPaint = new Paint();
        newPaint.setAlpha((int)test2);
        cc.drawBitmap(backupImg.getBitmap(), 0, 0, newPaint);
        img.setBitmap(newBM);
        return img;
    } else {
        return img;
    }
}

The Pixmap part is just a custom Bitmap class. backupImg is just a copy of img, created in the constructor of the object this function belongs to.

please keep in mind that this will be a canvas based bitmap. If I recall correctly imageView's aren't drawn on the canvas? So, as a further example. Imagine a sprite drawn to the canvas that you want to alter the alpha of. So you do it using the function I've posted. Now, let's say you want to undo the changes and restore it to the sprite's original alpha, of some other value. Well, you can't because the alpha value of the image has been changed permanently. What I want to do is store reference to the original image with another variable, and refer to that whenever I need to adjust the alpha value of the image. Hopefully that makes sense

  • Possible duplicate of [Android and setting alpha for (image) view alpha](https://stackoverflow.com/questions/4931071/android-and-setting-alpha-for-image-view-alpha) – vinS Dec 14 '17 at 03:48
  • Clearly you would have to either keep track of the original value or read it from the unaltered image, wouldn't you? – Ken White Dec 14 '17 at 03:49
  • which i have, in the variable called backupImg. it's value is only set when the object is constructed and never altered otherwise. Just trying to figure out how to do it otherwise – Andrew MacMillan Dec 14 '17 at 04:27

3 Answers3

0

Why don't you set alpha to the ImageView instead of setting it to a bitmap.

By setting alpha to the ImageView or say any view you can reverse it easily.

Refer to this answer in order to do it.

Ashraf Patel
  • 699
  • 1
  • 7
  • 17
  • would that be just for the image? Keep in mind this is all on canvas, and for a game. So it would need to happen in real time – Andrew MacMillan Dec 14 '17 at 04:09
  • Yes, that can be done on any view which is subclass of View(every view in Android is subclass of View class) class. For the need of doing it in realtime you can do that with Java at runtime. Please refer to that link for that also. – Ashraf Patel Dec 14 '17 at 04:18
  • @AndrewMacMillan Don't try to set alpha to the bitmap try to set it on view. You must be showing that bitmap to some view right, think how you can apply alpha to that view. – Ashraf Patel Dec 14 '17 at 04:56
  • In this case, your view is img. So set alpha on that using `.setAlpha`. [Check docs for reference](https://developer.android.com/reference/android/view/View.html#setAlpha(float)) – Ashraf Patel Dec 14 '17 at 06:07
0

You can do it from xml. Just add the below line in the imageview tag:-

android:alpha="0.5"

You can set alpha between 0 to 1. Above line will set alpha to half that is 0.5.

For bitmap:

Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), 
   originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(newBitmap);

Paint alphaPaint = new Paint();
alphaPaint.setAlpha(75);
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);
Prits Ramani
  • 100
  • 4
  • is this for an object on a canvas? or inside an activity. If you take note of the function I am in fact doing this via caanvas – Andrew MacMillan Dec 14 '17 at 04:10
  • sorry, i can't get what you are saying can you clarify more. I think you want to set alpha to an image right? – Prits Ramani Dec 14 '17 at 04:22
  • correct, the alpha of the bitmap. So if I have a character sprite that is drawn on the canvas, I want to be able to redraw it with the updated alpha values – Andrew MacMillan Dec 14 '17 at 04:24
  • isn't that what I have in my original post? @Prits Ramani Also, keep in mind that setAlpha sor a paint is a value from 0-255 inclusive – Andrew MacMillan Dec 14 '17 at 04:34
  • yes, I know the code works. But if you go back and try to adjust the same bitmap's alpha value to some other number, it'll be more transparent, or as transparent as the altered value. Never more. So you wouldn't be able to set the alpha value to 85% after that. it would instead be 85% of the 75% – Andrew MacMillan Dec 14 '17 at 04:43
0

I've solved the problem myself. So, there is nothing wrong with the function I wrote. The problem instead lies within how Java uses pointers. because everything is passed via reference, I was actually referencing the same object, rather than creating two separate objects. So instead of:

Bitmap oldBM = new Bitmap();
Bitmap newBM = oldBM;

you would instead want to do

Bitmap oldBM = new Bitmap();
Bitmap newBM = new Bitmap(using old bitmap's value);