扩展视图的 Fresco-CustomView 无法显示图像
Fresco-CustomView that extends view can not display image
我想创建一个扩展视图并具有壁画功能的自定义视图。图片可以下载,但不能显示。请帮助我, 代码如下:
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);
我已经做到了。您必须像在 ImageView 中一样实现 updateDrawable 和 setBounds 方法。因为RootDrawable的getIntrinsicWidth()和getIntrinsicHeight()都是-1.
我想创建一个扩展视图并具有壁画功能的自定义视图。图片可以下载,但不能显示。请帮助我, 代码如下:
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);
我已经做到了。您必须像在 ImageView 中一样实现 updateDrawable 和 setBounds 方法。因为RootDrawable的getIntrinsicWidth()和getIntrinsicHeight()都是-1.