Monday, 31 March 2014

ImageSwitter Functionality Creation in Android Apps


This blog describe about ImageSwitcher view of image gallery.

main.xml


<LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Simple Image Switcher"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:textColor="#336633"
        android:textStyle="bold"/>



    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp" >
    </ImageSwitcher>

</LinearLayout>

ImageSwitcherActivity.java


package com.rakesh.tiwari.imageswitcher;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherActivity extends Activity implements ViewFactory {
int[] imageIDs = { R.drawable.pic1, R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10 };
ImageSwitcher imageSwitcher;
Gallery gallery;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(imageIDs[arg2]);
}
});
}

public class ImageAdapter extends BaseAdapter {

private Context ctx;

public ImageAdapter(Context c) {
ctx = c;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return imageIDs.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(ctx);
imageView.setImageResource(imageIDs[arg0]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));

return imageView;
}

}

@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imgv = new ImageView(this);
imgv.setScaleType(ImageView.ScaleType.FIT_XY);
imgv.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imgv.setBackgroundColor(0xFF000000);
return imgv;
}

}


AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.imageswitcher"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rakesh.tiwari.imageswitcher.ImageSwitcherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


ImageSwitcher View
ImageSwitcher View

ImageSwitcher Widget
ImageSwitcher Widget