Powered By Blogger

Monday 27 February 2012

GalleryImgSwitcher


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Gallery2 extends Activity {
    /** Called when the activity is first created. */
   
Button b1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener()
        {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Gallerynew.class);
startActivity(i);

}
       
        });
    }
}
Gallery.java


import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
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.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;

public class Gallerynew extends Activity implements ViewFactory{
Gallery g;
Integer i[]={R.drawable.crane,R.drawable.face,R.drawable.watch};
ImageSwitcher img;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
img=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
img.setFactory(this);
img.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
img.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener()
{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"you selected"+(arg2+1)+"picture",Toast.LENGTH_LONG).show();
img.setImageResource(i[arg2]);
}

});


}
public View makeView() {
// TODO Auto-generated method stub
ImageView im=new ImageView(this);
im.setBackgroundColor(0xFF000000);
im.setScaleType(ImageView.ScaleType.FIT_XY);
im.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return im;
}
public class ImageAdapter extends BaseAdapter
{
  Context c;
  int itembackground;
public ImageAdapter(Context con)
{
c=con;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itembackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return i.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView img=new ImageView(c);
img.setImageResource(i[position]);
img.setLayoutParams(new Gallery.LayoutParams(150,120));
img.setBackgroundResource(itembackground);
return img;
}

}

}


No comments:

Post a Comment