1- ImageSwitcher overview
-
-
This is an application that uses ImageSwitcher:
-
-
- TODO: More infos
- This is an application that uses ImageSwitcher:
- TODO: More infos
2- Android ImageSwitcher example
-
-
Create a project named AndroidImageSwitcher:
-
-
You need some images participate in the examples:
-
image1.png
image2.png
image3.png
-
Copy the images above into drawable folder.
-
-
Application interface:
-
-
main_actvity.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
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"
tools:context
=
".MainActivity"
>
<
ImageSwitcher
android:layout_width
=
"match_parent"
android:layout_height
=
"250dp"
android:id
=
"@+id/imageSwitcher"
android:layout_alignParentTop
=
"true"
android:layout_centerHorizontal
=
"true"
/>
<
Button
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Previous"
android:id
=
"@+id/button_previous"
android:layout_marginLeft
=
"94dp"
android:layout_marginStart
=
"94dp"
android:layout_alignParentBottom
=
"true"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentStart
=
"true"
/>
<
Button
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Next"
android:id
=
"@+id/button_next"
android:layout_alignTop
=
"@+id/button_previous"
android:layout_toRightOf
=
"@+id/button_previous"
android:layout_toEndOf
=
"@+id/button_previous"
/>
</
RelativeLayout
>
-
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package
org.o7planning.androidimageswitcher;
import
android.app.ActionBar.LayoutParams;
import
android.app.Activity;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.util.Log;
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.Toast;
import
android.widget.ViewSwitcher.ViewFactory;
public
class
MainActivity
extends
Activity {
private
ImageSwitcher imageSwitcher;
private
Button buttonPrevious;
private
Button buttonNext;
private
final
String[] imageNames={
"image1"
,
"image2"
,
"image3"
};
private
int
currentIndex;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPrevious = (Button) findViewById(R.id.button_previous);
buttonNext = (Button) findViewById(R.id.button_next);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// Animation when switching to another image.
Animation out= AnimationUtils.loadAnimation(
this
, android.R.anim.fade_out);
Animation in= AnimationUtils.loadAnimation(
this
, android.R.anim.fade_in);
// Set animation when switching images.
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
//
imageSwitcher.setFactory(
new
ViewFactory() {
// Returns the view to show Image
// (Usually should use ImageView)
@Override
public
View makeView() {
ImageView imageView =
new
ImageView(getApplicationContext());
imageView.setBackgroundColor(Color.LTGRAY);
imageView.setScaleType(ImageView.ScaleType.CENTER);
ImageSwitcher.LayoutParams params=
new
ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
return
imageView;
}
});
this
.currentIndex=
0
;
this
.showImage(
this
.currentIndex);
buttonPrevious.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
previousImage();
}
});
buttonNext.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
nextImage();
}
});
}
private
void
previousImage() {
if
(currentIndex >
0
) {
currentIndex--;
}
else
{
Toast.makeText(getApplicationContext(),
"No Previous Image"
, Toast.LENGTH_SHORT).show();
return
;
}
this
.showImage(currentIndex);
}
private
void
nextImage() {
if
(currentIndex <
this
.imageNames.length-
1
) {
currentIndex++;
}
else
{
Toast.makeText(getApplicationContext(),
"No Next Image"
, Toast.LENGTH_SHORT).show();
return
;
}
this
.showImage(currentIndex);
}
private
void
showImage(
int
imgIndex) {
String imageName=
this
.imageNames[imgIndex];
int
resId= getDrawableResIdByName(imageName);
if
(resId!=
0
) {
this
.imageSwitcher.setImageResource(resId);
}
}
// Find Image ID corresponding to the name of the image (in the drawable folder).
public
int
getDrawableResIdByName(String resName) {
String pkgName =
this
.getPackageName();
// Return 0 if not found.
int
resID =
this
.getResources().getIdentifier(resName ,
"drawable"
, pkgName);
Log.i(
"MyLog"
,
"Res Name: "
+ resName +
"==> Res ID = "
+ resID);
return
resID;
}
}
-
Running the apps:
-
- Create a project named AndroidImageSwitcher:
- You need some images participate in the examples:
image1.png image2.png image3.png - Copy the images above into drawable folder.
- Application interface:
- main_actvity.xml1234567891011121314151617181920212223242526272829303132333435363738394041
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
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"
tools:context
=
".MainActivity"
>
<
ImageSwitcher
android:layout_width
=
"match_parent"
android:layout_height
=
"250dp"
android:id
=
"@+id/imageSwitcher"
android:layout_alignParentTop
=
"true"
android:layout_centerHorizontal
=
"true"
/>
<
Button
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Previous"
android:id
=
"@+id/button_previous"
android:layout_marginLeft
=
"94dp"
android:layout_marginStart
=
"94dp"
android:layout_alignParentBottom
=
"true"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentStart
=
"true"
/>
<
Button
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Next"
android:id
=
"@+id/button_next"
android:layout_alignTop
=
"@+id/button_previous"
android:layout_toRightOf
=
"@+id/button_previous"
android:layout_toEndOf
=
"@+id/button_previous"
/>
</
RelativeLayout
>
- MainActivity.java123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
package
org.o7planning.androidimageswitcher;
import
android.app.ActionBar.LayoutParams;
import
android.app.Activity;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.util.Log;
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.Toast;
import
android.widget.ViewSwitcher.ViewFactory;
public
class
MainActivity
extends
Activity {
private
ImageSwitcher imageSwitcher;
private
Button buttonPrevious;
private
Button buttonNext;
private
final
String[] imageNames={
"image1"
,
"image2"
,
"image3"
};
private
int
currentIndex;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPrevious = (Button) findViewById(R.id.button_previous);
buttonNext = (Button) findViewById(R.id.button_next);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// Animation when switching to another image.
Animation out= AnimationUtils.loadAnimation(
this
, android.R.anim.fade_out);
Animation in= AnimationUtils.loadAnimation(
this
, android.R.anim.fade_in);
// Set animation when switching images.
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
//
imageSwitcher.setFactory(
new
ViewFactory() {
// Returns the view to show Image
// (Usually should use ImageView)
@Override
public
View makeView() {
ImageView imageView =
new
ImageView(getApplicationContext());
imageView.setBackgroundColor(Color.LTGRAY);
imageView.setScaleType(ImageView.ScaleType.CENTER);
ImageSwitcher.LayoutParams params=
new
ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
return
imageView;
}
});
this
.currentIndex=
0
;
this
.showImage(
this
.currentIndex);
buttonPrevious.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
previousImage();
}
});
buttonNext.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
nextImage();
}
});
}
private
void
previousImage() {
if
(currentIndex >
0
) {
currentIndex--;
}
else
{
Toast.makeText(getApplicationContext(),
"No Previous Image"
, Toast.LENGTH_SHORT).show();
return
;
}
this
.showImage(currentIndex);
}
private
void
nextImage() {
if
(currentIndex <
this
.imageNames.length-
1
) {
currentIndex++;
}
else
{
Toast.makeText(getApplicationContext(),
"No Next Image"
, Toast.LENGTH_SHORT).show();
return
;
}
this
.showImage(currentIndex);
}
private
void
showImage(
int
imgIndex) {
String imageName=
this
.imageNames[imgIndex];
int
resId= getDrawableResIdByName(imageName);
if
(resId!=
0
) {
this
.imageSwitcher.setImageResource(resId);
}
}
// Find Image ID corresponding to the name of the image (in the drawable folder).
public
int
getDrawableResIdByName(String resName) {
String pkgName =
this
.getPackageName();
// Return 0 if not found.
int
resID =
this
.getResources().getIdentifier(resName ,
"drawable"
, pkgName);
Log.i(
"MyLog"
,
"Res Name: "
+ resName +
"==> Res ID = "
+ resID);
return
resID;
}
}
- Running the apps:
Comments
Post a Comment