Code example for LayerDrawable

Methods: setBounds, getIntrinsicHeight, getIntrinsicWidth, setId

0
        int h = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, w, h);
    } 
 
    private static LayerDrawable createLayerDrawable(Drawable... layers) {
        return new LayerDrawable(layers);
    } 
 
    static { 
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI("www.youtube.com", "v/*", EMBED_YOUTUBE);
    } 
 
    /** 
     * Cache of recently used images. 
     */ 
    private static final Map<String, SoftReference<Bitmap>> sImageCache = new HashMap<String, SoftReference<Bitmap>>();
 
    /** 
     * Keeps track of pending tasks. 
     */ 
    private final Set<ImageTask> mTasks = new HashSet<ImageTask>();
 
    /** 
     * The total number of tasks created for the last call to 
     * {@link #setHtml(String)}. 
     */ 
    private int mTotalTaskCount;
 
    /** 
     * The number of tasks completed since the last call to 
     * {@link #setHtml(String)}. 
     */ 
    private int mCompleteTaskCount;
 
    private HtmlChromeClient mHtmlChromeClient = new HtmlChromeClient();
 
    /** 
     * The x-position of the last touch event. 
     */ 
    private int mLastX;
 
    /** 
     * The y-position of the last touch event. 
     */ 
    private int mLastY;
 
    private String mHtml;
 
    private Drawable mDrawableVideoBackground;
 
    private Drawable mDrawableVideoPlay;
 
    private Drawable mDrawableYouTubeLogo;
 
    private Drawable mDrawableMissingEmbed;
 
    private Drawable mDrawableMissingImage;
 
    public HtmlView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    } 
 
    public HtmlView(Context context, AttributeSet attrs) {
        super(context, attrs);
    } 
 
    public HtmlView(Context context) {
        super(context);
    } 
 
    { 
        Context context = getContext();
        String packgeName = context.getPackageName();
        Resources resources = getResources();
        int backgroundResId = resources.getIdentifier("background", "drawable", packgeName);
        int playResId = resources.getIdentifier("play_center", "drawable", packgeName);
        int logoResId = resources.getIdentifier("logo", "drawable", packgeName);
        int missingEmbedResId = resources.getIdentifier("missing_embed", "drawable", packgeName);
        int missingImageResId = resources.getIdentifier("missing_image", "drawable", packgeName);
        if (backgroundResId == 0) {
            throw new RuntimeException("R.drawable.background is missing");
        } 
        if (playResId == 0) {
            throw new RuntimeException("R.drawable.play_center is missing");
        } 
        if (logoResId == 0) {
            throw new RuntimeException("R.drawable.logo is missing");
        } 
        if (missingEmbedResId == 0) {
            throw new RuntimeException("R.drawable.missing_embed is missing");
        } 
        if (missingImageResId == 0) {
            throw new RuntimeException("R.drawable.missing_image is missing");
        } 
        mDrawableVideoBackground = resources.getDrawable(backgroundResId);
        mDrawableVideoPlay = resources.getDrawable(playResId);
        mDrawableYouTubeLogo = resources.getDrawable(logoResId);
        mDrawableMissingEmbed = resources.getDrawable(missingEmbedResId);
        mDrawableMissingImage = resources.getDrawable(missingImageResId);
        setBoundsToIntrinsicSize(mDrawableMissingEmbed);
        setBoundsToIntrinsicSize(mDrawableMissingImage);
    } 
 
    /** 
     * Creates a new {@link LayerDrawable} to represent an embedded video. 
     * <p> 
     * The layer {@link android.R.id#background} can be replaced with a 
     * thumbnail of the video using 
     * {@link LayerDrawable#setDrawableByLayerId(int, Drawable)}. 
     */ 
    private LayerDrawable createVideoDrawable(Drawable logo) {
        // Note: It is important that the LayerDrawable is not inflated from a 
        // resource because Drawable#mutate() does not make it safe to swap 
        // layers. 
        LayerDrawable drawable = (logo != null) ? createLayerDrawable(mDrawableVideoBackground,
                mDrawableVideoPlay, logo) : createLayerDrawable(mDrawableVideoBackground,
                mDrawableVideoPlay);
 
        int backgroundIndex = 0;
        drawable.setId(backgroundIndex, android.R.id.background);
 
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, w, h);
 
        return drawable;
    } 
 
    @Override