Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 优化自定义裁剪图形_Android_Performance_Android Layout_Memory - Fatal编程技术网

Android 优化自定义裁剪图形

Android 优化自定义裁剪图形,android,performance,android-layout,memory,Android,Performance,Android Layout,Memory,我已经通过以下方式实现了组件的绘图,如图所示: @Override protected void onDraw(Canvas canvas) { int w = canvas.getWidth(); int h = canvas.getHeight(); if (mFinalBitmap == null) { mFinalBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.AR

我已经通过以下方式实现了组件的绘图,如图所示:

@Override
protected void onDraw(Canvas canvas) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();

    if (mFinalBitmap == null) {
        mFinalBitmap = Bitmap.createBitmap(w, h,
                Bitmap.Config.ARGB_8888);
    }

    if (mTempCanvas == null) {
        mTempCanvas = new Canvas(mFinalBitmap);
    }

    if (mBackgroundBitmap == null) {
        mBackgroundBitmap = createBitmap(R.drawable.rounded_background,
                w, h);
    }

    if (mBackgroundImage == null) {
        mBackgroundImage = createBitmap(R.drawable.image_for_background, w, h);
    }

    mTempCanvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
    mTempCanvas.drawBitmap(mBackgroundImage, 0, 0, mPaint);

    canvas.drawBitmap(mFinalBitmap, 0, 0, null);
}

private Bitmap createBitmap(int id, int width, int height) {

    Bitmap bitmap = BitmapFactory.decodeResource(getContext()
            .getResources(), id);

    return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
mPaint在哪里

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

我想知道这段代码是否好,或者是否可以针对相同的结果进行优化,它使用了大量内存,并且可能触发
OutOfMemoryError


谢谢。

使用
位图着色器
可以实现以下绘制,然后我只需要创建ALPHA_8
位图
即可使用着色器使用
绘制
对象。但是,着色器固定在窗口坐标中,因此在滚动组件中使用此方法可能会导致一些问题,因为需要正确转换
矩阵

// Create bitmap shader
if (mShaderBitmap == null) {
    mShaderBitmap = use wanted bitmap here.
    BitmapShader shader = new BitmapShader(mShaderBitmap,
            TileMode.CLAMP, TileMode.CLAMP);
    mPaint.setShader(shader);
}

// Create alpha bitmap to draw with shader paint
if (mBitmapToDraw == null) {
    mBitmapToDraw = load the shape here with ALPHA_8 Config
}

canvas.drawBitmap(mBitmapToDraw, 0, 0, mPaint);

看一看代码几乎是一样的,大量的位图创建,临时画布在哪里绘制,使用ARGB_888的结果位图使其沉重。这真的是最好的解决方案吗?如何“正确地翻译矩阵”?我在自定义drawable中使用此方法,但它在Android 4.0+上不起作用,它总是固定在窗口画布(0,0)的左上角,而不是我的视图!但是我已经在2.3.3上测试过了,效果很好!?!?