簡體   English   中英

模糊textview的背景-IllegalArgumentException寬度和高度必須> 0

[英]Blurring the background of a textview - IllegalArgumentException width and height must be > 0

我有2個布局文件,一個用於listview,一個用於自定義listview中的行布局。 然后,我有了listview的Activity類,一個具有Utils的類,該類模糊了視圖的背景和自定義ListAdapter類。 我正在嘗試使用listviewActivity內部的Utils.java中的blur()方法模糊該行的TextView名稱,但出現以下異常:

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

listview.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>

row.xml

<?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>

ListViewActivity.java

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


    }

這是自定義ListAdapter類中的getView:

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

}

並且該錯誤發生在我的Utils類中的方法createBitmap()中:

public class Utils {

    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 7.5f;

    public static Drawable getResizedDrawable(Context context,
                                              int drawableResourceId, int imgWidth, int imgHeight) {

        Drawable drawableResource = ContextCompat.getDrawable(context, drawableResourceId);
        Bitmap bitmap = ((BitmapDrawable) drawableResource).getBitmap();
        Drawable drawableResizedBitmap = new BitmapDrawable(
                context.getResources(), Bitmap.createScaledBitmap(bitmap,
                imgWidth, imgHeight, true));

        return drawableResizedBitmap;
    }


    public static Bitmap blur(View v) {
        return blur(v.getContext(), getScreenshot(v));
    }

    public static Bitmap blur(Context ctx, Bitmap image) {
        int width = Math.round(image.getWidth() * BITMAP_SCALE);
        int height = Math.round(image.getHeight() * BITMAP_SCALE);

        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        RenderScript rs = RenderScript.create(ctx);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }

    private static Bitmap getScreenshot(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM