0

i have an app. in my main file, i want to go to another activity. But before that, i want to show some pictures. i saw already some topics here but they cant help me.

this is my idea:

when loading the next screen i want to show different picture But 1 at 1 time, with an interval of 4 seconds and with a nice transition.

i hope somebody can help me ? please help me so quickly as possible.

here's my code:

 <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <ProgressBar
     android:id="@+id/progressBar1"
     style="?android:attr/progressBarStyleLarge"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"
     android:layout_marginBottom="32dp" />
</RelativeLayout>

here my java code:(everything between 2 stars give's an error)

class Main extends AsyncTask<Void, Void, Void> {

int i = 0;
protected Void doInBackground(Void... progress ) {

     try {
    while(i < 3)
        {
        publishProgress();
        i++;
            Thread.sleep(4000);             
        }               
        return null;
    } catch (InterruptedException e) {              
        e.printStackTrace();
    }
    return null;
 }

 protected void onProgressUpdate(Void... j) {

     Drawable image=null;
     try{
         int[] imgs = new int[]{R.drawable.grijs,R.drawable.home_afbeelding,R.drawable.menu};
     image = getResources().**getDrawable**(imgs[i]);
     }
     catch(Exception e)
     {
         image = getResources().**getDrawable**(R.drawable.grijs);
         **splashImage**.setImageDrawable(image);
     }
     **splashImage**.setImageDrawable(image);            
 }

 private Object getResources() {
    // TODO Auto-generated method stub
    return null;
}

protected void onPostExecute(Void i) {
     if(**changed** != true)
        {
        **changed** = true;
        **finish()**;
        **startActivity**(new **Intent**(**classContext()**,**NewActivity**.class));
    }
 }      

}

4

3 に答える 3

0

AsyncTask を使用して画像を変更します。AsyncTask のコードは次のとおりです。そして、あなたはそれを次のように使うべきです

ChangeImages ci = new ChangeImages();
ci.execute();

画像は drawables フォルダーにあり、R.drawable.image#として参照されます。# は番号を表します。遅延を変更する場合は、Thread.sleep(4000);を変更します。4000 は 4 秒に相当します。

private class ChangeImages extends AsyncTask<Void, Void, Void> {

    int i = 0;
    protected Void doInBackground(Void... progress ) {

         try {
        while(i < 3)
            {
            publishProgress();
            i++;
                Thread.sleep(4000);             
            }               
            return null;
        } catch (InterruptedException e) {              
            e.printStackTrace();
        }
        return null;
     }

     protected void onProgressUpdate(Void... j) {

         Drawable image=null;
         try{
             int[] imgs = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3};
         image = getResources().getDrawable(imgs[i]);
         }
         catch(Exception e)
         {
             image = getResources().getDrawable(R.drawable.image1);
             splashImage.setImageDrawable(image);
         }
         splashImage.setImageDrawable(image);            
     }

     protected void onPostExecute(Void i) {
         if(changed != true)
            {
            changed = true;
            finish();
            startActivity(new Intent(classContext(),NewActivity.class));
        }
     }      
 }
于 2013-06-15T17:31:41.367 に答える
0

4秒のスリープ時間で画像を変更するためのスレッドを作成します..

于 2013-06-15T17:01:26.303 に答える
0

AnimationDrawableを使用して簡単に画像を表示できます

res/anim/loading.xml

  <?xml version="1.0" encoding="utf-8"?>
  <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/voipemLoading"
  android:oneshot="false" >

  <item
    android:drawable="@drawable/loading_01"
    android:duration="300"/>
  <item
    android:drawable="@drawable/loading_02"
    android:duration="300"/>
  <item
    android:drawable="@drawable/loading_03"
    android:duration="300"/>
  <item
    android:drawable="@drawable/loading_04"
    android:duration="300"/>

  </animation-list>

dravable_animation.xml

<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"
tools:context=".DravableAnimation"
android:background="@drawable/bg_login" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp"
    android:text="@string/hello_world"
    android:textColor="@color/black"
    android:textSize="24sp" />

<ImageView
    android:id="@+id/loading_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:src="@anim/loading"
    android:visibility="invisible" />

<Button
    android:id="@+id/btn_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loading_image"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp"
    android:text="@string/btn_start" />

DravableAnimation

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;

public class DravableAnimation extends Activity implements OnClickListener {

private AnimationDrawable mAnimation;
private ImageView mAnimLogo;
private Button mbtnStart;
private boolean mStart;

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

    mAnimLogo = (ImageView) findViewById(R.id.loading_image);
    mAnimation = (AnimationDrawable) mAnimLogo.getDrawable();
    mbtnStart = (Button) findViewById(R.id.btn_start);

    mbtnStart.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (!mStart) {
        mAnimLogo.post(new Runnable() {
            @Override
            public void run() {
                mAnimLogo.setVisibility(View.VISIBLE);
                mAnimation.start();
                mStart = true;
                mbtnStart.setText("Stop");
            }
        });
    } else {
        mAnimation.stop();
        mbtnStart.setText("Start");
        mStart = false;
    }
  }
 }

ここからソースコードをダウンロード

于 2013-06-27T17:42:54.923 に答える