Java Code Examples for com.hippo.yorozuya.MathUtils

The following examples show how to use com.hippo.yorozuya.MathUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: MHViewer   Source File: SpiderQueen.java    License: Apache License 2.0 6 votes vote down vote up
private SpiderQueen(EhApplication application, @NonNull GalleryInfo galleryInfo) {
    mHttpClient = EhApplication.getOkHttpClient(application);
    mSpiderInfoCache = EhApplication.getSpiderInfoCache(application);
    mGalleryInfo = galleryInfo;
    mSpiderDen = new SpiderDen(mGalleryInfo);

    mWorkerMaxCount = MathUtils.clamp(Settings.getMultiThreadDownload(), 1, 10);
    mPreloadNumber = MathUtils.clamp(Settings.getPreloadImage(), 0, 100);

    for (int i = 0; i < DECODE_THREAD_NUM; i++) {
        mDecodeIndexArray[i] = GalleryPageView.INVALID_INDEX;
    }

    mWorkerPoolExecutor = new ThreadPoolExecutor(mWorkerMaxCount, mWorkerMaxCount,
            0, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(),
            new PriorityThreadFactory(SpiderWorker.class.getSimpleName(), Process.THREAD_PRIORITY_BACKGROUND));
}
 
Example 2
Source Project: MHViewer   Source File: GalleryActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param lightness 0 - 200
 */
private void setScreenLightness(boolean enable, int lightness) {
    if (null == mMaskView) {
        return;
    }

    Window w = getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    if (enable) {
        lightness = MathUtils.clamp(lightness, 0, 200);
        if (lightness > 100) {
            mMaskView.setColor(0);
            // Avoid BRIGHTNESS_OVERRIDE_OFF,
            // screen may be off when lp.screenBrightness is 0.0f
            lp.screenBrightness = Math.max((lightness - 100) / 100.0f, 0.01f);
        } else {
            mMaskView.setColor(MathUtils.lerp(0xde, 0x00, lightness / 100.0f) << 24);
            lp.screenBrightness = 0.01f;
        }
    } else {
        mMaskView.setColor(0);
        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    }
    w.setAttributes(lp);
}
 
Example 3
Source Project: MHViewer   Source File: AddDeleteDrawable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    float canvasRotate;
    if (mVerticalMirror) {
        canvasRotate = MathUtils.lerp(270, 135f, mProgress);
    } else {
        canvasRotate = MathUtils.lerp(0f, 135f, mProgress);
    }

    canvas.save();
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.rotate(canvasRotate);


    canvas.drawPath(mPath, mPaint);
    canvas.restore();
}
 
Example 4
Source Project: MHViewer   Source File: PreciselyClipDrawable.java    License: Apache License 2.0 6 votes vote down vote up
public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) {
    super(drawable);
    float originWidth = drawable.getIntrinsicWidth();
    float originHeight = drawable.getIntrinsicHeight();

    if (originWidth <= 0 || originHeight <= 0) {
        // Can not clip
        mClip = false;
    } else {
        mClip = true;
        mScale = new RectF();
        mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
                MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
                MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
                MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f));
        mTemp = new Rect();
    }
}
 
Example 5
Source Project: MHViewer   Source File: Slider.java    License: Apache License 2.0 6 votes vote down vote up
public void setProgress(int progress) {
    progress = MathUtils.clamp(progress, mStart, mEnd);
    int oldProgress = mProgress;
    if (mProgress != progress) {
        mProgress = progress;
        mPercent = MathUtils.delerp(mStart, mEnd, mProgress);
        mTargetProgress = progress;

        if (mProgressAnimation == null) {
            // For init
            mDrawPercent = mPercent;
            mDrawProgress = mProgress;
            updateBubblePosition();
            mBubble.setProgress(mDrawProgress);
        } else {
            startProgressAnimation(mPercent);
        }
        invalidate();
    }
    if (mListener != null) {
        mListener.onSetProgress(this, progress, oldProgress, false, true);
    }
}
 
Example 6
Source Project: MHViewer   Source File: FixedAspectImageView.java    License: Apache License 2.0 6 votes vote down vote up
private int resolveAdjustedSize(int desiredSize, int minSize, int maxSize,
        int measureSpec) {
    int result = desiredSize;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize =  MeasureSpec.getSize(measureSpec);
    switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            // Parent says we can be as big as we want. Just don't be smaller
            // than min size, and don't be larger than max size.
            result = MathUtils.clamp(desiredSize, minSize, maxSize);
            break;
        case MeasureSpec.AT_MOST:
            // Parent says we can be as big as we want, up to specSize.
            // Don't be larger than specSize, and don't be smaller
            // than min size, and don't be larger than max size.
            result = Math.min(MathUtils.clamp(desiredSize, minSize, maxSize), specSize);
            break;
        case MeasureSpec.EXACTLY:
            // No choice. Do what we are told.
            result = specSize;
            break;
    }
    return result;
}
 
Example 7
Source Project: Nimingban   Source File: ImageDrawable.java    License: Apache License 2.0 6 votes vote down vote up
private List<Tile> createTileArray() {
    int width = mImageWrapper.getWidth();
    int height = mImageWrapper.getHeight();
    int capacity = MathUtils.clamp(MathUtils.ceilDivide(width, TILE_SIZE) *
            MathUtils.ceilDivide(height, TILE_SIZE), 0, 100);
    List<Tile> tiles = new ArrayList<>(capacity);

    for (int x = 0; x < width; x += TILE_SIZE) {
        int w = Math.min(TILE_SIZE, width - x);
        for (int y = 0; y < height; y += TILE_SIZE) {
            int h = Math.min(TILE_SIZE, height - y);
            Tile tile = new Tile();
            tile.x = x;
            tile.y = y;
            tile.w = w;
            tile.h = h;
            tile.bitmap = sBitmapPool.get(w, h);
            tiles.add(tile);
        }
    }

    return tiles;
}
 
Example 8
Source Project: Nimingban   Source File: Slider.java    License: Apache License 2.0 6 votes vote down vote up
public void setProgress(int progress) {
    progress = MathUtils.clamp(progress, mStart, mEnd);
    int oldProgress = mProgress;
    if (mProgress != progress) {
        mProgress = progress;
        mPercent = MathUtils.delerp(mStart, mEnd, mProgress);
        mTargetProgress = progress;

        if (mProgressAnimation == null) {
            // For init
            mDrawPercent = mPercent;
            mDrawProgress = mProgress;
            updateBubblePosition();
            mBubble.setProgress(mDrawProgress);
        } else {
            startProgressAnimation(mPercent);
        }
        invalidate();
    }
    if (mListener != null) {
        mListener.onSetProgress(this, progress, oldProgress, false, true);
    }
}
 
Example 9
Source Project: Nimingban   Source File: FixedAspectImageView.java    License: Apache License 2.0 6 votes vote down vote up
private int resolveAdjustedSize(int desiredSize, int minSize, int maxSize,
        int measureSpec) {
    int result = desiredSize;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize =  MeasureSpec.getSize(measureSpec);
    switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            // Parent says we can be as big as we want. Just don't be smaller
            // than min size, and don't be larger than max size.
            result = MathUtils.clamp(desiredSize, minSize, maxSize);
            break;
        case MeasureSpec.AT_MOST:
            // Parent says we can be as big as we want, up to specSize.
            // Don't be larger than specSize, and don't be smaller
            // than min size, and don't be larger than max size.
            result = Math.min(MathUtils.clamp(desiredSize, minSize, maxSize), specSize);
            break;
        case MeasureSpec.EXACTLY:
            // No choice. Do what we are told.
            result = specSize;
            break;
    }
    return result;
}
 
Example 10
Source Project: EhViewer   Source File: SpiderQueen.java    License: Apache License 2.0 6 votes vote down vote up
private SpiderQueen(EhApplication application, @NonNull GalleryInfo galleryInfo) {
    mHttpClient = EhApplication.getOkHttpClient(application);
    mSpiderInfoCache = EhApplication.getSpiderInfoCache(application);
    mGalleryInfo = galleryInfo;
    mSpiderDen = new SpiderDen(mGalleryInfo);

    mWorkerMaxCount = MathUtils.clamp(Settings.getMultiThreadDownload(), 1, 10);
    mPreloadNumber = MathUtils.clamp(Settings.getPreloadImage(), 0, 100);

    for (int i = 0; i < DECODE_THREAD_NUM; i++) {
        mDecodeIndexArray[i] = GalleryPageView.INVALID_INDEX;
    }

    mWorkerPoolExecutor = new ThreadPoolExecutor(mWorkerMaxCount, mWorkerMaxCount,
            0, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(),
            new PriorityThreadFactory(SpiderWorker.class.getSimpleName(), Process.THREAD_PRIORITY_BACKGROUND));
}
 
Example 11
Source Project: EhViewer   Source File: GalleryActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param lightness 0 - 200
 */
private void setScreenLightness(boolean enable, int lightness) {
    if (null == mMaskView) {
        return;
    }

    Window w = getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    if (enable) {
        lightness = MathUtils.clamp(lightness, 0, 200);
        if (lightness > 100) {
            mMaskView.setColor(0);
            // Avoid BRIGHTNESS_OVERRIDE_OFF,
            // screen may be off when lp.screenBrightness is 0.0f
            lp.screenBrightness = Math.max((lightness - 100) / 100.0f, 0.01f);
        } else {
            mMaskView.setColor(MathUtils.lerp(0xde, 0x00, lightness / 100.0f) << 24);
            lp.screenBrightness = 0.01f;
        }
    } else {
        mMaskView.setColor(0);
        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    }
    w.setAttributes(lp);
}
 
Example 12
Source Project: EhViewer   Source File: AddDeleteDrawable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();
    float canvasRotate;
    if (mVerticalMirror) {
        canvasRotate = MathUtils.lerp(270, 135f, mProgress);
    } else {
        canvasRotate = MathUtils.lerp(0f, 135f, mProgress);
    }

    canvas.save();
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.rotate(canvasRotate);
    canvas.drawPath(mPath, mPaint);
    canvas.restore();
}
 
Example 13
Source Project: EhViewer   Source File: PreciselyClipDrawable.java    License: Apache License 2.0 6 votes vote down vote up
public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) {
    super(drawable);
    float originWidth = drawable.getIntrinsicWidth();
    float originHeight = drawable.getIntrinsicHeight();

    if (originWidth <= 0 || originHeight <= 0) {
        // Can not clip
        mClip = false;
    } else {
        mClip = true;
        mScale = new RectF();
        mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
                MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
                MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
                MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f));
        mTemp = new Rect();
    }
}
 
Example 14
Source Project: EhViewer   Source File: Slider.java    License: Apache License 2.0 6 votes vote down vote up
public void setProgress(int progress) {
    progress = MathUtils.clamp(progress, mStart, mEnd);
    int oldProgress = mProgress;
    if (mProgress != progress) {
        mProgress = progress;
        mPercent = MathUtils.delerp(mStart, mEnd, mProgress);
        mTargetProgress = progress;

        if (mProgressAnimation == null) {
            // For init
            mDrawPercent = mPercent;
            mDrawProgress = mProgress;
            updateBubblePosition();
            mBubble.setProgress(mDrawProgress);
        } else {
            startProgressAnimation(mPercent);
        }
        invalidate();
    }
    if (mListener != null) {
        mListener.onSetProgress(this, progress, oldProgress, false, true);
    }
}
 
Example 15
Source Project: EhViewer   Source File: FixedAspectImageView.java    License: Apache License 2.0 6 votes vote down vote up
private int resolveAdjustedSize(int desiredSize, int minSize, int maxSize,
        int measureSpec) {
    int result = desiredSize;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize =  MeasureSpec.getSize(measureSpec);
    switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            // Parent says we can be as big as we want. Just don't be smaller
            // than min size, and don't be larger than max size.
            result = MathUtils.clamp(desiredSize, minSize, maxSize);
            break;
        case MeasureSpec.AT_MOST:
            // Parent says we can be as big as we want, up to specSize.
            // Don't be larger than specSize, and don't be smaller
            // than min size, and don't be larger than max size.
            result = Math.min(MathUtils.clamp(desiredSize, minSize, maxSize), specSize);
            break;
        case MeasureSpec.EXACTLY:
            // No choice. Do what we are told.
            result = specSize;
            break;
    }
    return result;
}
 
Example 16
Source Project: MHViewer   Source File: GalleryListScene.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getSuitableTitleForUrlBuilder(
        Resources resources, ListUrlBuilder urlBuilder, boolean appName) {
    String keyword = urlBuilder.getKeyword();
    int category = urlBuilder.getCategory();

    if (ListUrlBuilder.MODE_NORMAL == urlBuilder.getMode() &&
            EhUtils.NONE == category &&
            TextUtils.isEmpty(keyword) &&
            urlBuilder.getAdvanceSearch() == -1 &&
            urlBuilder.getMinRating() == -1 &&
            urlBuilder.getPageFrom() == -1 &&
            urlBuilder.getPageTo() == -1) {
        return resources.getString(appName ? R.string.app_name : R.string.homepage);
    } else if (ListUrlBuilder.MODE_SUBSCRIPTION == urlBuilder.getMode() &&
            EhUtils.NONE == category &&
            TextUtils.isEmpty(keyword) &&
            urlBuilder.getAdvanceSearch() == -1 &&
            urlBuilder.getMinRating() == -1 &&
            urlBuilder.getPageFrom() == -1 &&
            urlBuilder.getPageTo() == -1) {
        return resources.getString(R.string.subscription);
    } else if (MODE_WHATS_HOT == urlBuilder.getMode()) {
        return resources.getString(R.string.whats_hot);
    } else if (!TextUtils.isEmpty(keyword)) {
        return keyword;
    } else if (MathUtils.hammingWeight(category) == 1) {
        return EhUtils.getCategory(category);
    } else {
        return null;
    }
}
 
Example 17
Source Project: MHViewer   Source File: SearchBar.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
    if (mInAnimation) {
        final int state = canvas.save();
        int bottom = MathUtils.lerp(mBaseHeight, mHeight, mProgress);
        mRect.set(0, 0, mWidth, bottom);
        canvas.clipRect(mRect);
        super.draw(canvas);
        canvas.restoreToCount(state);
    } else {
        super.draw(canvas);
    }
}
 
Example 18
Source Project: MHViewer   Source File: TileThumb.java    License: Apache License 2.0 5 votes vote down vote up
public void setThumbSize(int thumbWidth, int thumbHeight) {
    float aspect;
    if (thumbWidth > 0 && thumbHeight > 0) {
        aspect = MathUtils.clamp(thumbWidth / (float) thumbHeight, MIN_ASPECT, MAX_ASPECT);
    } else {
        aspect = DEFAULT_ASPECT;
    }
    setAspect(aspect);
}
 
Example 19
Source Project: MHViewer   Source File: SimpleRatingView.java    License: Apache License 2.0 5 votes vote down vote up
public void setRating(float rating) {
    if (mRating != rating) {
        mRating = rating;
        int ratingInt = MathUtils.clamp((int) Math.ceil(rating), 0, 10);
        if (mRatingInt != ratingInt) {
            mRatingInt = ratingInt;
            invalidate();
        }
    }
}
 
Example 20
Source Project: MHViewer   Source File: Settings.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
private static String generateUserID() {
    int length = LENGTH_USER_ID;
    StringBuilder sb = new StringBuilder(length);

    for (int i = 0; i < length; i++) {
        if (MathUtils.random(0, ('9' - '0' + 1) + ('z' - 'a' + 1)) <= '9' - '0') {
            sb.append((char) MathUtils.random('0', '9' + 1));
        } else {
            sb.append((char) MathUtils.random('a', 'z' + 1));
        }
    }

    return sb.toString();
}
 
Example 21
Source Project: MHViewer   Source File: BatteryDrawable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * How to draw:<br>
 * |------------------------------|<br>
 * |\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|<br>
 * |------------------------------|---|<br>
 * |/////////////////|         |//|\\\|<br>
 * |/////////////////|         |//|\\\|<br>
 * |------------------------------|---|<br>
 * |\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|<br>
 * |------------------------------|
 */
@Override
public void draw(Canvas canvas) {
    if (mElect == -1) {
        return;
    }

    mElectRect.right = MathUtils.lerp(mStart, mStop, mElect / 100.0f);

    canvas.drawRect(mTopRect, mPaint);
    canvas.drawRect(mBottomRect, mPaint);
    canvas.drawRect(mRightRect, mPaint);
    canvas.drawRect(mHeadRect, mPaint);
    canvas.drawRect(mElectRect, mPaint);
}
 
Example 22
Source Project: MHViewer   Source File: SearchBarMover.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (mHelper.isValidView(recyclerView)) {
        int oldBottom = (int) ViewUtils.getY2(mSearchBar);
        int offsetYStep = MathUtils.clamp(-dy, -oldBottom, -(int) mSearchBar.getTranslationY());
        if (offsetYStep != 0) {
            ViewUtils.translationYBy(mSearchBar, offsetYStep);
        }
    }
}
 
Example 23
Source Project: MHViewer   Source File: CheckTextView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void drawableHotspotChanged(float x, float y) {
    super.drawableHotspotChanged(x, y);

    mX = x;
    mY = y;
    mMaxRadius = MathUtils.coverageRadius(getWidth(), getHeight(), x, y);
}
 
Example 24
Source Project: Nimingban   Source File: HttpCookieWithId.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return 0 - Long.MAX_VALUE for the actual max age, -1 for eternal life
 */
public long getMaxAge() {
    if (mMaxAge == -1l) {
        return -1l;
    } else {
        return MathUtils.clamp(mMaxAge - ((System.currentTimeMillis() - mWhenCreated) / 1000), 0l, Long.MAX_VALUE);
    }
}
 
Example 25
Source Project: Nimingban   Source File: Site.java    License: Apache License 2.0 5 votes vote down vote up
public static Site fromId(int id) {
    switch (id) {
        case RANDOM:
            return fromId(MathUtils.random(SITE_MIN, SITE_MAX + 1));
        case AC:
            return ACSite.getInstance();
        default:
            throw new IllegalStateException("Unknown site " + id);
    }
}
 
Example 26
Source Project: Nimingban   Source File: ACSite.java    License: Apache License 2.0 5 votes vote down vote up
private ACCdnPath getCdnPath() {
    final float r = MathUtils.random(mRateSum);
    float sum = 0.0f;
    List<ACCdnPath> list = mCdnPathList;
    ACCdnPath cdnPath = null;
    for (int i = 0, size = list.size(); i < size; i++) {
        cdnPath = list.get(i);
        sum += cdnPath.rate;
        if (r <= sum) {
            return cdnPath;
        }
    }
    return cdnPath;
}
 
Example 27
Source Project: Nimingban   Source File: Settings.java    License: Apache License 2.0 5 votes vote down vote up
public static @NonNull String getRandomFeedId() {
    int length = 20;
    StringBuilder sb = new StringBuilder(length);

    for (int i = 0; i < length; i++) {
        if (MathUtils.random(0, 1 + 1) == 0) {
            sb.append((char) MathUtils.random('a', 'z' + 1));
        } else {
            sb.append((char) MathUtils.random('0', '9' + 1));
        }
    }

    return sb.toString();
}
 
Example 28
Source Project: Nimingban   Source File: PostLayout.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
    if (child.getId() == R.id.type_send) {
        View view = child.findViewById(R.id.toolbar);
        if (view != null) {
            return MathUtils.clamp(top, 0, getHeight() - view.getHeight());
        }
    }
    return top;
}
 
Example 29
Source Project: EhViewer   Source File: GalleryListScene.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getSuitableTitleForUrlBuilder(
        Resources resources, ListUrlBuilder urlBuilder, boolean appName) {
    String keyword = urlBuilder.getKeyword();
    int category = urlBuilder.getCategory();

    if (ListUrlBuilder.MODE_NORMAL == urlBuilder.getMode() &&
            EhUtils.NONE == category &&
            TextUtils.isEmpty(keyword) &&
            urlBuilder.getAdvanceSearch() == -1 &&
            urlBuilder.getMinRating() == -1 &&
            urlBuilder.getPageFrom() == -1 &&
            urlBuilder.getPageTo() == -1) {
        return resources.getString(appName ? R.string.app_name : R.string.homepage);
    } else if (ListUrlBuilder.MODE_SUBSCRIPTION == urlBuilder.getMode() &&
        EhUtils.NONE == category &&
        TextUtils.isEmpty(keyword) &&
        urlBuilder.getAdvanceSearch() == -1 &&
        urlBuilder.getMinRating() == -1 &&
        urlBuilder.getPageFrom() == -1 &&
        urlBuilder.getPageTo() == -1) {
        return resources.getString(R.string.subscription);
    } else if (ListUrlBuilder.MODE_WHATS_HOT == urlBuilder.getMode()) {
        return resources.getString(R.string.whats_hot);
    } else if (!TextUtils.isEmpty(keyword)) {
        return keyword;
    } else if (MathUtils.hammingWeight(category) == 1) {
        return EhUtils.getCategory(category);
    } else {
        return null;
    }
}
 
Example 30
Source Project: EhViewer   Source File: SearchBar.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
    if (mInAnimation) {
        final int state = canvas.save();
        int bottom = MathUtils.lerp(mBaseHeight, mHeight, mProgress);
        mRect.set(0, 0, mWidth, bottom);
        canvas.clipRect(mRect);
        super.draw(canvas);
        canvas.restoreToCount(state);
    } else {
        super.draw(canvas);
    }
}