Android kankan-wheel修改

时间:2015-04-20 13:29:13

标签: android android-wheel

我正在制作老虎机应用程序并使用kankan的方向盘。我想修改库,这样当旋转停止项目时,它将指向我设置的那个。我已经这样做但是有一个小故障表明我们已经将实际图像更改为我们想要的图像。怎么做到这一点?

更新

我已经研究了很多,如果我是对的,那么android滚动是基于持续时间和距离而不是项目。从kankan的轮库我可以获得当前项目。现在,我试图停止动画以及滚动,只要达到一定的持续时间并且该项目是我想要的项目(通过索引)。但这不起作用。请帮助!!

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

}
}

这是代码。通过这段代码,当插槽停止时我想让它滚动一些,直到它到达我从服务器上回收的图像位置。我可以做到这一点。但这提供了一个小故障。有没有办法在达到特定持续时间后立即停止滚动。

P.S。如果您需要更多详细信息,我可以为您提供。 P.P.S.截图不会给你任何关于这个问题的详细见解。

1 个答案:

答案 0 :(得分:0)

经过几天的搜索,我终于做到了。我所要做的就是将插值器设置为LinearInterpolater,并将setCurrentItem设置为动画。