آموزش کار با ImageSwitcher در برنامه نویسی اندروید

سلام دوستان در این سری از آموزش برنامه نویسی اندروید به آموزش کار با ImageSwitcher در برنامه نویسی اندروید می پردازیم از ImageSwitcher برای ساخت slider در برنامه استفاده می شود. به طو مثال اگر شما بخواهید چند تا عکس را به صورت اسلاید نشان دهید باید از ImageSwitcher استفاده کنید در ادامه با ما همراه باشید.
پس یک وارد layout خود شده کد های زیر را در آن قرار دهید.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:gravity="center_horizontal"
    android:orientation="vertical">
    <TextView
        android:id="@+id/androidImageSwitcherTutorial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ImageSwitcher Example By Programchi.ir"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="28dp"
        android:layout_marginTop="40dp" />
    <Button
        android:id="@+id/nextImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:onClick="nextImageButton"
        android:text="Next Image" />
    <TextView
        android:id="@+id/android_image_switcher_tutorial"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:autoLink="web"
        android:gravity="bottom|center"
        android:text="Programchi.ir"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />
</LinearLayout>

در بالا یک ImageSwitcher داریم و یک دکمه که با کلیک بروی  آن به اسلاید بعدی برویم
حالا باید کد های مربوط به اکتیویتی خودمان را بنویسم پس یک فایل به نام AndroidImageSwitcherExample.java ایجاد کنید و کد های زیر را در آن قرار دهید.
قبل از اینکه از کد زیر استفاده کنید باید یکسری عکس در پوشه drawable قرار دهید سپس در بخش imageSwitcherImages نام آنها را تغییر دهید.

package ir.proframchi;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class AndroidImageSwitcherExample extends AppCompatActivity {
    ImageSwitcher myImageSwitcher;
    Button nextImageButton;
    int imageSwitcherImages[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6};
    int switcherImage = imageSwitcherImages.length;
    int counter = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_image_switcher_example);
        myImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        nextImageButton = (Button) findViewById(R.id.nextImageButton);
        myImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView switcherImageView = new ImageView(getApplicationContext());
                switcherImageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT
                ));
                switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                switcherImageView.setImageResource(R.drawable.img1);
                //switcherImageView.setMaxHeight(100);
                return switcherImageView;
            }
        });
        Animation animationOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        Animation animationIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        myImageSwitcher.setOutAnimation(animationOut);
        myImageSwitcher.setInAnimation(animationIn);
    }
    public void nextImageButton(View view) {
        counter++;
        if (counter == switcherImage)
            counter = 0;
        myImageSwitcher.setImageResource(imageSwitcherImages[counter]);
    }
}

در ابتدا در imageSwitcherImages عکس ها را قرار دادهایم سپس اندازه آن را به دست آوریدم برای اینکه زمانی که روی دکمه کلیک شد عکس تغییر کند از متد setFactory در ImageSwitcher استفاده کردیم برای اینکه یک انیمیشنی هم داشته باشد از انیمیشن های درون ساخت اندروید استفاده کردیم و void که در آخر استفاده شده است ( nextImageButton ) برای تغییر عکس است.
 
این آموزش هم به پایان رسید.
 
موفق و موید باشید.

مطالعه بیشتر