java - 模糊 TextView 的背景 - IllegalArgumentException 宽度和高度必须 > 0

标签 java android image-processing view textview

我有 2 个布局文件,一个用于 ListView ,一个用于自定义 ListView 内的行布局。然后我有 ListView 的 Activity 类、一个带有模糊 View 背景的 Utils 的类和自定义的 ListAdapter 类。我试图使用 listviewActivity 内的 Utils.java 中的模糊()方法来模糊行的 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;
            }
        });

关于java - 模糊 TextView 的背景 - IllegalArgumentException 宽度和高度必须 > 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338612/

相关文章:

java - Java-8 中 Xml 解码错误的任何替代解决方案? : “secure-processing org.xml.sax.SAXNotRecognizedException"

android - ListView 中的动画列表项

android - 替换 Android Platform Source 中的预构建内核

python - 如何在 python 中获取给定像素标签的对象边界框?

java - for循环中的函数不随机化

java - 删除子字符串的自定义 Java 方法 - 删除引号时的奇怪行为

image-processing - 在opencv中检测叶状形状的最佳方法

java - 我想将jpg图像转换为字节数组并将字节数组作为命令发送给java中的阅读器

java - 如何使用 Java 将图像/文件上传到 Firebase 存储?

Android firebase ui 返回语句 getFirebaseRef