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

  • SEARCH
  • Home
  • 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 9222353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:15+00:00 2026-06-18T03:49:15+00:00

Referring to my 1st question here ( Android: Change center of linear GradientDrawable via

  • 0

Referring to my 1st question here ( Android: Change center of linear GradientDrawable via code ), i m still struggling with trying to change the gradient center of the background.

basically, i want to change the center of a linear gradient in the background, depending on the system volume. so if the user changes the volume, the gradient is either (#1)redraw or (#2) changes position:

To #1: There s a workaround for Linear Gradient with a Shader Factory

ShapeDrawable.ShaderFactory sf=new ShapeDrawable.ShaderFactory() {
   @Override
    public Shader resize(int width, int height) {
    return new LinearGradient(0, 0, width, height,
        new int[]{Color.WHITE, Color.GRAY, Color.BLACK},
        new float[]{0,0.5f,1}, Shader.TileMode.MIRROR);
    }
};

But the problem with this solution is, that there are several different gradient modes AND the background would be changed and redrawn EVERYTIME the volume changes…Since it’s a streaming application I m not sure about the performance in this case

So I came to solution #2. I draw every linear gradient once if it is selected and draw it for example in a view which is bigger than the screen of the device.

enter image description here

Now, if the user chages the volume, the screen just is moving on its Y-axis. BUT, I tried it with linear Gradient and also with the Shader Factory, but everytime the Background-View-Gradient is as big as the screen device and not bigger.

 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();
 display.getSize(size);
 int width = size.x;
 int height = size.y + 500; //change height to higher than parentview

 //get a background and change layout (tried with view,relativelayout as well)
 LinearLayout backgroundGradientView = (LinearLayout)
            findViewById(R.id.player_backgroundGradient);  
 backgroundGradientView.getLayoutParams().width = width;
 backgroundGradientView.getLayoutParams().height = height;

 GradientDrawable gd = new GradientDrawable(
             GradientDrawable.Orientation.TOP_BOTTOM, new int[] { startColor, endColor });
 backgroundGradientView.setBackground(gd);

 //later, i m changing the Y Value, but the background is as big as the parent
 backgroundGradientView.setY(currentY + 100) 

anyone has any ideas how to change this? i really don t know how to solve this case?!

  • 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-06-18T03:49:16+00:00Added an answer on June 18, 2026 at 3:49 am

    OK, got it working. I finally used the Shader Factory solution. Don’t know if the performance of redrawing the gradient every time is the best, but at least it s working.. So here is changing background gradient depending on android device volume:

    //global variables, because they can change during runtime
    private int startColor;
    private int endColor;
    private float gradientCenter;
    
    
    // call this, after every volume change, gradientCenter is a value between 0.0-0.5f, depending on the current volume 
    //...
    AudioManager audMgr = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    gradientCenter = 0.5f - (float) currentVolume / (float) maxVolume
            * 0.5f; 
    drawBackground();
    //...
    
    
    
    private void drawBackground() {
    PaintDrawable p = new PaintDrawable();
    p.setShape(new RectShape());
    p.setShaderFactory(sf);
    (YOUR_VIEW_FOR_BACKGROUND).setBackgroundDrawable(p);
    }
    
    //ShaderFactory is using the global gradientCenter,startColor and endColor
    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        return new LinearGradient(0, 0, 0, height, new int[] { startColor,
                endColor }, new float[] { gradientCenter, 1.0f },
                Shader.TileMode.CLAMP);
    }
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

referring to the code example of mine android Remote image Getting problem? how can
Referring to question Spring Autowiring stopped working on GAE I ask: why doesn't AppEngine
Referring to the other question I asked Django using AJAX with Forms, Views ,
Referring to this question , how can i get the current page size in
referring to this question , I've decided to duplicate the tables every year, creating
Referring to the first answer about python's bound and unbound methods here, I have
Referring to the answer here: How to use TinyMCE functions on text without actually
Referring to TLB and maintenance issues ... My question to people (often) using the
Referring to this buddy question , I want to know how one can manage
Referring to this question, let's say we have the following scenario - A model

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.