如何确定viewflipper的当前视图?

时间:2015-09-16 10:03:44

标签: android eclipse imageview viewflipper

我有四个图像视图的视图鳍状肢,相机中捕获的图像被设置为每个图像视图。使用showNext()方法查看viewflipper中的所有子节点。我必须获取viewflipper的当前视图并设置当前imageview的壁纸。

下面是我用来获取当前视图并设置为墙纸的代码。

package com.example.websamples;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Photo extends Activity implements OnClickListener {
    ViewFlipper vfImageViews;
    ImageView ivCapture1, ivCapture2, ivCapture3, ivCapture4;
    ImageButton ibCamera;
    Button bSetWall;
    final int cameraData = 0;
    Bitmap bmpImageView0, bmpImageView1, bmpImageView2, bmpImageView3;
    int iSetWallpaper;
    int iStatus = 1;
    ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        ivCapture1 = (ImageView) findViewById(R.id.ivCapture1);
        ivCapture2 = (ImageView) findViewById(R.id.ivCapture2);
        ivCapture3 = (ImageView) findViewById(R.id.ivCapture3);
        ivCapture4 = (ImageView) findViewById(R.id.ivCapture4);
        ibCamera = (ImageButton) findViewById(R.id.ibCamera);
        bSetWall = (Button) findViewById(R.id.bSetWall);
        vfImageViews = (ViewFlipper) findViewById(R.id.vfImageViews);
        ibCamera.setOnClickListener(this);
        bSetWall.setOnClickListener(this);
        vfImageViews.setOnClickListener(this);
        // vfImageViews.setFlipInterval(5000);
        // vfImageViews.startFlipping();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.vfImageViews:
            vfImageViews.showNext();
            break;
        case R.id.ibCamera:
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, cameraData);
            break;
        case R.id.bSetWall:
            try {
                iSetWallpaper = vfImageViews.indexOfChild(vfImageViews
                        .getCurrentView());
                // WallpaperManager myWallpaperManager =
                // WallpaperManager.getInstance(getApplicationContext());
                // myWallpaperManager.setResource(iSetWallpaper);
                getApplicationContext().setWallpaper(
                        bitmapArray.get(iSetWallpaper));
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == cameraData) {
                Bundle extras = data.getExtras();
                if (iStatus == 1) {
                    bmpImageView0 = (Bitmap) extras.get("data");
                    bitmapArray.add(bmpImageView0);
                    ivCapture1.setImageBitmap(bmpImageView0);
                    vfImageViews.setDisplayedChild(vfImageViews
                            .indexOfChild(findViewById(R.id.ivCapture1)));
                    iStatus++;
                } else if (iStatus == 2) {
                    bmpImageView1 = (Bitmap) extras.get("data");
                    bitmapArray.add(bmpImageView1);
                    ivCapture2.setImageBitmap(bmpImageView1);
                    vfImageViews.setDisplayedChild(vfImageViews
                            .indexOfChild(findViewById(R.id.ivCapture2)));
                    iStatus++;
                } else if (iStatus == 3) {
                    bmpImageView2 = (Bitmap) extras.get("data");
                    bitmapArray.add(bmpImageView2);
                    ivCapture3.setImageBitmap(bmpImageView2);
                    vfImageViews.setDisplayedChild(vfImageViews
                            .indexOfChild(findViewById(R.id.ivCapture3)));
                    iStatus++;
                } else if (iStatus == 4) {
                    bmpImageView3 = (Bitmap) extras.get("data");
                    bitmapArray.add(bmpImageView3);
                    ivCapture4.setImageBitmap(bmpImageView3);
                    vfImageViews.setDisplayedChild(vfImageViews
                            .indexOfChild(findViewById(R.id.ivCapture4)));
                    iStatus = 1;
                }
            }
        }
    }
}

我的申请人在此停止                     iSetWallpaper = vfImageViews.indexOfChild(vfImageViews.getCurrentView());

请更正代码出错的位置。

1 个答案:

答案 0 :(得分:2)

您可以使用此

设置子值
flipper.setDisplayedChild(8);

您可以检索值

flipper.getDisplayedChild();