有没有办法强制 android 视图重绘它的背景?
我设置了一个以渐变为背景的可绘制形状,效果很好,但在更改视图的高度后,您可以看到那些丑陋的渐变步骤。
我能做些什么?
我尝试了 view.invalidate() 和 view.refreshDrawableState() ,没有任何明显的差异。
谢谢你!
//编辑:
以下是更多详细信息和我的代码:
在您回答之后,我认为条带是由于我的代码可能很糟糕造成的。这就是我正在做的事情:
- 缩放视图
- 设置其新的宽度和高度,因为缩放后的视图不接受“旧”区域之外的其他区域的用户输入(请参阅android 缩放后控制的奇怪(至少对我来说)行为/不整个区域不接受输入)
- 尝试再次设置背景(其中包含渐变和圆角矩形)
这是我的代码:
public void onAnimationEnd(Animation animation)
{
MyView view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.getLayoutParams().width = toWidth;
view.getLayoutParams().height = toHeight;
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // background will be changed
}
在设备上,您可以看到背景可绘制对象已更改,但有闪烁,在我看来,就像在动画结束之前应用了背景更改。按计划来说应该结束了!还是我错了?
再次感谢您!
Is there a way to force an android view to redraw it's background?
I have set a drawable shape with a gradient as background which works fine but after changing the view's height you can see those ugly gradient steps.
What can I do?
I tried view.invalidate()
and view.refreshDrawableState()
without any visible difference.
Thank you!
//EDIT:
Here are some more details and my code:
After your answers I think the banding is a result of my probably bad code. Here is what I'm doing:
- Scaling a view
- setting its new width and height because the scaled view does not accept user input in other areas then the "old" one (see android weird (at least for me) behaviour of control after scaling / doesn't accept input in it's whole area )
- trying to set the background again (which contains a gradient and a rectangle with rounded corners)
Here is my code:
public void onAnimationEnd(Animation animation)
{
MyView view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.getLayoutParams().width = toWidth;
view.getLayoutParams().height = toHeight;
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // background will be changed
}
On the device you can see that the background drawable is changed but there is a flickering which seems to me like the backround change is applied before the animation is over. Programatically it should be over! Or am I wrong?
Thank you again!
发布评论
评论(1)
调整大小后可以尝试吗:
v.setBackgroundResource(null);
v.setBackgroundResource(...我的drawable...);
Can you try after resize:
v.setBackgroundResource(null);
v.setBackgroundResource(...my drawable...);