1

I am making a slot machine app and using kankan's wheel for the same. I want to modify the library such that when the rotation stops the item it will point shoud be the one that I set . I have done this but there is a glitch that shows that we have changed the actual image to the one that we want . How to achieve this?

Update:

I have researched a lot on this and if I am right , android scroll is based on duration and distance not items . From kankan's wheel library I can get current item .Now , I am trying to stop the animation as well as scroll , as soon as a certain duration has been reached and the item is the one that I want (through index) . But this is not working .Please help!!

GameActivity

public class GameActivity extends Activity {

float mDeviceDensity;
String mUuid, mTitle, mContent, mReward;
ImageButton play;
SlotMachineAdapter slotAdapter;
private List<HashMap<String, Object>> slotImages = new ArrayList<HashMap<String, Object>>();
ArrayList<String> imagesWinId = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_filler_up_game);

    DisplayMetrics display = getResources().getDisplayMetrics();
    mDeviceDensity = display.density;
    slotAdapter = new SlotMachineAdapter(this);
    getPassedData();

    setSoundPlayer(R.raw.clicks,true);
    initWheel(R.id.slot_1, false, 0);
    initWheel(R.id.slot_2, false, 1);
    initWheel(R.id.slot_3, true, 2);

    play = (ImageButton) findViewById(R.id.btn_mix);
    play.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            shuffle(R.id.slot_1, 5000);
            shuffle(R.id.slot_2, 7000);
            shuffle(R.id.slot_3, 9000);

        }
    });

}

protected ImageLoader imageLoader;
ArrayList<SlotItem> arrListSlotItems;

private void getPassedData() {
    try {
        mUuid = getIntent().getStringExtra(getString(R.string.FILLER_UP_UUID));

        imageLoader = ImageLoader.getInstance();

        Uuid slotImagesExtra = (Uuid) (getIntent()
                .getSerializableExtra(getString(R.string.FILLER_UP_IMAGES)));

        arrListSlotItems = slotImagesExtra.getArrSlotItemArray();
        for (int i = 0; i < arrListSlotItems.size(); i++)
            downloadSlotImages(arrListSlotItems.get(i).getSlotId(), arrListSlotItems.get(i).getImageUrl());

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

// Wheel scrolled flag
private boolean wheelScrolled = false;

// Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {

    public void onScrollingStarted(WheelView wheel) {
        wheelScrolled = true;

    }

    public void onScrollingFinished(WheelView wheel) {
        wheelScrolled = false;

        setStatus(wheel.getId(), getWheel(wheel.getId()).getWinningIndex());
    }

};

// Wheel changed listener
private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (!wheelScrolled) {

        }

    }
};

/**
 * Updates status
 */
private void updateStatus() {

    myThread();


}

public void myThread(){
    Thread th=new Thread(){

     @Override
     public void run(){
      try
      {

       Thread.sleep(2000);
       GameActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            showAlertDialogWithSingleButton(GameActivity.this, mTitle, mContent, success);

}
           });

      }catch (InterruptedException e) {
    // TODO: handle exception
   }
}
    };
    th.start();
   }

android.content.DialogInterface.OnClickListener success = new android.content.DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (mContent != null && mContent.contains("again"))
            startHomeActivity();
        else
            startNewsActivity();
    }
};

private void startHomeActivity() {

}

private void startNewsActivity() {

}

android.content.DialogInterface.OnClickListener fail = new android.content.DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

    // 
    }
};

public void showAlertDialogWithSingleButton(final Activity ctx, final String title, final String message,
        DialogInterface.OnClickListener onClickListener) {

    // show dialog 
}

private void initWheel(int id, boolean monitorScroll, int itemIndex) {
    Random randomGenerator = new Random();
    int index = randomGenerator.nextInt(arrListSlotItems.size());

    WheelView wheel = getWheel(id);
    wheel.setViewAdapter(slotAdapter);
    wheel.setCurrentItem((index ));
    wheel.setVisibleItems(1);

    wheel.setWinningIndex(itemIndex);
    wheel.addChangingListener(changedListener);
    wheel.addScrollingListener(scrolledListener);
    wheel.setCyclic(true);

    wheel.setEnabled(false);
}

private WheelView getWheel(int id) {
    return (WheelView) findViewById(id);
}




private void setStatus(int id, int item) {
    int index = 0;
    for (int i = 0; i < arrListSlotItems.size(); i++) {
        SlotItem d = arrListSlotItems.get(i);

        if (d.getSlotId() != 0 && d.getSlotId() == Integer.parseInt(imagesWinId.get(item)))
            index = arrListSlotItems.indexOf(d);

    }


    getWheel(id).setCurrentItem(index, true);


    if (id == R.id.slot_3) {
        if(player.isPlaying())
        {
            stopBackgroundAudio();
        }
    updateStatus();

    }
}

private void shuffle(int id, int duration) {
    WheelView wheel = getWheel(id);
    wheel.scroll(450 + (int) (Math.random() * 50), duration);
}

private class SlotMachineAdapter extends AbstractWheelAdapter {

    final int IMAGE_WIDTH = getImageWidth(mDeviceDensity);
    final int IMAGE_HEIGHT = getImageHeight(mDeviceDensity);

    private Context context;

    /**
     * Constructor
     */
    public SlotMachineAdapter(Context context) {
        this.context = context;

    }

    /**
     * Loads image from resources
     */
    private Bitmap loadImage(Bitmap bitmap) {

        Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true);

        return scaled;
    }

    @Override
    public int getItemsCount() {
        return slotImages.size();
    }

    // Layout params for image view
    final LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);

    @Override
    public View getItem(int index, View cachedView, ViewGroup parent) {
        ImageView img;
        if (cachedView != null) {
            img = (ImageView) cachedView;

        } else {
            img = new ImageView(context);

        }
        img.setPadding(0, 5, 0, 5);
        img.setLayoutParams(params);
        @SuppressWarnings("unchecked")
        SoftReference<Bitmap> bitmapRef = (SoftReference<Bitmap>)   slotImages.get(index).get("image");
        Bitmap bitmap = bitmapRef.get();

        if (bitmap == null) {
            bitmap = loadImage(bitmap);

        }

        img.setImageBitmap(bitmap);

        return img;
    }
}

private int getImageWidth(float density) {

}

private int getImageHeight(float density) {

}

private void downloadSlotImages(final int id, String slotObj) {

    //downloading slot images from server

}
}

This is the code. Through this code, when slot stops I want it to scroll some more untill it reaches the image position that I receaved from server. I can do this .But this is providing a lil glitch . Is there any way to stop scrolling when the image is reached as soon as certain duration is reached.

P.S. If you need anymore detail I can provide you. P.P.S. Screenshots wont give you any detailed insight about the issue.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Payal
  • 903
  • 1
  • 9
  • 28
  • Add screenshots of what is happening at the moment and what do you expect to happen! Also add the related code. – Tushar Gogna Apr 28 '15 at 08:56
  • @UncaughtException added the code .please have a look – Payal Apr 28 '15 at 09:58
  • @yushi added the code .please have a look – Payal Apr 28 '15 at 09:59
  • sorry still not clear to me. –  Apr 28 '15 at 10:30
  • All i want is to rotate the slot for 5 seconds and then stop at a position that is given by me instead of some random position @yushi – Payal Apr 28 '15 at 10:45
  • @Payal Okay! so, can you stop the position of the wheel programmatically ? –  Apr 28 '15 at 10:48
  • yes ..If you see the code , it thers is a listener called OnWheelScrollListener .Here onScrollingFinished I am setting up the position where I want to stop.But the animation appears as if we have deliberately changed the position, which of course we dont wanna show. This is the issue I am facing @yushi – Payal Apr 28 '15 at 10:50
  • can't you stop the animation while calling of onScrollingFinished? –  Apr 28 '15 at 10:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76439/discussion-between-payal-and-yushi). – Payal Apr 28 '15 at 11:03
  • Reading android-wheel library I think you can do something in https://github.com/maarek/android-wheel/blob/master/wheel/src/kankan/wheel/widget/WheelView.java In onJustify callback you can you can do something to calculate time you need and the scrolling offset value according to your winning index. The main point is to use onJustify callback and passing a non-zero value to function scroller.scroll. Line number 174 – Parvaz Bhaskar May 01 '15 at 05:11

1 Answers1

0

After days of searching I finally did it.All I had to do was set interpolater as LinearInterpolater and While setting setCurrentItem set animation as true.

Payal
  • 903
  • 1
  • 9
  • 28
  • how u implemented method setCurrentItem i m using this lib https://github.com/LukeDeighton/WheelView here is my issue how to resolve? https://github.com/LukeDeighton/WheelView/issues/17 – Erum Jun 23 '15 at 21:04