Ninepatch scaling wrong when scrolling after applying a Colorfilter?

柔情痞子 提交于 2019-12-07 15:57:14

问题


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.

Here's my code:

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 ;(


回答1:


Your interpretation is correct, you cannot use the Drawable on several Views. The Drawable has a size set by its View, so if you attach it to several Views at the same time, it won't work properly unless the Views have exactly the same dimension.




回答2:


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

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.

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.

EDIT (Thousand times simpler):

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 );


来源:https://stackoverflow.com/questions/5884481/ninepatch-scaling-wrong-when-scrolling-after-applying-a-colorfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!