2015-07-10 58 views
1

我有2個佈局文件,一個用於listview,另一個用於自定義listview中的行佈局。然後我有ListView的Activity類,一個帶有Utils的類,它模糊了視圖背景和自定義ListAdapter類。我試圖模糊與Utils.java中的listviewActivity內的方法模糊()該行的一個TextView的名字,但我得到以下異常:模糊textview的背景 - IllegalArgumentException的寬度和高度必須> 0

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


    } 

這是getView自定義ListAdapter類:

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

} 
+0

發佈你的空缺代碼 –

+0

@jitendraparmar完成。對不起,我認爲如果我沒有包含所有內容,看起來可能會更容易。 – Vas

回答

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

謝謝您的回覆。現在錯誤被刪除,它會讓我運行代碼,但textview不受blur()方法的影響。 – Vas

相關問題