AndroidSamples-Unsplash (Google I/O 2016)
https://github.com/googlesamples/android-unsplash
1. ForegroundImageView.java (extends ImageView)
自定义View,继承自ImageView
private Drawable foreground;
.....
public void setForeground(Drawable drawable) {
if (foreground != drawable) {
if (foreground != null) {
foreground.setBounds(0, 0, getWidth(), getHeight());
setWillNotDraw(false);
foreground.setCallback(null);
foreground.setCallback(this);
if (foreground.isStateful()) {
foreground.setState(getDrawableState());
}
} else {
setWillNotDraw(true);
}
invalidate();
}
}
...
不明白`foreground.setCallback(null);foreground.setCallback(null);
网上解释如下:
setImageDrawable(drawable);
d.setCallback(null);
Documentation for setCallback (Drawable.Callback cb) states:
Bind a Drawable.Callback object to this Drawable. Required for clients that want to support animated drawables.
在试图实现小内存高速缓存可绘中,我了解到,为了避免关闭的活动我需要解除那些可绘制后内存泄漏:设置自己的回调为null
// Drawable 的静态内部类
public static interface Drawable.Callback
Implement this interface if you want to create an animated drawable that extends Drawable. Upon retrieving a drawable, use Drawable.setCallback(android.graphics.drawable.Drawable.Callback) to supply your implementation of the interface to the drawable; it uses this interface to schedule and execute animation changes.
hasOverlappingRendering:
/***
* 自定义 View 时重写 hasOverlappingRendering 方法指定 View 是否有 Overlapping 的情况,提高渲染性能。
*/
@Override
public boolean hasOverlappingRendering() {
return false;
}
onSizeChanged(),当你的view第一次被赋予一个大小时,或者你的view大小被更改时会被执行。在onSizeChanged方法里面计算位置,间距等其他与你的view大小值。