Android ImageSwitcher

Android ImageSwitcher

Android ImageSwitcher – Sometimes you do not want a pic to seem abruptly on the display screen, alternatively you want to use some type of animation to the image/pic when it transitions in one image to some other. This is recognized by android by means of android ImageSwitcher.

Android ImageSwitcher

Android imageswticher  gives you to include some transitions on the images through just how they seem on display. To be able to use android imageswitcher, you will need to first explain its XML part. Its syntax is listed below

<ImageSwitcher
   android:id="@+id/imageSwitcher1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true" >
</ImageSwitcher>

 

Now we create an intance of android ImageSwithcer in java file and get a reference point of the XML part. Its syntax is listed below

private ImageSwitcher imageSwitcher;
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);

 

The following point we have to do put into action the ViewFactory put into action and program unimplemented method that results an imageView. Its syntax is below

imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
   public View makeView() {
      ImageView myView = new ImageView(getApplicationContext());
      return myView;
   }
}

 

The very last thing you must do is to include animation to the android ImageSwitcher. You will need to establish an subject of Animation course through AnimationUtilities course by dialling a static method loadAnimation. Its syntax is listed below

Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);

 

The technique setInAnimaton places the animation of the looks of the thing on the display whereas setOutAnimation does indeed the opposite. The technique loadAnimation() creates an animation object.

From these methods apart, there are other methods identified in the android  ImageSwitcher class. They are described below

Methods

  1. setImageDrawable(Drawable drawable)
  2. setImageResource(int resid)
  3. setImageURI(Uri uri)
  4. ImageSwitcher(Context context, AttributeSet attrs)
  5. onInitializeAccessibilityEvent (AccessibilityEvent event)
  6. onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Android ImageSwticher Example

The below android imageswitcher example shows a few of the image switcher results on the bitmap. It crates a simple application which allows you to see the animation results on the images.

To test out this android imageswticher example , you will need to perform this on a genuine device.

Steps     Description

  1. You use Android studio room IDE to make an Android software under a program com.example.sappal.myapplication.
  2. Modify src/MainActivity.java to include necessary code.
  3. Modify the res/structure/activity_main to include respected XML components
  4. Run the application form and choose a working android os device and set up the application onto it and validate the results

Content -> src/MainActivity.java.

Inside the below code abc and tp implies the company logo of http://www.androdevelopment.com

package com.example.sappal.myapplication;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
   private ImageSwitcher sw;
   private Button b1,b2;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);

      sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
      sw.setFactory(new ViewFactory() {
         @Override
         public View makeView() {
            ImageView myView = new ImageView(getApplicationContext());
            myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            myView.setLayoutParams(new 
               ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                  LayoutParams.WRAP_CONTENT));
            return myView;
         }
      });

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "previous Image",
               Toast.LENGTH_LONG).show();
            sw.setImageResource(R.drawable.abc);
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Next Image",
               Toast.LENGTH_LONG).show();
            sw.setImageResource(R.drawable.tp);
         }
      });
   }
}

Modified Content -> res/layout/activity_main.xml.

<?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:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
      
   <TextView android:text="Gestures  Example" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageSwitcher
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageSwitcher"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="168dp" />
      
   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/left"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/right"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button" />
      
</RelativeLayout>

 

Content -> Strings.xml.

<resources>
    <string name="app_name">My Application</string>
    <string name="left"><![CDATA[<]]></string>
    <string name="right"><![CDATA[>]]></string>
</resources>

 

Content -> AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sappal.myapplication"

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication.MainActivity"
         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>

 

Let’s make an effort to run the android imageswitcher application we just revised. I assume you’d created your AVD while doing android environment setup. To perform the  app from Android studio, wide open one of your project’s activity data and click Run Eclipse Run Icon from the toolbar. Google android studio room installs the app on your AVD and begins it of course, if everything is okay with your installation and application, it’ll screen pursuing Emulator screen

android imageswitcher

Now if you can look at the device display , you will notice both keys.

Now just choose the top button that right arrow. An image seems from right and move towards left. It really is shown below

android imageswitcher

Now touch on the below button, that provides back the prior image with some move. It really is shown below

android imageswitcher