Issue
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.
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?!
Solution
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);
}
//...
Answered By - longi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.