Keys;
- ViewSwitcher
- ViewSwitcher ViewFactory
- ViewSwitcher setFactory
- ViewSwitcher setInAnimation
- ViewSwitcher setOutAnimation
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#ff000000"
tools:context="com.example.imageswitcher.MainActivity">
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Gallery>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="turkeyGallery">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory {
Integer[] imageIDs = {
R.drawable.res1,
R.drawable.res2,
R.drawable.res3,
R.drawable.res4,
R.drawable.res5,
};
private ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
imageSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
@SuppressWarnings("deprecatin")
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
public void onItemClick(AdapterView < ?>adapterView, View view, int i, long l) {
imageSwitcher.setImageResource(imageIDs[i]);
}
});
}
@SuppressWarnings("deprecation")
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT));
return imageView;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
TypedArray typedArray = obtainStyledAttributes(R.styleable.turkeyGallery);
itemBackground = typedArray.getResourceId(R.styleable.turkeyGallery_android_galleryItemBackground, 0);
typedArray.recycle();
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View converView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
İlk Yorumu Siz Yapın