main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/gallery1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" >
</ImageSwitcher>
</RelativeLayout>
ImageSwitcherActivity.java
package ums.androideasycode.imageswitcher;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherActivity extends Activity implements ViewFactory
{
int imgs[] =
{
R.drawable.love_island,
R.drawable.blue_bird,
R.drawable.paris,
R.drawable.kittens,
R.drawable.morning_paris
};
ImageSwitcher imgSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imgSwitcher.setFactory(this);
imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
imgSwitcher.setImageResource(imgs[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context ctx;
public ImageAdapter(Context c)
{
ctx = c;
}
public int getCount()
{
return imgs.length;
}
public Object getItem(int arg0)
{
return arg0;
}
public long getItemId(int arg0)
{
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2)
{
ImageView iView = new ImageView(ctx);
iView.setImageResource(imgs[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(130, 170));
return iView;
}
}
public View makeView()
{
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}
Screen-Shots
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/gallery1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" >
</ImageSwitcher>
</RelativeLayout>
ImageSwitcherActivity.java
package ums.androideasycode.imageswitcher;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherActivity extends Activity implements ViewFactory
{
int imgs[] =
{
R.drawable.love_island,
R.drawable.blue_bird,
R.drawable.paris,
R.drawable.kittens,
R.drawable.morning_paris
};
ImageSwitcher imgSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imgSwitcher.setFactory(this);
imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
imgSwitcher.setImageResource(imgs[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context ctx;
public ImageAdapter(Context c)
{
ctx = c;
}
public int getCount()
{
return imgs.length;
}
public Object getItem(int arg0)
{
return arg0;
}
public long getItemId(int arg0)
{
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2)
{
ImageView iView = new ImageView(ctx);
iView.setImageResource(imgs[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(130, 170));
return iView;
}
}
public View makeView()
{
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}
Screen-Shots
Kittens |
Blue Bird |
No comments:
Post a Comment