2013-05-28 4 views
0

Как название Мне нужно немного помочь моему университетскому проекту, над которым я работаю. Я должен сделать вид деятельности в андроиде, который задал набор изображений, запускающих слайд-шоу с таймером. Эта активность разделена на три части:Android simple gallery custom

  • выше: кнопка для запуска/паузы слайд-шоу изображений
  • центральная часть: текущее изображение, отображаемое в середине экрана
  • под: три изображения, которые представляют , соответственно, предыдущее изображение, текущее и следующее слайд-шоу.

Также, если оно выполнено, салфетка на середине должна перейти к предыдущей или следующей в зависимости от направления движения. Я уже сделал макет xml и acitivity. Может ли кто-нибудь помочь мне, может быть, даже в малейшей степени? Большое спасибо!

<?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; 
    }    
} 
} 
} 



Надеюсь, что это помогает вам, Есть много лучших подходов для этого, но я думаю, что это простой и легкий способ сделать это.
Также .. Я не тестировал его, поэтому вам могут понадобиться некоторые незначительные исправления ... извините