Ninepatch scaling wrong when scrolling after applying a Colorfilter?

依然范特西╮ 提交于 2019-12-06 03:56:29

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.

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