Search in sources :

Example 31 with Paint

use of android.graphics.Paint in project TwinklingRefreshLayout by lcodecorex.

the class WaveView method init.

private void init() {
    path = new Path();
    paint = new Paint();
    paint.setColor(0xff1F2426);
    paint.setAntiAlias(true);
}
Also used : Path(android.graphics.Path) Paint(android.graphics.Paint)

Example 32 with Paint

use of android.graphics.Paint in project ion by koush.

the class IonDrawable method drawDeepZoom.

private void drawDeepZoom(Canvas canvas) {
    // zoom 0: entire image fits in a TILE_DIMxTILE_DIM square
    // draw base bitmap for empty tiles
    // figure out zoom level
    // figure out which tiles need rendering
    // draw stuff that needs drawing
    // missing tile? fetch it
    // use parent level tiles for tiles that do not exist
    // TODO: crossfading?
    Rect clip = canvas.getClipBounds();
    Rect bounds = getBounds();
    float zoom = (float) canvas.getWidth() / (float) clip.width();
    float zoomWidth = zoom * bounds.width();
    float zoomHeight = zoom * bounds.height();
    double wlevel = Math.log(zoomWidth / TILE_DIM) / LOG_2;
    double hlevel = Math.log(zoomHeight / TILE_DIM) / LOG_2;
    double maxLevel = Math.max(wlevel, hlevel);
    int visibleLeft = Math.max(0, clip.left);
    int visibleRight = Math.min(bounds.width(), clip.right);
    int visibleTop = Math.max(0, clip.top);
    int visibleBottom = Math.min(bounds.height(), clip.bottom);
    int level = (int) Math.floor(maxLevel);
    level = Math.min(this.maxLevel, level);
    level = Math.max(level, 0);
    int levelTiles = 1 << level;
    int textureTileDim = textureDim / levelTiles;
    //            System.out.println("textureTileDim: " + textureTileDim);
    //            System.out.println(info.key + " visible: " + new Rect(visibleLeft, visibleTop, visibleRight, visibleBottom));
    final boolean DEBUG_ZOOM = false;
    if (info.bitmap != null) {
        canvas.drawBitmap(info.bitmap, null, getBounds(), paint);
        if (DEBUG_ZOOM) {
            paint.setColor(Color.RED);
            paint.setAlpha(0x80);
            canvas.drawRect(getBounds(), paint);
            paint.setAlpha(0xFF);
        }
    } else {
        paint.setColor(Color.BLACK);
        canvas.drawRect(getBounds(), paint);
    }
    int sampleSize = 1;
    while (textureTileDim / sampleSize > TILE_DIM) sampleSize <<= 1;
    for (int y = 0; y < levelTiles; y++) {
        int top = textureTileDim * y;
        int bottom = textureTileDim * (y + 1);
        bottom = Math.min(bottom, bounds.bottom);
        // TODO: start at visible pos
        if (bottom < visibleTop)
            continue;
        if (top > visibleBottom)
            break;
        for (int x = 0; x < levelTiles; x++) {
            int left = textureTileDim * x;
            int right = textureTileDim * (x + 1);
            right = Math.min(right, bounds.right);
            // TODO: start at visible pos
            if (right < visibleLeft)
                continue;
            if (left > visibleRight)
                break;
            Rect texRect = new Rect(left, top, right, bottom);
            // find, render/fetch
            //                    System.out.println("rendering: " + texRect + " for: " + bounds);
            String tileKey = FileCache.toKeyString(info.key, ",", level, ",", x, ",", y);
            BitmapInfo tile = ion.bitmapCache.get(tileKey);
            if (tile != null && tile.bitmap != null) {
                // render it
                //                        System.out.println("bitmap is: " + tile.bitmaps[0].getWidth() + "x" + tile.bitmaps[0].getHeight());
                canvas.drawBitmap(tile.bitmap, null, texRect, paint);
                continue;
            }
            // TODO: cancellation of unnecessary regions when fast pan/zooming
            if (ion.bitmapsPending.tag(tileKey) == null) {
                // fetch it
                //                        System.out.println(info.key + ": fetching region: " + texRect + " sample size: " + sampleSize);
                LoadBitmapRegion region = new LoadBitmapRegion(ion, tileKey, info.decoder, texRect, sampleSize);
            }
            ion.bitmapsPending.add(tileKey, tileCallback);
            int parentLeft = 0;
            int parentTop = 0;
            int parentUp = 1;
            int parentLevel = level - parentUp;
            if (x % 2 == 1)
                parentLeft++;
            if (y % 2 == 1)
                parentTop++;
            int parentX = x >> 1;
            int parentY = y >> 1;
            while (parentLevel >= 0) {
                tileKey = FileCache.toKeyString(info.key, ",", parentLevel, ",", parentX, ",", parentY);
                tile = ion.bitmapCache.get(tileKey);
                if (tile != null && tile.bitmap != null)
                    break;
                if (parentX % 2 == 1) {
                    parentLeft += 1 << parentUp;
                }
                if (parentY % 2 == 1) {
                    parentTop += 1 << parentUp;
                }
                parentLevel--;
                parentUp++;
                parentX >>= 1;
                parentY >>= 1;
            }
            // well, i give up
            if (tile == null || tile.bitmap == null)
                continue;
            int subLevelTiles = 1 << parentLevel;
            int subtileDim = textureDim / subLevelTiles;
            int subSampleSize = 1;
            while (subtileDim / subSampleSize > TILE_DIM) subSampleSize <<= 1;
            int subTextureDim = subtileDim / subSampleSize;
            //                    System.out.println(String.format("falling back for %s,%s,%s to %s,%s,%s: %s,%s (%s to %s)", x, y, level, parentX, parentY, parentLevel, parentLeft, parentTop, subTextureDim, subTextureDim >> parentUp));
            subTextureDim >>= parentUp;
            int sourceLeft = subTextureDim * parentLeft;
            int sourceTop = subTextureDim * parentTop;
            Rect sourceRect = new Rect(sourceLeft, sourceTop, sourceLeft + subTextureDim, sourceTop + subTextureDim);
            canvas.drawBitmap(tile.bitmap, sourceRect, texRect, paint);
            if (DEBUG_ZOOM) {
                paint.setColor(Color.RED);
                paint.setAlpha(0x80);
                canvas.drawRect(texRect, paint);
                paint.setAlpha(0xFF);
            }
        }
    }
}
Also used : Rect(android.graphics.Rect) Paint(android.graphics.Paint) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 33 with Paint

use of android.graphics.Paint in project ion by koush.

the class IonDrawable method draw.

@Override
public void draw(Canvas canvas) {
    if (info == null) {
        // draw stuff
        super.draw(canvas);
        // see if we can fetch a bitmap
        if (bitmapFetcher != null) {
            if (bitmapFetcher.sampleWidth == 0 && bitmapFetcher.sampleHeight == 0) {
                if (canvas.getWidth() != 1)
                    bitmapFetcher.sampleWidth = canvas.getWidth();
                if (canvas.getHeight() != 1)
                    bitmapFetcher.sampleHeight = canvas.getHeight();
                // now that we have final dimensions, reattempt to find the image in the cache
                bitmapFetcher.recomputeDecodeKey();
                BitmapInfo found = ion.bitmapCache.get(bitmapFetcher.bitmapKey);
                if (found != null) {
                    // won't be needing THIS anymore
                    bitmapFetcher = null;
                    // found what we're looking for, but can't draw at this very moment,
                    // since we need to trigger a new measure.
                    callback.onCompleted(null, found);
                    return;
                }
            }
            // no image found fetch it.
            callback.register(ion, bitmapFetcher.bitmapKey);
            // already in progress
            if (BitmapFetcher.shouldDeferImageView(ion)) {
                bitmapFetcher.defer();
            } else {
                bitmapFetcher.execute();
            }
            // won't be needing THIS anymore
            bitmapFetcher = null;
        }
        // well, can't do anything else here.
        return;
    }
    if (info.decoder != null) {
        drawDeepZoom(canvas);
        return;
    }
    if (info.drawTime == 0)
        info.drawTime = SystemClock.uptimeMillis();
    long destAlpha = this.alpha;
    if (fadeIn) {
        destAlpha = ((SystemClock.uptimeMillis() - info.drawTime) << 8) / FADE_DURATION;
        destAlpha = Math.min(destAlpha, this.alpha);
    }
    // remove plaeholder if not visible
    if (destAlpha == this.alpha) {
        if (placeholder != null) {
            placeholder = null;
            setDrawableByLayerId(0, NULL_PLACEHOLDER);
        }
    } else {
        // invalidate to fade in
        if (placeholder != null)
            invalidateSelf();
    }
    if (info.gifDecoder != null) {
        super.draw(canvas);
        GifFrame frame = gifDecoder.getCurrentFrame();
        if (frame != null) {
            paint.setAlpha((int) destAlpha);
            canvas.drawBitmap(frame.image, null, getBounds(), paint);
            paint.setAlpha(this.alpha);
            invalidateSelf();
        }
        return;
    }
    if (info.bitmap != null) {
        if (bitmapDrawable != null)
            bitmapDrawable.setAlpha((int) destAlpha);
    } else {
        if (error != null)
            error.setAlpha((int) destAlpha);
    }
    super.draw(canvas);
    if (true)
        return;
    // stolen from picasso
    canvas.save();
    canvas.rotate(45);
    paint.setColor(Color.WHITE);
    canvas.drawRect(0, -10, 7.5f, 10, paint);
    int sourceColor;
    if (servedFrom == ResponseServedFrom.LOADED_FROM_CACHE)
        sourceColor = Color.CYAN;
    else if (servedFrom == ResponseServedFrom.LOADED_FROM_CONDITIONAL_CACHE)
        sourceColor = Color.YELLOW;
    else if (servedFrom == ResponseServedFrom.LOADED_FROM_MEMORY)
        sourceColor = Color.GREEN;
    else
        sourceColor = Color.RED;
    paint.setColor(sourceColor);
    canvas.drawRect(0, -9, 6.5f, 9, paint);
    canvas.restore();
}
Also used : GifFrame(com.koushikdutta.ion.gif.GifFrame) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) Paint(android.graphics.Paint)

Example 34 with Paint

use of android.graphics.Paint in project Lazy by l123456789jy.

the class BitmapUtil method saturation.

/**
     * 饱和度处理
     *
     * @param bitmap          原图
     * @param saturationValue 新的饱和度值
     * @return 改变了饱和度值之后的图片
     */
public static Bitmap saturation(Bitmap bitmap, int saturationValue) {
    // 计算出符合要求的饱和度值
    float newSaturationValue = saturationValue * 1.0F / 127;
    // 创建一个颜色矩阵
    ColorMatrix saturationColorMatrix = new ColorMatrix();
    // 设置饱和度值
    saturationColorMatrix.setSaturation(newSaturationValue);
    // 创建一个画笔并设置其颜色过滤器
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(saturationColorMatrix));
    // 创建一个新的图片并创建画布
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    // 将原图使用给定的画笔画到画布上
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return newBitmap;
}
Also used : ColorMatrixColorFilter(android.graphics.ColorMatrixColorFilter) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) ColorMatrix(android.graphics.ColorMatrix) Paint(android.graphics.Paint)

Example 35 with Paint

use of android.graphics.Paint in project Lazy by l123456789jy.

the class ImageProcessor method lumAndHueAndSaturation.

/**
     * 亮度、色相、饱和度处理
     * @param lumValue 亮度值
     * @param hueValue 色相值
     * @param saturationValue 饱和度值
     * @return 亮度、色相、饱和度处理后的图片
     */
public Bitmap lumAndHueAndSaturation(int lumValue, int hueValue, int saturationValue) {
    //计算出符合要求的饱和度值
    float newSaturationValue = saturationValue * 1.0F / 127;
    //计算出符合要求的亮度值
    float newlumValue = lumValue * 1.0F / 127;
    //计算出符合要求的色相值
    float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
    //创建一个颜色矩阵并设置其饱和度
    ColorMatrix colorMatrix = new ColorMatrix();
    //设置饱和度值
    colorMatrix.setSaturation(newSaturationValue);
    //设置亮度值
    colorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
    // 控制让红色区在色轮上旋转的角度
    colorMatrix.setRotate(0, newHueValue);
    // 控制让绿红色区在色轮上旋转的角度
    colorMatrix.setRotate(1, newHueValue);
    // 控制让蓝色区在色轮上旋转的角度
    colorMatrix.setRotate(2, newHueValue);
    //创建一个画笔并设置其颜色过滤器
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    //创建一个新的图片并创建画布
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    //将原图使用给定的画笔画到画布上
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return newBitmap;
}
Also used : ColorMatrixColorFilter(android.graphics.ColorMatrixColorFilter) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) ColorMatrix(android.graphics.ColorMatrix) Paint(android.graphics.Paint)

Aggregations

Paint (android.graphics.Paint)2248 Canvas (android.graphics.Canvas)466 Bitmap (android.graphics.Bitmap)415 Rect (android.graphics.Rect)267 RectF (android.graphics.RectF)231 TextPaint (android.text.TextPaint)178 PorterDuffXfermode (android.graphics.PorterDuffXfermode)124 Path (android.graphics.Path)120 Matrix (android.graphics.Matrix)105 TypedArray (android.content.res.TypedArray)81 BitmapDrawable (android.graphics.drawable.BitmapDrawable)75 Drawable (android.graphics.drawable.Drawable)65 BitmapShader (android.graphics.BitmapShader)63 Point (android.graphics.Point)60 SuppressLint (android.annotation.SuppressLint)57 View (android.view.View)54 Resources (android.content.res.Resources)42 Test (org.junit.Test)40 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)39 Typeface (android.graphics.Typeface)39