我想创建一个扩展视图并具有壁画功能的自定义视图。图像可以下载,但无法显示。请帮帮我,代码如下:
public class QView<DH extends DraweeHierarchy> extends View {
private DraweeHolder<DH> mDraweeHolder;
private boolean mInitialised = false;
public QView(Context context) {
super(context);
init(context);
}
@SuppressWarnings("unchecked")
private void init(Context context) {
if(!mInitialised){
GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources()).build();
mDraweeHolder = (DraweeHolder<DH>) DraweeHolder.create(hierarchy, context);
mInitialised = true;
}
}
@Override
public void onStartTemporaryDetach() {
super.onStartTemporaryDetach();
mDraweeHolder.onDetach();
}
@Override
public void onFinishTemporaryDetach() {
super.onFinishTemporaryDetach();
mDraweeHolder.onAttach();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mDraweeHolder.onAttach();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mDraweeHolder.onDetach();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDraweeHolder.onTouchEvent(event) || super.onTouchEvent(event);
}
@Override
protected boolean verifyDrawable(Drawable who) {
if (who == mDraweeHolder.getTopLevelDrawable()) {
return true;
}
return true;
}
public void setImageURI(Uri uri) {
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setOldController(mDraweeHolder.getController())
.build();
mDraweeHolder.setController(controller);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable drawable = mDraweeHolder.getHierarchy().getTopLevelDrawable();
//Drawable drawable = getResources().getDrawable(R.drawable.weekend_move_access);
drawable.setBounds(0, 0, 400, 400);
drawable.draw(canvas);
}
}
MainActivity.java:
LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj);
//SimpleDraweeView view = new SimpleDraweeView(this);
QView view = new QView(this);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(400, 400);
view.setLayoutParams(layout);
view.setImageURI(Uri.parse("http://d.hiphotos.baidu.com/image/pic/item/aa64034f78f0f7363465230c0855b319ebc41327.jpg"));
ll.addView(view);
答案 0 :(得分:0)
我做了它。您必须在ImageView中实现updateDrawable和setBounds的方法。因为RootDrawable的getIntrinsicWidth()和getIntrinsicHeight()是-1。