Reputation: 21
I have view flipper with four imageview, the images captured in camera is set to each imageview. Used the showNext() method to view all the child in viewflipper. I have to get the currentview of the viewflipper and set the wallpaper of the current imageview displayed.
below is the code I have used to get the current view and set as wallpaper.
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;
}
}
}
}
}
My application force stops here iSetWallpaper = vfImageViews.indexOfChild(vfImageViews.getCurrentView());
Please correct where the code went wrong.
Upvotes: 2
Views: 2087
Reputation: 1977
You can set child Values using this
flipper.setDisplayedChild(8);
and you can retrieve value
flipper.getDisplayedChild();
Upvotes: 4