1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.annotation.DrawableRes;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.net.Uri;
23import android.util.AttributeSet;
24
25/**
26 * {@link ViewSwitcher} that switches between two ImageViews when a new
27 * image is set on it. The views added to an ImageSwitcher must all be
28 * {@link ImageView ImageViews}.
29 */
30public class ImageSwitcher extends ViewSwitcher
31{
32    /**
33     * Creates a new empty ImageSwitcher.
34     *
35     * @param context the application's environment
36     */
37    public ImageSwitcher(Context context)
38    {
39        super(context);
40    }
41
42    /**
43     * Creates a new empty ImageSwitcher for the given context and with the
44     * specified set attributes.
45     *
46     * @param context the application environment
47     * @param attrs a collection of attributes
48     */
49    public ImageSwitcher(Context context, AttributeSet attrs) {
50        super(context, attrs);
51    }
52
53    /**
54     * Sets a new image on the ImageSwitcher with the given resource id.
55     * This will set that image resource on the next ImageView in the switcher and will
56     * then switch to that view.
57     *
58     * @param resid a Drawable resource id
59     *
60     * @see ImageView#setImageResource(int)
61     */
62    public void setImageResource(@DrawableRes int resid)
63    {
64        ImageView image = (ImageView)this.getNextView();
65        image.setImageResource(resid);
66        showNext();
67    }
68
69    /**
70     * Sets a new image on the ImageSwitcher with the given Uri.
71     * This will set that image on the next ImageView in the switcher and will
72     * then switch to that view.
73     *
74     * @param uri the Uri of an image
75     *
76     * @see ImageView#setImageURI(Uri)
77     */
78    public void setImageURI(Uri uri)
79    {
80        ImageView image = (ImageView)this.getNextView();
81        image.setImageURI(uri);
82        showNext();
83    }
84
85    /**
86     * Sets a new drawable on the ImageSwitcher.
87     * This will set that drawable on the next ImageView in the switcher and will
88     * then switch to that view.
89     *
90     * @param drawable the drawable to be set or {@code null} to clear the content
91     *
92     * @see ImageView#setImageDrawable(Drawable)
93     */
94    public void setImageDrawable(Drawable drawable)
95    {
96        ImageView image = (ImageView)this.getNextView();
97        image.setImageDrawable(drawable);
98        showNext();
99    }
100
101    @Override
102    public CharSequence getAccessibilityClassName() {
103        return ImageSwitcher.class.getName();
104    }
105}
106