2013-05-28 49 views
0

作爲標題,我需要一點幫助,以幫助我正在進行的大學項目。 我必須在android中創建一個活動,給定一組圖像運行帶有計時器的幻燈片。這種活性被分成三個部分:Android簡單圖庫自定義

  • 以上:一個按鈕,開始/暫停圖像的幻燈片
  • 中心部分:表示三個圖像:顯示在中間屏幕
  • 下方的當前圖像,分別顯示上一張圖片,當前和下一張幻燈片。

此外,如果它執行了中間的輕掃,則必須根據輕掃的方向轉到上一個或下一個。 我已經做了佈局xml和活動。有人可以幫助我,甚至可以幫助我嗎?非常感謝!

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#000000" > 

    <Button 
     android:id="@+id/button_start_stop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/button_stop" 
     android:textColor="#FFFFFF" /> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:contentDescription="@string/img_view_desc" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/text_view_notes" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/image_view" 
     android:layout_marginTop="76dp" 
     android:maxLines="3" 
     android:contentDescription="@string/text_view_desc" 
     android:text="@string/default_notes" 
     android:textColor="#FFFFFF"/> 

    <ImageView 
     android:id="@+id/image_view_previous" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:contentDescription="@string/img_view_desc" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/image_view_current" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/image_view" 
     android:layout_alignParentBottom="true" 
     android:contentDescription="@string/img_view_desc" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/image_view_next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:contentDescription="@string/img_view_desc" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

    public class Slideshow extends Activity{ 
     private Button startstop; 
     public ImageView ivdiplayed, ivprevious, ivcurrent, ivnext; 
     boolean isPlaying = true; //true=play | false=stop 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.slideshow); 

      //imageview 
      ivdiplayed = (ImageView) findViewById(R.id.image_view); 
      ivprevious = (ImageView) findViewById(R.id.image_view_previous); 
      ivcurrent = (ImageView) findViewById(R.id.image_view_current); 
      ivnext = (ImageView) findViewById(R.id.image_view_next); 

      //play/stop button 
      startstop = (Button) findViewById(R.id.button_start_stop); 
      startstop.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        if(isPlaying){ 
         startstop.setText(getString(R.string.button_stop)); 
         isPlaying=false; 
        } 
        else{ 
         startstop.setText(getString(R.string.button_play)); 
         isPlaying=true; 
        } 
       } 
      }); 

     } 
    } 

回答

0

爲什麼不使用ViewPager?這將使幻燈片更容易。
您可以用ViewPager替換您的中間(主)圖像。下面是步驟:

第一 - 我們編輯頁面佈局[注意ViewPager]
mainLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#000000" > 

<Button 
    android:id="@+id/button_start_stop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="@string/button_stop" 
    android:textColor="#FFFFFF" /> 

<android.support.v4.view.ViewPager 
android:id="@+id/pagr" 
android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 

<TextView 
    android:id="@+id/text_view_notes" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/image_view" 
    android:layout_marginTop="76dp" 
    android:maxLines="3" 
    android:contentDescription="@string/text_view_desc" 
    android:text="@string/default_notes" 
    android:textColor="#FFFFFF"/> 

<ImageView 
    android:id="@+id/image_view_previous" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:contentDescription="@string/img_view_desc" 
    android:src="@drawable/ic_launcher" /> 

<ImageView 
    android:id="@+id/image_view_current" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/image_view" 
    android:layout_alignParentBottom="true" 
    android:contentDescription="@string/img_view_desc" 
    android:src="@drawable/ic_launcher" /> 

<ImageView 
    android:id="@+id/image_view_next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:contentDescription="@string/img_view_desc" 
    android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 



然後,我們建立我們的形象,項目視圖,將在ViewPager顯示被。
imageitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/imageItem" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:contentDescription="my mage" /> 



,我們需要創建適配器的ViewPager
ImageAdapter.Java類

public class ImageAdapter extends PagerAdapter { 

// list of images resources ids 
private ArrayList<Integer> items; 
private Context context; 

public ImageAdapter(Context context, ArrayList<Integer> items) { 
    super(); 
    this.context = context; 
    this.items = items; 
} 

@Override 
public int getCount() { 
    return items.size(); 
} 

@Override 
public Object instantiateItem(View collection, int position) { 
    View v = null; 
LayoutInflater vi = null; 

    if (v == null) { 
     vi = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.imageitem, null); 
    } 

    } 

    // sets the image resource to our ImageView item 
    ImageView imageView = (ImageView) v.findViewById(R.id.imageItem); 
    imageView.setImageResource(items.get(position)); 

    // add the image to ViewPager 
    ((ViewPager) collection).addView(v); 

    return v; 
} 

@Override 
public void destroyItem(View collection, int position, Object view) { 
    ((ViewPager) collection).removeView((View) view); 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((View) object); 
} 

@Override 
public Parcelable saveState() { 
    return null; 
} 
} 



內處理這些圖像後最後我們的MainActivity Cl屁股
Slideshow.Java類

import android.app.Activity; 
import android.view.View; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.ImageView; 

public class Slideshow extends Activity{ 
private ViewPager viewPager; 
private ImageAdapter adapter; 
    private Button startstop; 
private Thread timerThread; 
    public ImageView ivdiplayed, ivprevious, ivcurrent, ivnext; 
    boolean isPlaying = true; //true=play | false=stop 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.slideshow); 

    // Setup Images List 
ArrayList<Integer> imageResources = new ArrayList<Integer>(); 
imageResources.add(R.drawable.image1); 
imageResources.add(R.drawable.image2); 
imageResources.add(R.drawable.image3); // etc.. add all images you got 

// Initialize our timer thread 
timerThread = new Thread(this); 

     //Init our ViewPager 
     viewPager = (ViewPager) findViewById(R.id.pagr); 

    //Init our Adapter and pass it the list of images 
    adapter = new ImagesAdapter(this, imageResources); 

    // Set the adapter to the view 
    viewPager.setAdapter(adapter); 

    ivprevious = (ImageView) findViewById(R.id.image_view_previous); 
     ivcurrent = (ImageView) findViewById(R.id.image_view_current); 
     ivnext = (ImageView) findViewById(R.id.image_view_next); 

    // To detect when image changes 
    viewPager.setOnPageChangeListener(new OnPageChangeListener() { 

     public void onPageSelected(int position) { 
     if(position>0)// No images before 
      ivprevious.setImageResource(imageResources.get(position-1)); 
       ivcurrent.setImageResource(imageResources.get(position-1)); 

     if(position<(imageResources.size()-1))// No images after 
       ivnext.setImageResource(imageResources.get(position+1)); 
     } 
    }); 

     //play/stop button 
     startstop = (Button) findViewById(R.id.button_start_stop); 
     startstop.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if(isPlaying){ 
        startstop.setText(getString(R.string.button_stop)); 
        isPlaying=false; 
       } 
       else{ 
     /*Start our timer.. You can use AlarmManger, but I think this will do the trick*/ 
     timerThread.start(); 
        startstop.setText(getString(R.string.button_play)); 
        isPlaying=true; 
       } 
      } 
     }); 
    } 

// Our Timer thread function 
public void run() { 
// The position of the current image shown in the ViewPager 
int position; 

while (isPlaying) { //condition to exit thread loop 
//Get which image is selected in the ViewPager 
position = viewPager.getCurrentItem(); 
    try { 
// Time between each slide in MilliSeconds 
     Thread.sleep(1000); 

// Move to next image if available OR go to first one 
if(position<(imageResources.size()-1)){ 
     viewPager.setCurrentItem(position+1); 
}else{ 
    viewPager.setCurrentItem(0); 
} 

    } catch (InterruptedException e) { 
     return; 
    } catch (Exception e) { 
     return; 
    }    
} 
} 
} 



希望這可以幫助你,還有很多爲更好的方法,但我認爲這是一個簡單易用的方式來做到這一點。
另外..我沒有測試它,所以你可能需要一些小的修復...對不起