说到Log1那么crashes.Whats错了?
It comes to Log1 then crashes.Whats wrong?
"is"是Imageswitcher的ID
s 是数组中的图像编号
数组包含 5 images:play 停止下一个上一个暂停
public class MainActivity extends AppCompatActivity {
Button btnnext,btnprev;
ImageSwitcher is;
int s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Integer[] img=new Integer[]{R.drawable.next,R.drawable.play,R.drawable.previous,R.drawable.stop,R.drawable.pause,};
btnnext=(Button)findViewById(R.id.btnnext);
btnprev=(Button)findViewById(R.id.btnprev);
is =(ImageSwitcher) findViewById(R.id.is);
Log.i("log1","1");
is.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView=new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
下面这行有什么问题吗?
imageView.setLayoutParams(new ImageSwitcher.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
is.setImageResource(img[s]);
Log.i("log","4");
return imageView;
}
});
Log.i("log2", "2");
按钮下一个代码:
btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s+1)]);
}
});
Log.i("log3", "3");
按钮以前的代码:
btnprev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s-1)]);
}
});
}
}
应删除此代码:
is.setImageResource(img[s]);
而不是:
is.setImageResource(img[(s+1)]);
我应该使用:
currentImage++;
currentImage = currentImage % img.length;
is.setImageResource(img[currentImage]);
而不是:
is.setImageResource(img[(s-1)]);
我应该使用:
currentImage--;
currentImage = (currentImage + img.length) % img.length;
is.setImageResource(img[currentImage]);
"is"是Imageswitcher的ID
s 是数组中的图像编号
数组包含 5 images:play 停止下一个上一个暂停
public class MainActivity extends AppCompatActivity {
Button btnnext,btnprev;
ImageSwitcher is;
int s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Integer[] img=new Integer[]{R.drawable.next,R.drawable.play,R.drawable.previous,R.drawable.stop,R.drawable.pause,};
btnnext=(Button)findViewById(R.id.btnnext);
btnprev=(Button)findViewById(R.id.btnprev);
is =(ImageSwitcher) findViewById(R.id.is);
Log.i("log1","1");
is.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView=new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
下面这行有什么问题吗?
imageView.setLayoutParams(new ImageSwitcher.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
is.setImageResource(img[s]);
Log.i("log","4"); return imageView;
}
});
Log.i("log2", "2");
按钮下一个代码:
btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s+1)]);
}
});
Log.i("log3", "3");
按钮以前的代码:
btnprev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s-1)]);
}
});
}
}
应删除此代码:
is.setImageResource(img[s]);
而不是:
is.setImageResource(img[(s+1)]);
我应该使用:
currentImage++;
currentImage = currentImage % img.length;
is.setImageResource(img[currentImage]);
而不是:
is.setImageResource(img[(s-1)]);
我应该使用:
currentImage--;
currentImage = (currentImage + img.length) % img.length;
is.setImageResource(img[currentImage]);