根据Imageviews的高度和宽度在onCreate()中创建缩放位图

时间:2013-02-25 19:11:33

标签: android bitmap imageview

这是问题即时通讯在onCreate()中我想首先将imageview设置为文件列表中的第一个图像,但是使用ImageViews的宽度和高度,当它来回翻转时,它会设置图像到ImageViews维度没有问题,但在onCreate()我得到IllegalArgumentException宽度和高度必须大于0.我试图使启动方法,如果它没有发生在onCreate(),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);
  }

1 个答案:

答案 0 :(得分:3)

只有在onCreate完成后才能为父视图请求布局时,才会计算图像视图的高度和宽度。您应该实现OnGlobalLayoutListener并从那里获取imageview的高度和宽度。但是,您不应该在那里加载图片,使用AsyncTask或其他方法将图片加载到背景中。