android - 自定义圆形 imageView 得到错误膨胀

Posted

技术标签:

【中文标题】android - 自定义圆形 imageView 得到错误膨胀【英文标题】:android - custom round imageView get error inflating 【发布时间】:2015-05-26 01:38:33 【问题描述】:

我有一个列表视图,我使用自定义布局来制作行。在每一行中,我都有自定义 imageView 用于舍入图像。这是课程:

public class RoundedImageView extends ImageView 

public RoundedImageView(Context context) 
    super(context);
    // TODO Auto-generated constructor stub


public RoundedImageView(Context context, AttributeSet attrs) 
    super(context, attrs);


public RoundedImageView(Context context, AttributeSet attrs, int defStyle) 
    super(context, attrs, defStyle);


@Override
protected void onDraw(Canvas canvas) 

    Drawable drawable = getDrawable();

    if (drawable == null) 
        return;
    

    if (getWidth() == 0 || getHeight() == 0) 
        return;
    
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0, 0, null);



public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) 
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
            sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;

如果没有在适配器中使用,上面的代码可以正常工作。

这是我的适配器代码:

public class ListAdapter extends BaseAdapter 
    Activity activity;
    public ArrayList<HashMap<String,String>> list;

    public ListAdapter(Activity activity, ArrayList<HashMap<String, String>>list ) 
        super();
        this.activity=(Activity)activity;
        this.list=list;
    

    public int getCount() 
        return contactList.size();
    

    public Object getItem(int position) 
        return contactList.get(position);
    

    public long getItemId(int arg0) 
        return 0;
    

    private class ViewHolder 
        ImageView imguser; 
    

    public View getView(int position, View convertView, ViewGroup parent) 
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();
        if (convertView == null) 
            convertView = inflater.inflate(R.layout.ads, null);
            holder = new ViewHolder();
            holder.imguser=(ImageView)convertView.findViewById(R.id.imguser);
            convertView.setTag(holder);
         else 
            holder = (ViewHolder) convertView.getTag();
        

        HashMap<String, String> item = contactList.get(position);
        imageLoader.displayImage("http://192.168.1.35/test/usersPic/"+item.get("profileImg"), holder.imguser,
                options, imageListener);

        return convertView;
    

这是我得到的错误:

    03-23 05:30:02.853: E/androidRuntime(1533): android.view.InflateException: Binary XML file line #12: Error inflating class com.tejarat.example.RoundedImageView
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at com.tejarat.example.Fragment_C$ListAdapter.getView(Fragment_C.java:168)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.ListView.onMeasure(ListView.java:1159)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:704)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15848)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.os.Handler.handleCallback(Handler.java:730)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.os.Looper.loop(Looper.java:137)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invoke(Method.java:525)
03-23 05:30:02.853: E/AndroidRuntime(1533):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:73

你能帮我解决这个问题吗?

【问题讨论】:

同时发布您的 xml 代码 你在你的 xml 代码中使用 com.tejarat.example.RoundedImageView 作为 ImageView 标签吗?如果是这样,用你的包名替换 'com.tejarat.example'(如果这不是你的包名)。 @KarthikaPB 是的,这是我的包名 【参考方案1】:

刷新您的项目,然后检查您的 xml 中的自定义 roundimageview 是否在您单击它时将您重定向到您的类文件。

【讨论】:

【参考方案2】:

在你的getView()方法改行:

holder.imguser=(ImageView)convertView.findViewById(R.id.imguser);

用这个:

holder.imguser=(RoundedImageView)convertView.findViewById(R.id.imguser);

可能还有其他问题,但这个问题目前是可见的。

【讨论】:

没用,我以前用过它作为imageView,它确实有效。 在你的xml中,你必须用com.tejarat.example.RoundedImageView替换ImageView标签,你还必须在java中使用RoundedImageView。但是很难假设那里出了什么问题。实际上,您需要替换适配器中的所有 ImageView。否则将无法执行 我已经这样做了,我已经在我的 xml 代码中定义了 com.tejarat.example.RoundedImageView。 你是不是把CustomAdapter中的所有imageView都换成了RoundedImageView?【参考方案3】:

请在您的适配器中检查传递参数:

imageLoader.displayImage("http://192.168.1.35/test/usersPic/"+item.get("profileImg"), holder.imguser,
                options, imageListener);

选项??

你有没有初始化选项值。 请检查。

【讨论】:

【参考方案4】:

如果你只使用包含

mOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true)
                .cacheInMemory(true)
                .showImageForEmptyUri(R.drawable.noimage_round)
                .showImageOnFail(R.drawable.noimage_round)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelSize(R.dimen.adapter_main_width)))
                .build();

  mImageLoader.displayImage(searchImage, holder.sThumb, mOptions);

将创建圆形图像

【讨论】:

以上是关于android - 自定义圆形 imageView 得到错误膨胀的主要内容,如果未能解决你的问题,请参考以下文章

Android 丨 Glide加载自定义圆形ImageView

Android学习笔记-绘制圆形ImageView实例

Android 圆角、圆形 ImageView 实现

Android开发圆形ImageView实现

Android自定义圆角矩形ImageView,支持Glide加载图片及颜色填充

Android自定义圆角矩形ImageView,支持Glide加载图片及颜色填充