易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Android ImageSwitcher 图片切换 按钮点击切换
图片显示: 1. MainActivity
package
com.gamedog.test;
import
Android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageSwitcher;
import
android.widget.ImageView;
import
android.widget.ViewSwitcher.ViewFactory;
public
class
MainActivity
extends
Activity
implements
ViewFactory
{
private
ImageSwitcher switcher;
private
Button forward;
private
Button next;
// 图片索引
private
static
int
index =
0
;
// 显示的图片资源
private
static
final
Integer[] imagelist =
{ R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6 };
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
forward = (Button) findViewById(R.id.forward);
next = (Button) findViewById(R.id.next);
switcher = (ImageSwitcher) findViewById(R.id.image);
switcher.setFactory(
this
);
switcher.setImageResource(imagelist[index]);
// 上一张
forward.setOnClickListener(
new
View.OnClickListener()
{
@Override
public
void
onClick(View view)
{
index--;
if
(index <
0
)
{
index = imagelist.length -
1
;
}
switcher.setImageResource(imagelist[index]);
}
});
// 下一张
next.setOnClickListener(
new
View.OnClickListener()
{
@Override
public
void
onClick(View view)
{
index++;
if
(index >= imagelist.length)
{
index =
0
;
}
switcher.setImageResource(imagelist[index]);
}
});
}
// 用于显示图片
@Override
public
View makeView()
{
return
new
ImageView(
this
);
}
}
2.布局文件
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<ScrollView
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"horizontal"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:gravity
=
"center"
android:background
=
"#f0f0f0"
>
<Button
android:id
=
"@+id/forward"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/left_btn"
android:layout_gravity
=
"center"
/>
<ImageSwitcher
android:id
=
"@+id/image"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginLeft
=
"20dp"
android:layout_marginRight
=
"20dp"
/>
<Button
android:id
=
"@+id/next"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/right_btn"
android:layout_gravity
=
"center"
/>
</LinearLayout>
</ScrollView>
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图