/** * This method is called by SDL using JNI. * * @return result of getSystemService(name) but executed on UI thread. */ public Object getSystemServiceFromUiThread(final String name) { final Object lock = new Object(); final Object[] results = new Object[2]; // array for writable variables synchronized (lock) { runOnUiThread( new Runnable() { @Override public void run() { synchronized (lock) { results[0] = getSystemService(name); results[1] = Boolean.TRUE; lock.notify(); } } }); if (results[1] == null) { try { lock.wait(); } catch (InterruptedException ex) { ex.printStackTrace(); } } } return results[0]; }
@Override public void run() { isInertia = true; boolean isEnable = false; while (isInertia) { isScrollEnd = false; if (xVelocity < 0) { acceler = -200; } else { acceler = 200; } try { int space = (xVelocity - acceler) / 20; if (acceler < 0 && space >= 0) { isInertia = false; } if (acceler > 0 && space <= 0) { isInertia = false; } if (axesData.size() > 1 && (Math.abs(space) > 2)) { int max = axesData.get(axesData.size() - 1).X; int min = axesData.get(0).X; int maxLimit = width - rightPadding - 20; int minLimit = leftPadding + yTextWidth + 20; if (min + space > minLimit) { // 滑动以后左边界判断 if (min < minLimit) { space = minLimit - min; isEnable = true; } else { space = 0; } } else { isEnable = true; } if (max + space < maxLimit) { // 滑动后右边界判断 if (max > maxLimit) { space = maxLimit - max; isEnable = true; } else { space = 0; } } else { isEnable = true; } if (isEnable) { refreshChart(space); } } xVelocity = xVelocity - acceler * 2; sleep(timeInterval); isScrollEnd = true; } catch (InterruptedException e) { e.printStackTrace(); } } }
private void downloadImageThread() { Thread thread = new Thread(null, runInBackground, "Background"); thread.start(); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }
/** * This method is called by SDL using JNI. Shows the messagebox from UI thread and block calling * thread. buttonFlags, buttonIds and buttonTexts must have same length. * * @param buttonFlags array containing flags for every button. * @param buttonIds array containing id for every button. * @param buttonTexts array containing text for every button. * @param colors null for default or array of length 5 containing colors. * @return button id or -1. */ public int messageboxShowMessageBox( final int flags, final String title, final String message, final int[] buttonFlags, final int[] buttonIds, final String[] buttonTexts, final int[] colors) { messageboxSelection[0] = -1; // sanity checks if ((buttonFlags.length != buttonIds.length) && (buttonIds.length != buttonTexts.length)) { return -1; // implementation broken } // collect arguments for Dialog final Bundle args = new Bundle(); args.putInt("flags", flags); args.putString("title", title); args.putString("message", message); args.putIntArray("buttonFlags", buttonFlags); args.putIntArray("buttonIds", buttonIds); args.putStringArray("buttonTexts", buttonTexts); args.putIntArray("colors", colors); // trigger Dialog creation on UI thread runOnUiThread( new Runnable() { @Override public void run() { showDialog(dialogs++, args); } }); // block the calling thread synchronized (messageboxSelection) { try { messageboxSelection.wait(); } catch (InterruptedException ex) { ex.printStackTrace(); return -1; } } // return selected value return messageboxSelection[0]; }
/** 线程运行的方法,当线程start后执行 */ @Override public void run() { while (flag) { mDraw(); // 调用自定义的绘图方法 mGameLogic(); // 调用自定义的逻辑方法 try { Thread.sleep(50); // 让线程休息50毫秒 } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override public void run() { isInertia = true; boolean isEnable = false; if (velocity != 0) { mDuration = getSplineFlingDuration(velocity); // 不断循环直到惯性时间到达或者用户点击屏幕中断滑动 while (mDuration >= spendTime && isInertia) { // 每时间间隔内惯性滑动的距离 int space = (int) (velocity + (2 * spendTime / timeInterval + 1) * -velocity / mDuration * timeInterval / 2) * timeInterval / 1000; try { if (axesData.size() > 1) { int max = axesData.get(axesData.size() - 1).X; int min = axesData.get(0).X; int maxLimit = width - rightPadding - 20; int minLimit = leftPadding + yTextWidth + 20; if (min + space > minLimit) { // 滑动以后左边界判断 if (min < minLimit) { isEnable = true; } else { space = 0; refreshChart(space); return; } } else { isEnable = true; } if (max + space < maxLimit) { // 滑动后右边界判断 if (max > maxLimit) { isEnable = true; } else { space = 0; refreshChart(space); return; } } else { isEnable = true; } if (isEnable) { refreshChart(space); } } else { return; } spendTime += timeInterval; sleep(timeInterval); isScrollEnd = true; } catch (InterruptedException e) { e.printStackTrace(); } } } }