2014-10-19 43 views
0

我是Eclipse和Android開發的新手。我正在開發一個應用程序,並且希望在我的啓動畫面具有由圖片組成的背景並以每張圖片之間具有5秒的幻燈片形式顯示後顯示的第一個屏幕。我不知道如何解決這個問題,並會感謝每一個有用的材料,可以指導我完成這件事。依靠你的創意。我還需要知道圖片應該放入哪種格式,以及它們是否必須具有相同的尺寸。 謝謝。我如何在eclipse中創建自動滑動圖像的背景

使用代碼進行編輯。

package com.joel.mybusapp; 

import android.support.v7.app.ActionBarActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ViewFlipper; 

public class MainActivity extends ActionBarActivity { 

Animation slide_in_left, slide_out_right; 
ViewFlipper viewFlipper; 

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

    viewFlipper = (ViewFlipper) this.findViewById(R.id.bckgrndViewFlipper1); 

    slide_in_left = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_in_left); 
      slide_out_right = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_out_right); 

      viewFlipper.setInAnimation(slide_in_left); 
      viewFlipper.setOutAnimation(slide_out_right);    

     //sets auto flipping 
      viewFlipper.setAutoStart(true); 
      viewFlipper.setFlipInterval(5000); 
      viewFlipper.startFlipping(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_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="com.joel.mybusapp.MainActivity" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:orientation="vertical" > 

    <ViewFlipper 
     android:id="@+id/bckgrndViewFlipper1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <ImageView 
     android:id="@+id/bckgrndImageView5" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen6" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen6png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView4" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen5" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen5png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen3" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen3png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen2" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen2png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen1" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen1png" /> 
    </ViewFlipper>    

</LinearLayout> 

我設法後臺自動帶ViewFLipper和ImageView的工具滑動。上面的代碼的作品只是顯示屏幕周圍有一個白色寄宿生,我想消失。我已經逐字地將每個容器填充爲空,但白色寄宿生物仍然存在。我的意思是屏幕截圖如下。我需要一些幫助擺脫白人邊境。 enter image description here

+0

請告訴我們您的代碼迄今爲止。只需使用他們目前正在使用的格式即可。不要害怕犯錯誤。您以後可以始終優化最終結果。 – 2014-10-19 18:19:35

+0

@StephanBranczyk謝謝。迄今爲止,我已經用代碼更新了這個問題。 – Selase 2014-10-19 18:38:09

+0

你的形象在哪裏? – 2014-10-19 19:01:52

回答

1

下面固定的問題,感謝解決了所有問題的想法來自@Frederick nyawaya

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom" 
    tools:context="com.joel.mybusapp.MainActivity" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:orientation="vertical" > 

    <ViewFlipper 
     android:id="@+id/bckgrndViewFlipper1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <ImageView 
     android:id="@+id/bckgrndImageView5" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen6" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen6png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView4" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen5" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen5png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen3" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen3png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen2" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen2png" /> 

    <ImageView 
     android:id="@+id/bckgrndImageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/bkscreen1" 
     android:scaleType="centerCrop" 
     android:src="@drawable/backscreen1png" /> 

     </ViewFlipper>    

    </LinearLayout> 


</RelativeLayout> 

MainActivity.java

代碼
package com.joel.mybusapp; 

import android.support.v7.app.ActionBarActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ViewFlipper; 

public class MainActivity extends ActionBarActivity { 

Animation slide_in_left, slide_out_right; 
ViewFlipper viewFlipper; 

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

    viewFlipper = (ViewFlipper) this.findViewById(R.id.bckgrndViewFlipper1); 

    slide_in_left = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_in_left); 
      slide_out_right = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_out_right); 

      viewFlipper.setInAnimation(slide_in_left); 
      viewFlipper.setOutAnimation(slide_out_right);    

     //sets auto flipping 
      viewFlipper.setAutoStart(true); 
      viewFlipper.setFlipInterval(5000); 
      viewFlipper.startFlipping(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 
} 
0

使用ViewFlipper。您將需要添加具有所需位圖的ImageView並開始翻轉視圖。圖像應該在你的res/drawables目錄中,PNG或JPEG,但是當然它們會被加載爲位圖。它們不需要具有相同的大小,請使用ImageView的SCALE TYPE屬性來調整其外觀。

+0

謝謝。我不知道這個概念是如何工作的,所以我有點困惑。我是否需要爲每張圖片添加imageViews和viewflipper,或者先添加所有imageViews,然後添加一個單視圖翻轉器來翻轉視圖? – Selase 2014-10-20 01:30:13

+0

謝謝@fredrick。我不知道這個概念是如何工作的,所以我有點困惑。我是否需要爲每張圖片添加imageViews和viewflipper,或者先添加所有imageViews,然後添加一個單視圖翻轉器來翻轉視圖?另外什麼值的viewFLipper我必須設置過渡自動發生後每隔5secs從右到左,並啓動一次頁面/視圖加載 – Selase 2014-10-20 02:05:39

+0

@fredrick我需要添加例外以避免我的應用程序粉碎? – Selase 2014-10-20 03:33:06