开源工程笔记

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大小值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值