Ninepatch缩放错误应用Colorfilter后滚动时?缩放、错误、Ninepatch、Colorfilter

2023-09-06 05:19:51 作者:納痛、依然猶存

今天,我试图让我的ListView多一点活力。所以,我创建了一个白色ninepatch形象,并增加了colorfilter与.setColorFilter方法​​。这不是问题。但是,应用此之后,每次我滚动图像缩放是错误的(随机),所以可以说我的产品100dp高一些文字。滚动项目后仍100dp高,所有的文字显示,但背景图像只使用50dp现在。

today i tried to make my listview a bit more dynamic. So i created a white ninepatch-image and added a colorfilter with the .setColorFilter method. That's not the problem. But after applying this, everytime i scroll the Image is scaling wrong (randomly) so lets say my item is 100dp high with some text. After scrolling the item is still 100dp high and all text is shown but the image in the Background only uses 50dp now.

下面是我的code:

在这里,我怎么设置Colorfilter:

here how i set the Colorfilter:

orgleftbox = context.getResources().getDrawable(R.drawable.list_bubble);
orgleftbox.setColorFilter( 0xff00c0ff, Mode.MULTIPLY );

在这里我如何在我的适配器添加

and here how i add it in my adapter

v = inflater.inflate(R.layout.list_view_item, null);            
TextView t = (TextView)v.findViewById(R.id.text);
t.setBackgroundDrawable(orgrightbox);

我希望有人能够帮助我。因为这个错误是讨厌我;(

I hope someone could help me. Because this bug is annoying me ;(

推荐答案

我能解决我自己的我的问题。所以我回答这个问题的其他人。)

i was able to fix my problem on my own. So i answer this for everybody else ;).

问题是,我在构造函数中加载我绘制一次,因为我想,所以我不会加载它为每个列表项新。但是,Android的系统处理它们的方式相同的内存对象。所以每一个ListItem背景使用相同的MEM-空间(我希望这是没有错的,我想是这样)。如果我开始滚动下一个列表项将被宣布和背景的高度和宽度,改变它的需要,例如下一个项目只有50dp高它保存的值更改为这一个。现在该列表的每一个其它背景将或多或少改变该高度太

The problem was that i loaded my drawable once in the constructor, because i thought so i won't have to load it for each listitem new. But the android-system handles them as the same memory object. so every listitem-background uses the same mem-space (i hope it isn't wrong, i think so). If i start scrolling the next listitem will be declared and changes the height and width of its background to its needs, example the next item is only 50dp high it changes the saved value to this one. Now every other background of the list will change more or less to this height too.

最简单的解决方法是,你必须加载和应用的colorfilter为每个新项目。爱斯佩克。在getView方法。

The simple fix is, that you have to load and apply the colorfilter for each item new. espec. in the getView Method.

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    View v = null;
    leftbox = (NinePatchDrawable) r.getDrawable(R.drawable.bubble_green);
    leftbox.setColorFilter( 0xff00c0ff, Mode.MULTIPLY );

    v = inflater.inflate(R.layout.list_view_item_right, null);
    TextView t = (TextView)v.findViewById(R.id.text);
    t.setBackgroundDrawable(leftbox);

我希望这个答案是有益的。

i hope this answer was useful.

编辑(千次简单):

如果你只想一个colorFilter适用于布局(TextView的...)做这种方式(感谢帕夏):

If you only want to apply a colorFilter to a Layout(Textview...) do it this way (thanks Pasha):

TextView t = (TextView)v.findViewById(R.id.text);
t.getBackground().setColorFilter(0xff00c0ff, Mode.MULTIPLY );