这是我遇到的问题,在 onCreate() 中,我想最初将 imageview 设置为文件列表中的第一个图像,但使用 ImageViews 的宽度和高度,当它来回翻转时,它将图像设置为 ImageViews尺寸没问题,但在 onCreate() 中我得到 IllegalArgumentException 宽度和高度必须大于 0。如果它没有在 onCreate() 中发生,我尝试创建 start 方法,idk,非常感谢任何帮助,谢谢您的时间
public class ViewFlipperActivity extends Activity {
ViewFlipper page;
Animation animFlipInForeward;
Animation animFlipOutForeward;
Animation animFlipInBackward;
Animation animFlipOutBackward;
String[] imagefiles;
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "how");
int filescount,nowcount;
ImageView image;
Matrix matrix;
Bitmap d;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagefiles = file.list();
filescount = imagefiles.length;
nowcount = 0;
matrix = new Matrix();
matrix.postRotate(90);
page = (ViewFlipper)findViewById(R.id.flipper);
image = (ImageView)findViewById(R.id.zero);
animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.left_in);
animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.left_out);
animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.right_in);
animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.right_out);
start();
}
private void start(){
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);
d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();
}
private void SwipeRight(){
page.setInAnimation(animFlipInBackward);
page.setOutAnimation(animFlipOutBackward);
nowcount--;
if(nowcount < 0)
nowcount = filescount - 1;
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);
d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();
page.showPrevious();
Log.d("show previous", "exe");
}
private void SwipeLeft(){
page.setInAnimation(animFlipInForeward);
page.setOutAnimation(animFlipOutForeward);
nowcount++;
if(nowcount > 3)
nowcount = 0;
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);
d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();
page.showNext();
Log.d("show next", "exe");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener
= new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float sensitvity = 50;
if((e1.getX() - e2.getX()) > sensitvity){
SwipeLeft();
}else if((e2.getX() - e1.getX()) > sensitvity){
SwipeRight();
}
return true;
}
};
GestureDetector gestureDetector
= new GestureDetector(simpleOnGestureListener);
}