2013-02-27 92 views
0

以下代碼正在從左到右刷卡,現在我想設置the同樣的事情從右到左also.swiping正在工作,但講話正在與position.in page碰撞.Adapter向前刷卡工作正常。落後的重擊,也只工作不錯,但文本到語音無法正常工作viewpager刷卡

下面是代碼:

public Object instantiateItem(ViewGroup container, int position) { 
       ImageView image=new ImageView(context); 
       image.setImageResource(fruits[position]); 
       ((ViewPager) container).addView(image); 
       if(position>=1){ 
        image.setImageResource(fruits[position]); 
        speakOut(names[position-1]); 
       } 
       return image; 
      } 

這裏它不工作。任何一個可以給我的建議,並帶我出去這個問題的

public class Learn extends Activity implements TextToSpeech.OnInitListener{ 
     private TextToSpeech tts; 
     private ViewPager pic; 
     private Button next,back; 
     private TextView text; 
     private Context context; 
     private int i=0; 
     public Integer[] fruits={R.drawable.apple1,R.drawable.banana1,R.drawable.grapes1,R.drawable.cherry1,R.drawable.lemon1, 
       R.drawable.orange1,R.drawable.strwberry1,R.drawable.water1}; 
     public String [] names={"Apple","Banana","Grapes","Cherry","Lemon","Orange","Strawberry","Water melon"}; 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.learn); 
      tts = new TextToSpeech(this, this); 
      pic=(ViewPager)findViewById(R.id.view_pager); 
      next=(Button)findViewById(R.id.button1); 
      back=(Button)findViewById(R.id.button2); 
      text=(TextView)findViewById(R.id.text); 
      ImagePageAdapter adapterpic=new ImagePageAdapter(this); 
      pic.setAdapter(adapterpic); 

     } 
     private class ImagePageAdapter extends PagerAdapter { 

      public ImagePageAdapter(Context learn) { 
       // TODO Auto-generated constructor stub 
       context=learn; 
      } 

      @Override 
      public int getCount() { 
       // TODO Auto-generated method stub 
       return names.length; 
      } 

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

      @Override 
      public Object instantiateItem(ViewGroup container, int position) { 
       ImageView image=new ImageView(context); 
       image.setImageResource(fruits[position]); 
       ((ViewPager) container).addView(image); 
       if(position>=1){ 
        image.setImageResource(fruits[position]); 
        speakOut(names[position-1]); 
       } 
       return image; 
      } 
      @Override 
      public void destroyItem(ViewGroup container, int position, Object object) { 
       ((ViewPager) container).removeView((ImageView) object); 
      } 

     } 
     private void speakOut(String text) { 
      // TODO Auto-generated method stub 
      tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
     } 
     @Override 
     public void onDestroy() { 
      // Don't forget to shutdown tts! 
      if (tts != null) { 
       tts.stop(); 
       tts.shutdown(); 
      } 
      super.onDestroy(); 
     } 

     @Override 
     public void onInit(int status) { 

      if (status == TextToSpeech.SUCCESS) { 

       int result = tts.setLanguage(Locale.US); 

       if (result == TextToSpeech.LANG_MISSING_DATA 
         || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
        Log.e("TTS", "This Language is not supported"); 
       } else { 
        pic.setEnabled(true); 

        //speakOut("apple"); 
       } 

      } else { 
       Log.e("TTS", "Initilization Failed!"); 
      } 

     } 
    } 
    Here is stack trace 

02-27 16:39:00.994: I/TTS received:(2369): Apple 
02-27 16:39:05.144: I/TTS received:(2369): Banana 
02-27 16:39:06.534: I/TTS received:(2369): Grapes 
02-27 16:39:24.273: I/TTS received:(2369): Banana 
02-27 16:39:25.355: I/TTS received:(2369): Grapes 
02-27 16:39:26.593: I/TTS received:(2369): Cherry 
02-27 16:39:28.624: I/TTS received:(2369): Apple 

請給我建議,要走出這個問題的

+0

我建議添加堆棧跟蹤可能有幫助 – chip 2013-02-27 08:26:30

+0

02-27 14:03:31.824:W/KeyCharacterMap(2013):無鍵盤ID 0 02-27 14:03:31.824:W/KeyCharacterMap(2013) :使用默認鍵盤映射:/system/usr/keychars/qwerty.kcm.bin 02-27 14:03:45.623:收到的I/TTS:(2013):Apple 02-27 14:03:48.293:I/TTS (2013):香蕉 02-27 14:03:49.543:收到的I/TTS:(2013):葡萄 02-27 14:03:51.473:收到的I/TTS:(2013):Cherry 02- 27 14:03:53.353:收到的I/TTS:(2013):Lemon 02-27 14:03:56.254:收到的I/TTS:(2013):香蕉 02-27 14:03:58.534:I/TTS收到:(2013):蘋果 – 2013-02-27 08:34:23

+1

考慮使用碎片sw通過他們通過ip。 – jlopez 2013-02-27 08:46:18

回答

0

你爲什麼不嘗試把物體的聲音,而不是將它們轉換文本to speech ..你可以試試這個代碼

public class Learn extends Activity { 

    MediaPlayer mp; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_numbers); 
     ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager); 
     ImagePagerAdapter adapter = new ImagePagerAdapter(); 
     viewPager.setAdapter(adapter); 
    } 

private class ImagePagerAdapter extends PagerAdapter { 
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,R.drawable.no10,R.drawable.bg2}; 

private int[] mAudio = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,R.raw.ten,R.raw.one}; 

     @Override 
     public int getCount() { 
     return mImages.length; 
     } 

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

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      Context context = Numbers.this; 
      ImageView imageView = new ImageView(context); 
      int padding =context.getResources(). 
      getDimensionPixelSize(R.dimen.activity_vertical_margin); 
      imageView.setPadding(padding, padding, padding, padding); 
      imageView.setScaleType(ImageView.ScaleType.CENTER); 
      imageView.setImageResource(mImages[position]); 
      ((ViewPager) container).addView(imageView, 0); 
      if(position>=1){ 
      mp = MediaPlayer.create(Numbers.this, mAudio[position-1]); 
      mp.start(); 
      } 
      return imageView; 
     } 

      @Override 
     public void destroyItem(ViewGroup container, int position, Object object) { 
      ((ViewPager) container).removeView((ImageView) object); 
     } 
    } 
    } 

這裏值可以根據你的要求改變。