Java Code Examples for com.hippo.yorozuya.MathUtils#clamp()

The following examples show how to use com.hippo.yorozuya.MathUtils#clamp() . 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 File: GalleryActivity.java    From MHViewer with 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 2
Source File: Slider.java    From EhViewer with 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 3
Source File: FixedAspectImageView.java    From EhViewer with 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 4
Source File: GalleryActivity.java    From EhViewer with 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 5
Source File: Slider.java    From MHViewer with 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 File: FixedAspectImageView.java    From MHViewer with 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 File: ImageDrawable.java    From Nimingban with 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 File: SpiderQueen.java    From EhViewer with 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 9
Source File: FixedAspectImageView.java    From Nimingban with 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 File: Slider.java    From Nimingban with 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 11
Source File: HttpCookieWithId.java    From Nimingban with 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 12
Source File: SimpleRatingView.java    From EhViewer with 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 * 2), 0, 10);
        if (mRatingInt != ratingInt) {
            mRatingInt = ratingInt;
            invalidate();
        }
    }
}
 
Example 13
Source File: TileThumb.java    From EhViewer with 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 14
Source File: PostLayout.java    From Nimingban with 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 15
Source File: SearchBarMover.java    From EhViewer with 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 16
Source File: SearchBarMover.java    From MHViewer with 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 17
Source File: TileThumb.java    From MHViewer with 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 18
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
public static void initialize(Context context) {
    sCache = new SimpleDiskCache(new File(context.getCacheDir(), "image"),
            MathUtils.clamp(Settings.getReadCacheSize(), 40, 640) * 1024 * 1024);
}
 
Example 19
Source File: ProxyPreference.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private String getProxyTypeText(Context context, int type) {
    String[] array = context.getResources().getStringArray(R.array.proxy_types);
    return array[MathUtils.clamp(type, 0, array.length - 1)];
}
 
Example 20
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
public static void initialize(Context context) {
    sCache = new SimpleDiskCache(new File(context.getCacheDir(), "image"),
            MathUtils.clamp(Settings.getReadCacheSize(), 40, 640) * 1024 * 1024);
}