Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:42:10+00:00 2026-05-26T18:42:10+00:00

I created a custom object that has a Bitmap field. I’m drawing several of

  • 0

I created a custom object that has a Bitmap field. I’m drawing several of these objects to the canvas of a View to mimic a scrolling horizontal image gallery.

When the user long presses one of the images, I want to change the opacity of the remaining Bitmaps to a specified percentage. This could give the impression that they’ve darkened for “edit mode”, or it could mean they’ve returned to normal. (Please note that I don’t want to permanently alter the Bitmaps. I want to be able to adjust their opacity on the fly.)

I pieced together the following code from various forums, and everything seems to be working except for the change in opacity. I’ve confirmed that my Bitmaps are mutable and have alpha every step of the way. What am I doing wrong?

Targeting Android 2.1, API Level 7

View (abridged for brevity):

public class CanvasStoryEdit2 extends View
{

    public CanvasStoryEdit2(Context context, AttributeSet attrs) {
        super(context, attrs);

        for (int i = 0; i < getResources().getInteger(R.integer.maxAllowedSlides); i++)
        {

            ImageStoryEdit img = new ImageStoryEdit();

            //test images
            if (i == 0) { resource = R.drawable.a1; }
            else if (i == 1) { resource = R.drawable.a2; }
            else if (i == 2) { resource = R.drawable.a3; }
            else if (i == 3) { resource = R.drawable.a4; }
            else if (i == 4) { resource = R.drawable.a5; }

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);

            Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + bmp.getConfig().toString());
            Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(bmp.hasAlpha()));
            Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(bmp.isMutable()));

            final int imgHeight = bmp.getHeight() / (bmp.getWidth() / imgWidth);

            bmp = Bitmap.createScaledBitmap(bmp, imgWidth, imgHeight, false);

            Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + bmp.getConfig().toString());
            Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(bmp.hasAlpha()));
            Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(bmp.isMutable()));

            int width = bmp.getWidth();
            int height = bmp.getHeight();

            int[] pixels = new int[width * height];  

            bmp.getPixels(pixels, 0, width, 0, 0, width, height); 

            bmp.recycle();
            bmp = null;

            img.setBmp(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
            img.getBmp().setPixels(pixels, 0, width, 0, 0, width, height); 

            pixels = null;

            Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + img.getBmp().getConfig().toString());
            Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(img.getBmp().hasAlpha()));
            Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(img.getBmp().isMutable()));

            imageStoryEditList.add(img);

        }

    }

Call made on long press:

{
img.setOpacity(50);
invalidate();
}

ImageStoryEdit (also abridged):

public class ImageStoryEdit
{

    private int opacity;
    public Bitmap bmp;


    public Bitmap getBmp() {
        return bmp;
    }

    public void setBmp(Bitmap bmp)
    {

        this.bmp = bmp;

        UpdateRectF();

    }

    public int getOpacity() 
    {
        return opacity;

    }

    public void setOpacity(int percent) 
    {

        //ADJUST FOR RANGE OF 0-100%
        percent = percent < 0 ? 0 : percent;
        percent = percent > 100 ? 100 : percent;

        this.opacity = percent;

        int opacity = (int) (this.opacity * 2.55);

        Log.d("TEST", "OPACITY = " + Integer.toString(percent) + " : " + Integer.toString(opacity));

        adjustOpacity(opacity);

    }

    private void adjustOpacity(int opacity)
    {

        Log.d("TEST", "OPACITY = " + Integer.toString(opacity));
        Log.d("TEST", this.bmp.getConfig().toString());
        Log.d("TEST", "hasAlpha: " + Boolean.toString(this.bmp.hasAlpha()));
        Log.d("TEST", "isMutable: " + Boolean.toString(this.bmp.isMutable()));

        Bitmap bmp2 = this.bmp.copy(Config.ARGB_8888, true);

        Canvas canvas = new Canvas(bmp2);

        Paint paint = new Paint(); 

        paint.setAlpha(opacity); 

        canvas.drawBitmap(bmp2, 0, 0, paint);

        this.bmp = bmp2;

        Log.d("TEST", this.bmp.getConfig().toString());
        Log.d("TEST", "hasAlpha: " + Boolean.toString(this.bmp.hasAlpha()));
        Log.d("TEST", "isMutable: " + Boolean.toString(this.bmp.isMutable()));
        Log.d("TEST", "DONE");

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:42:11+00:00Added an answer on May 26, 2026 at 6:42 pm

    I think I figured this one out on my own, but I welcome any feedback if anyone can offer additional insight.

    I was able to do what I wanted by using a BitmapDrawable. It’s a wrapper for a Bitmap. Editing the Bitmap itself is, indeed, permanent. BitmapDrawables allow you to change certain parameters without directly affecting the underlying Bitmap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a custom URL object in JavaScript and discovered that Firefox already has
I have created a custom content type that has a ReferenceField. When I set
I created a custom binder that looks like this: public override object BindModel(ControllerContext controllerContext,
I have created an object called Project that has different properties (strings and some
I have a javascript that has custom indexes, I created them like so: var
I'm trying to create a custom object that behaves properly in set operations. I've
I'm trying to create a custom JSP tag that would take an array object
I want to create a custom set that will automatically convert objects into a
I've created a custom object, I have it appearing automatically on the Account details
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.