下面的代码正在从左向右滑动现在我想从右到左设置the
同样的事情.swiping正在工作但是speakout正在崩溃与position.in pageAdapter正向滑动正在工作好的。向后滑动也很好,但是Textto语音不能正常工作
以下是代码:
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 :(得分:0)
为什么不尝试放置对象的声音而不是将它们转换为语音..您可以尝试此代码
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);
}
}
}
您可以根据自己的要求改变价值。