gpt4 book ai didi

android - NetworkImageView 类转换异常

转载 作者:行者123 更新时间:2023-11-30 02:13:44 27 4
gpt4 key购买 nike

CircularNetworkImageView thumbNail = ((CircularNetworkImageView) convertView.findViewById(R.id.UserImage));
thumbNail.setImageUrl(m.getThumbnailUrl(),imageLoader);

自定义网络ImageView类

public class CircularNetworkImageView extends NetworkImageView {
Context mContext;

public CircularNetworkImageView(Context context) {
super(context);
mContext = context;
}

public CircularNetworkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}

public CircularNetworkImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mContext = context;
}

@Override
public void setImageBitmap(Bitmap bm) {
if(bm==null) return;
setImageDrawable(new BitmapDrawable(mContext.getResources(),
getCircularBitmap(bm)));
}

/**
* Creates a circular bitmap and uses whichever dimension is smaller to determine the width
* <br/>Also constrains the circle to the leftmost part of the image
*
* @param bitmap
* @return bitmap
*/
public Bitmap getCircularBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int width = bitmap.getWidth();
if(bitmap.getWidth()>bitmap.getHeight())
width = bitmap.getHeight();
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, width);
final RectF rectF = new RectF(rect);
final float roundPx = width / 2;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

错误

04-19 18:08:35.199: E/AndroidRuntime(2432): java.lang.ClassCastException: com.android.volley.toolbox.NetworkImageView 无法转换为 model.CircularNetworkImageView

最佳答案

那是因为您从 findViewById() 获取的对象是 NetworkImageView,而不是 CircularNetworkImageView。您必须创建自己的 xml CircularNetworkImageView 元素,或使其成为 NetworkImageView 的包装器并使用它。

关于android - NetworkImageView 类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com