如何在ViewFlipper中添加多个位图

时间:2016-09-14 06:08:12

标签: android database bitmap slideshow viewflipper

请帮助,我有多个在数据库中检索的图像路径。此图像在Bitmap中转换。我在ViewFlipper中添加了这些图片,使其看起来像幻灯片,但下一张图片没有出现。你能告诉我我做错了吗?这是我的代码:

ViewFlipper flip;

DatabaseHandler db;
List<GettersSetters> dbList;

int index =0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flip);

    Intent goFlip = getIntent();
    Bundle bundle = goFlip.getExtras();
    String getLocation = bundle.getString("path");

    db = new DatabaseHandler(this);
    dbList = new ArrayList<GettersSetters>();
    dbList = db.searchFromDB(getLocation);

    flip = (ViewFlipper) findViewById(R.id.flipper);

    flip.setInAnimation(this, R.anim.right_enter);
    flip.setOutAnimation(this, R.anim.left_out);

    String locate = "";

    if(dbList.size() > 0 ){

        for(GettersSetters currentClass : dbList){
            ImageView images = new ImageView(getApplicationContext());

            //This gets the image path in the database based on the index of recyclerview
            locate +=dbList.get(index++).getPath();

            //This converts the image path into Bitmap
            Bitmap img = decodeBitmapWithSize(locate,300,300,true);

            images.setImageBitmap(img);
            flip.addView(images);
            flip.setFlipInterval(3000);
            flip.startFlipping();
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,而不是您在 for循环中编写的代码:

Drawable drawable = new BitmapDrawable(getResources(), img);
images.setBackground(drawable);

它正在为我工​​作。