Code example for LayerDrawable

Methods: getBoundssetBoundsgetIntrinsicHeightgetIntrinsicWidthsetDrawableByLayerIdsetId

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 
    public Parcelable onSaveInstanceState() {
        cancelTasks(); 
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.mHtml = mHtml;
        return ss;
    } 
 
    @Override 
    public void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        String html = ss.mHtml;
        if (html != null) {
            setHtml(html);
        } 
    } 
 
    public void setHtmlChromeClient(HtmlChromeClient client) {
        if (client == null) {
            throw new NullPointerException();
        } 
        mHtmlChromeClient = client;
    } 
 
    private Bitmap getImage(String src) {
        if (src != null) {
            SoftReference<Bitmap> reference = sImageCache.get(src);
            return reference != null ? reference.get() : null;
        } else { 
            return null; 
        } 
    } 
 
    private void handleEmbed(Attributes attributes, Editable output) {
        String src = attributes.getValue("src");
        String type = attributes.getValue("type");
        boolean allowFullScreen = Boolean.parseBoolean(attributes.getValue("allowfullscreen"));
 
        Uri uri = null;
        int match = UriMatcher.NO_MATCH;
        if (src != null) {
            uri = Uri.parse(src);
            match = sUriMatcher.match(uri);
        } 
 
        Intent[] intents = null;
        String snapshotUrl = null;
        Drawable drawable;
        LayerDrawable frame = null;
        if (match == EMBED_YOUTUBE) {
            String videoId = getYouTubeVideoId(uri);
            drawable = frame = createVideoDrawable(mDrawableYouTubeLogo);
            snapshotUrl = getYouTubeSnapshotUrl(videoId);
            intents = new Intent[] {
                    // Try opening with YouTube application 
                    new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoId)),
                    // Fallback to opening website 
                    new Intent(Intent.ACTION_VIEW, Uri.parse("www.youtube.com/watch?v=" + videoId))
            }; 
        } else { 
            if ("application/x-shockwave-flash".equals(type) && allowFullScreen) {
                // The embed looks like a generic Flash video 
                Drawable logo = null;
                drawable = createVideoDrawable(logo);
            } else { 
                // The embed was not recognized 
                drawable = mDrawableMissingEmbed;
            } 
            if (src != null) {
                if (type != null) {
                    intents = new Intent[] {
                            // Try opening with URL and type (use application) 
                            new Intent(Intent.ACTION_VIEW, Uri.parse(src)).setType(type),
                            // Fallback to opening with URL (use browser) 
                            new Intent(Intent.ACTION_VIEW, Uri.parse(src))
                    }; 
                } else { 
                    intents = new Intent[] {
                        // Try opening source URL directly 
                        new Intent(Intent.ACTION_VIEW, Uri.parse(src))
                    }; 
                } 
            } 
        } 
        int start = output.length();
        output.append("\uFFFC");
        int end = output.length();
        output.setSpan(new ImageSpan(drawable, src), start, end,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (intents != null) {
            output.setSpan(new IntentsSpan(intents), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } 
        if (frame != null && snapshotUrl != null) {
            int layerId = android.R.id.background;
            Bitmap snapshotBitmap = getImage(snapshotUrl);
            if (snapshotBitmap != null) {
                Drawable snapshotDrawable = new BitmapDrawable(snapshotBitmap);
                snapshotDrawable.setBounds(frame.getBounds());
                frame.setDrawableByLayerId(layerId, snapshotDrawable);
            } else { 
                ImageTask task = new ImageTask(snapshotUrl, frame, layerId);
                executeImageTask(task);
            } 
        }