如何在android中从图像列表制作幻灯片
我制作了一个android应用程序,因为我得到了一个ImageURL的arrayList,我想用这个arrayList在android中制作一个自动化的lsideShow,我用线程尝试了如下操作,但它给了我ArrayIndexOputoBounds异常,它转到第二个图像abd,然后给出异常,请帮我保存,我的代码如下:如何在android中从图像列表制作幻灯片,android,timer,slideshow,universal-image-loader,picasso,Android,Timer,Slideshow,Universal Image Loader,Picasso,我制作了一个android应用程序,因为我得到了一个ImageURL的arrayList,我想用这个arrayList在android中制作一个自动化的lsideShow,我用线程尝试了如下操作,但它给了我ArrayIndexOputoBounds异常,它转到第二个图像abd,然后给出异常,请帮我保存,我的代码如下: resultArray = new ArrayList<String>(); iv_paly.setOnClickListener(new OnCLickListene
resultArray = new ArrayList<String>();
iv_paly.setOnClickListener(new OnCLickListener()){
@Override
OnClick(){
imageLoader.displayImage(resultArray.get(1), proImage, options);
proImage.postDelayed(swapImage, 3000);
}
MediaPlayer introSound, bellSound;
Runnable swapImage = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= resultArray.size(); i++) {
imageLoader.displayImage(resultArray.get(i), proImage, options);
}
}
};
}
resultArray=newarraylist();
iv_paly.setOnClickListener(新的OnCLickListener()){
@凌驾
OnClick(){
imageLoader.displayImage(resultArray.get(1)、proImage、options);
proImage.postDelayed(swapImage,3000);
}
MediaPlayer声音、贝尔声音;
Runnable swapImage=新Runnable(){
@凌驾
公开募捐{
对于(inti=0;i请尝试以下代码
package com.example.imagegal;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.background);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = {
R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d};
private Integer[] mImageIds = {
R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d};
}
package com.example.imagegal;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Context;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.Window;
导入android.view.animation.AnimationUtils;
导入android.widget.AdapterView;
导入android.widget.BaseAdapter;
导入android.widget.Gallery;
导入android.widget.ImageSwitcher;
导入android.widget.ImageView;
导入android.widget.ViewSwitcher;
导入android.widget.Gallery.LayoutParams;
导入android.support.v4.app.NavUtils;
公共类MainActivity扩展活动实现
AdapterView.OnItemSelectedListener、ViewSwitcher.ViewFactory{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.activity_main);
mSwitcher=(图像切换器)findviewbyd(R.id.switcher);
mSwitcher.setFactory(本);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.滑出(右);
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.slide_in_left));
画廊g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(this));
g、 setOnItemSelectedListener(此);
}
已选择公共位置(AdapterView父视图、视图v、整型位置、长id){
mSwitcher.setImageResource(mimageId[position]);
}
未选择公共无效(AdapterView父级){
}
公共视图makeView(){
ImageView i=新的ImageView(本);
i、 setBackgroundColor(0xFF000000);
i、 setScaleType(ImageView.ScaleType.FIT_CENTER);
i、 setLayoutParams(新的ImageSwitcher.LayoutParams(LayoutParams.MATCH_父级,
LayoutParams.MATCH_PARENT));
返回i;
}
专用图像切换器;
公共类ImageAdapter扩展了BaseAdapter{
公共图像适配器(上下文c){
mContext=c;
}
public int getCount(){
返回mThumbIds.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ImageView i=新的ImageView(mContext);
i、 setImageResource(mThumbIds[position]);
i、 setAdjustViewBounds(真);
i、 setLayoutParams(新建Gallery.LayoutParams(
LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
i、 挫折背景资源(R.drawable.background);
返回i;
}
私有上下文;
}
私有整数[]mThumbIds={
R.drawable.a,R.drawable.b,
R.drawable.c,R.drawable.d};
私有整数[]图像ID={
可拉拔的,可拉拔的,可拉拔的,
R.drawable.d};
}
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp"
/>
</RelativeLayout>
先生,我已经试过了,它一直工作到第二个映像,然后我得到了IndexOutOfBOunds异常..请帮助Private Integer[]mThumbIds和Integer[]mImageIds必须具有相同的大小,如果您在和emulator上运行,请不要放置包含大数据大小的映像,因为这样您将无法执行内存