JRowan
JRowan

Reputation: 7104

creating a scaled bitmap in onCreate() according to the Imageviews height and width

Here is the problem im having, in the onCreate() i would like to initially set the imageview to the first image in the list of files but with the ImageViews width and height, when it flips back and forth it sets the image to the ImageViews dimentions no problem but in the onCreate() i get IllegalArgumentException width and height must be greater than 0. i tried to make the start method if as it wasnt happening in the onCreate(), idk, any help is greatly appreciated, thank you for your time

    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);
  }

Upvotes: 3

Views: 1441

Answers (1)

ebarrenechea
ebarrenechea

Reputation: 3785

The height and width of the image view will only be calculated when a layout is requested for the parent view, which is after onCreate has finished. You should implement an OnGlobalLayoutListener and get the imageview's height and width from there. You shouldn't load your image in there, though, use an AsyncTask or some other approach to load it on the background.

Upvotes: 3

Related Questions