简体   繁体   中英

load multiple images in ViewFlipper from assets folder

I want to load all the images placed into assets subfolder in imageview and then add to ViewFlipper . It worked using drawable resource, but I need to locate the images in assets subfolders. This is a part of my code. The Android Studio don't show any error but the result only show the last image of the image list. When I uncomment the myViewFlipper.addView(imageView) line located into for loop and comment the last myViewFlipper.addView, then applicatios is crash. Sorry for my bad english. Thank you for help.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_layout);


        myViewFlipper = (ViewFlipper) findViewById(R.id.myflipper);
        ImageView imageView = new ImageView(PhotosActivity.this);

        final AssetManager am;
        am=getAssets();
        InputStream inputStream = null ;

        try {

            String[] images = am.list("hotel/vedado");

           for (int i = 0; i < images.length; i++) {


               try {

                   inputStream = am.open("hotel/vedado/vedado"+i+".jpg");
                   Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                   imageView.setImageBitmap(bitmap);
                   // Drawable drawable = Drawable.createFromStream(inputStream, null);
                   //imageView.setImageDrawable(drawable);
                   //myViewFlipper.addView(imageView);

               }
               catch (IOException e) {
                   e.printStackTrace();
               }


           }
           myViewFlipper.addView(imageView);

        }
        catch (IOException e) {
            e.printStackTrace();
        }



}
@Override
public boolean onTouchEvent(MotionEvent event) {
     switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            initialXPoint = event.getX();
            break;
        case MotionEvent.ACTION_UP:
            float finalx = event.getX();
            if (initialXPoint > finalx) {
                if (myViewFlipper.getDisplayedChild() == 4)
                    break;
                myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
                myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
                myViewFlipper.showNext();
            } else {
               // if (myViewFlipper.getDisplayedChild() == 0)
                 //   break;
                myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
                myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
                myViewFlipper.showPrevious();
            }
            break;
    }
    return true;
}
public void to_left(View v) {
    myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
    myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
    myViewFlipper.showNext();
}
public void to_right(View v) {
    myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
    myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
    myViewFlipper.showPrevious();

You are only creating one ImageView . You are then trying to add that ImageView multiple times to the ViewFlipper . This will not work.

If you want multiple images in a ViewFlipper , you either need one ImageView per image, or to switch AdapterViewFlipper (so you can recycle ImageView widgets and save a lot on memory).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM