Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 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 
     17 package com.example.android.apis.view;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.os.Bundle;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.Window;
     27 import android.view.animation.AnimationUtils;
     28 import android.widget.AdapterView;
     29 import android.widget.BaseAdapter;
     30 import android.widget.Gallery;
     31 import android.widget.Gallery.LayoutParams;
     32 import android.widget.ImageSwitcher;
     33 import android.widget.ImageView;
     34 import android.widget.ViewSwitcher;
     35 
     36 
     37 public class ImageSwitcher1 extends Activity implements
     38         AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
     39 
     40     @Override
     41     public void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         requestWindowFeature(Window.FEATURE_NO_TITLE);
     44 
     45         setContentView(R.layout.image_switcher_1);
     46 
     47         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
     48         mSwitcher.setFactory(this);
     49         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
     50                 android.R.anim.fade_in));
     51         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
     52                 android.R.anim.fade_out));
     53 
     54         Gallery g = (Gallery) findViewById(R.id.gallery);
     55         g.setAdapter(new ImageAdapter(this));
     56         g.setOnItemSelectedListener(this);
     57     }
     58 
     59     public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
     60         mSwitcher.setImageResource(mImageIds[position]);
     61     }
     62 
     63     public void onNothingSelected(AdapterView<?> parent) {
     64     }
     65 
     66     public View makeView() {
     67         ImageView i = new ImageView(this);
     68         i.setBackgroundColor(0xFF000000);
     69         i.setScaleType(ImageView.ScaleType.FIT_CENTER);
     70         i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
     71                 LayoutParams.MATCH_PARENT));
     72         return i;
     73     }
     74 
     75     private ImageSwitcher mSwitcher;
     76 
     77     public class ImageAdapter extends BaseAdapter {
     78         public ImageAdapter(Context c) {
     79             mContext = c;
     80         }
     81 
     82         public int getCount() {
     83             return mThumbIds.length;
     84         }
     85 
     86         public Object getItem(int position) {
     87             return position;
     88         }
     89 
     90         public long getItemId(int position) {
     91             return position;
     92         }
     93 
     94         public View getView(int position, View convertView, ViewGroup parent) {
     95             ImageView i = new ImageView(mContext);
     96 
     97             i.setImageResource(mThumbIds[position]);
     98             i.setAdjustViewBounds(true);
     99             i.setLayoutParams(new Gallery.LayoutParams(
    100                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    101             i.setBackgroundResource(R.drawable.picture_frame);
    102             return i;
    103         }
    104 
    105         private Context mContext;
    106 
    107     }
    108 
    109     private Integer[] mThumbIds = {
    110             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
    111             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
    112             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
    113             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
    114 
    115     private Integer[] mImageIds = {
    116             R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
    117             R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
    118             R.drawable.sample_6, R.drawable.sample_7};
    119 
    120 }
    121