模糊textview的背景 - IllegalArgumentException的宽度和高度必须是> 0

时间:2015-07-10 10:33:50

标签: java android image-processing view textview

我有2个布局文件,一个用于列表视图,另一个用于自定义列表视图中的行布局。然后我有listview的Activity类,一个使用Utils的类,它模糊了视图的背景和自定义的ListAdapter类。我试图使用listviewActivity中的Utils.java中的方法blur()来模糊行的TextView名称,但是我得到以下异常:

videoURL

listview.xml

java.lang.IllegalArgumentException: width and height must be > 0

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background2"
    android:id="@+id/listviewactivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"
        android:choiceMode="singleChoice"
        android:divider="#ffc8cabe"
        android:dividerHeight="4px"/>/>
</RelativeLayout>

ListViewActivity.java

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:minHeight="140dp"
    android:maxHeight="140dp"
    android:padding="5dp">


    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="#fff4f4f4"
         />

    <TextView
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="14sp"
        android:textColor="#fff4f4f4"
        />

</LinearLayout>

这是自定义ListAdapter类中的getView:

public class ListViewActivity extends ActionBarActivity {


    private List<ListModel> list = new ArrayList<ListModel>();
    private ListView listView;
    private CustomListAdapter adapter;
    public static Drawable resizedDrawable;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.challenges_view);
        Intent intent = getIntent();

        ListModel listmodel = new ListModel();
        listmodel.setChallengeName("Test text");
        listmodel.setChallengeStart("Start");
        list.add(listmodel);

        resizedDrawable = Utils.getResizedDrawable(this, R.drawable.image, 362, 161);

     LayoutInflater factory = getLayoutInflater();
        View textEntryView = factory.inflate(R.layout.row, null);
        TextView bluredname = (TextView) textEntryView.findViewById(R.id.name);
        Utils.blur(bluredname);

        listView = (ListView) findViewById(R.id.listView);
        adapter = new CustomListAdapter(this, list);
        listView.setAdapter(adapter);
}


    }

}

错误发生在createBitmap()方法的Utils类中:

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<ListModel> items;
public static TextView name;

public CustomListAdapter(Activity activity, List<ListModel> challengeItems) {
    this.activity = activity;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int location) {
    return items.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}


       @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (inflater == null)
                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null)
                convertView = inflater.inflate(R.layout.row, null);

            name = (TextView) convertView.findViewById(R.id.name);
            TextView startdate = (TextView) convertView.findViewById(R.id.start);


            convertView.setBackground(ListViewActivity.resizedDrawable);

            ListModel m = items.get(position);

            name.setText(m.getName());

            startdate.setText(m.getStart());

            return convertView;
        }

1 个答案:

答案 0 :(得分:0)

 bluredname.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                bluredname.getViewTreeObserver().removeOnPreDrawListener(this);
                Utils.blur(bluredname);
                return false;
            }
        });