2015-04-20 68 views
1

我正在製作一個老虎機應用程序,並使用kankan的輪相同。我想修改庫,以便當旋轉停止該項目時,它應該指向我設置的那個。我已經這樣做了,但是有一個小故障顯示我們已經將實際圖像更改爲我們想要的圖像。如何實現這一目標?Android 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.截圖不會給你任何關於這個問題的詳細見解。

+0

你的代碼是什麼? –

+0

添加當前正在發生的事件的屏幕截圖,以及您期望發生的事情!還要添加相關的代碼。 –

+0

@UncaughtException添加了代碼。請稍後查看 – Payal

回答

0

經過幾天的搜索,我終於做到了。我所要做的就是將interpolater設置爲LinearInterpolater,並將setCurrentItem設置爲true。

+0

你如何實現方法setCurrentItem我使用這個庫https://github.com/LukeDeighton/WheelView這裏是我的問題如何解決? https://github.com/LukeDeighton/WheelView/issues/17 – Erum