使用帖子处理程序创建幻灯片

时间:2015-03-05 13:46:42

标签: android

我正在制作Andriod应用程序幻灯片显示图像一个接一个的图像是在drawable中我是使用此代码的应用程序他们是没有错误但仍然我的应用程序不工作请帮助我我真的在需要请帮助我或他们是任何其他方式逐个做100张图片的幻灯片然后请帮助,这是我已经制作代码

public class third extends Activity {
ImageView img ;
Handler handler;
int i;
int[] drawables = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g};
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);

        img = (ImageView) findViewById(R.drawable.a);

        for (i = 1; i<drawables.length;)
        {

             handler = new Handler();
                handler.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      img = (ImageView) findViewById(drawables[i]);
                      i++;
                  }
                }, 2000);



        }

    }

}

1 个答案:

答案 0 :(得分:0)

你可以这样做:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    int[] drawables = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g};

    Handler   handler;
    int       i         = 0;
    ImageView img;

    public void slide() {

        this.handler = new Handler();
        this.handler.postDelayed( new Runnable() {
            @Override
            public void run() {

                MainActivity.this.img.setImageResource( MainActivity.this.drawables[MainActivity.this.i] );

                if ( MainActivity.this.i < ( MainActivity.this.drawables.length - 1 ) ) {

                    MainActivity.this.i++;
                } else {
                    MainActivity.this.i = 0;
                }

                Toast.makeText( MainActivity.this.getBaseContext(), "Next " + MainActivity.this.i, Toast.LENGTH_SHORT ).show();

                MainActivity.this.slide();
            }
        }, 3000 );
    }

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

        this.img = ( ImageView ) this.findViewById( R.id.image );

        this.slide();

    }

}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="200dip"
        android:layout_height="200dip" />

</RelativeLayout>

以下是更好的方法:android-image-slideshow-using-viewpager-pageradapter