1

私が作成した描画クラスには、パフォーマンスの問題があります。描画アクションと元に戻す/やり直し機能を処理する方法に関係していると思います。パフォーマンスを向上させる方法について誰かアドバイスをいただけますか?

public class KNDrawingSurfaceView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;
    public Bitmap mBitmap;
    public Canvas mCanvas;
    public Path mPath;
    public Paint mBitmapPaint;   
    float myWidth;
    float myHeight;
    public Paint mPaint;
    public MaskFilter mEmboss;
    public MaskFilter mBlur;
    public ArrayList<Path> paths = new ArrayList<Path>();
    public ArrayList<Paint>paints = new ArrayList<Paint>();
    public ArrayList<Path> undonePaths = new ArrayList<Path>(); 
    public ArrayList<Paint>undonePaints = new ArrayList<Paint>();

    private KNSketchBookActivity _parent;

    public KNDrawingSurfaceView(Context c, float width,float height, KNSketchBookActivity parent) {
        super(c);
        myWidth = width;
        myHeight = height;
        _parent =parent;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        mPath = new Path();

        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        mBitmap = Bitmap.createBitmap((int)myWidth, (int)myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        Log.v("onDraw:", "curent paths size:"+paths.size());
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        Paint tile = new Paint();

        Bitmap tileImage =  BitmapFactory.decodeResource(getResources(),R.drawable.checkerpattern);
        BitmapShader shader = new BitmapShader(tileImage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        tile.setShader(shader);
        canvas.drawRect(0, 0, myWidth, myHeight, tile);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        for (Path p : paths){
            canvas.drawPath(p, mPaint);
        }
        canvas.drawPath(mPath,mPaint);
    }

    public void onClickUndo () { 

        if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           undonePaints.add(paints.remove(paints.size()-1));
           invalidate();
         }
        else
        {

        }
         _parent.checkButtonStates();
    }
    public void onClickRedo (){
        if (undonePaths.size()>0) 
        { 
            paths.add(undonePaths.remove(undonePaths.size()-1));
            paints.add(undonePaints.remove(undonePaints.size()-1));
            invalidate();
        } 
        else 
        {

        }
        _parent.checkButtonStates();
     }
    public void onClickClear (){
        paths.clear();
        undonePaths.clear();
        invalidate();
        _parent.checkButtonStates();
     }
    public void saveDrawing(){


        FileOutputStream outStream = null;
        String fileName = "tempTag";
        try {

            outStream = new FileOutputStream("/sdcard/" + fileName + ".png");

            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }


    }
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        undonePaths.clear();
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);

        mCanvas.drawPath(mPath, mPaint);

        paths.add(mPath);
        paints.add(mPaint);
        _parent.checkButtonStates();
        mPath = new Path(); 
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if(x>myWidth){
            x=myWidth;

        }
        if(y>myHeight){
            y=myHeight;

        }
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
        }
        return true;
    }
}

描画/キャンバスの最適化に関する経験やリンクがあれば教えてください

4

4 に答える 4

1

皆さんが提案したように、私は onDraw からすべての var 開始を引き出し、それらをコンストラクターに入れました。

この特定の部分は、ユーザーが描画したものを元に戻したりやり直したりしたときにキャンバスをクリアするために必要です。

mBitmap = Bitmap.createBitmap((int) myWidth, (int) myHeight, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);

だから私は元に戻す/やり直すだけで呼び出す新しいメソッドを作成しました:

public void clearCanvasCache() {

        mBitmap = Bitmap.createBitmap((int) myWidth, (int) myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

今は素晴らしい作品..

クラス全体:

public class KNDrawingSurfaceView extends View {

    private static final float MINP = 0.25f;

    private static final float MAXP = 0.75f;

    public Bitmap mBitmap;

    public Canvas mCanvas;

    public Path mPath;

    public Paint mBitmapPaint;

    float myWidth;

    float myHeight;

    public Paint mPaint;

    public MaskFilter mEmboss;

    public MaskFilter mBlur;

    public ArrayList<Path> paths = new ArrayList<Path>();

    public ArrayList<Paint> paints = new ArrayList<Paint>();

    public ArrayList<Path> undonePaths = new ArrayList<Path>();

    public ArrayList<Paint> undonePaints = new ArrayList<Paint>();

    private KNSketchBookActivity _parent;

    Paint tile;

    Bitmap tileImage;

    BitmapShader shader;

    public KNDrawingSurfaceView(Context c, float width, float height, KNSketchBookActivity parent) {

        super(c);

        myWidth = width;
        myHeight = height;

        mBitmap = Bitmap.createBitmap((int) myWidth, (int) myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

        _parent = parent;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        tile = new Paint();

        tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.checkerpattern);
        shader = new BitmapShader(tileImage, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        tile.setShader(shader);

        mPath = new Path();

        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Log.v("onDraw:", "curent paths size:" + paths.size());
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        canvas.drawRect(0, 0, myWidth, myHeight, tile);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        for (Path p : paths) {
            canvas.drawPath(p, mPaint);
        }
        canvas.drawPath(mPath, mPaint);
    }

    public void onClickUndo() {

        if (paths.size() > 0) {
            undonePaths.add(paths.remove(paths.size() - 1));
            undonePaints.add(paints.remove(paints.size() - 1));
            clearCanvasCache();
            invalidate();
        } else {

        }
        _parent.checkButtonStates();
    }

    public void onClickRedo() {

        if (undonePaths.size() > 0) {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
            paints.add(undonePaints.remove(undonePaints.size() - 1));
            clearCanvasCache();
            invalidate();
        } else {

        }
        _parent.checkButtonStates();
    }

    public void onClickClear() {

        paths.clear();
        undonePaths.clear();
        clearCanvasCache();
        invalidate();
        _parent.checkButtonStates();
    }

    public void saveDrawing() {

        FileOutputStream outStream = null;
        String fileName = "tempTag";
        try {

            outStream = new FileOutputStream("/sdcard/" + fileName + ".png");

            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

    }

    private float mX, mY;

    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {

        undonePaths.clear();
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {

        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {

        mPath.lineTo(mX, mY);

        mCanvas.drawPath(mPath, mPaint);


        paths.add(mPath);
        paints.add(mPaint);
        _parent.checkButtonStates();
        mPath = new Path();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!_parent.isDrawerOpen()) {
            float x = event.getX();
            float y = event.getY();
            if (x > myWidth) {
                x = myWidth;

            }
            if (y > myHeight) {
                y = myHeight;

            }
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
            }
            return true;
        } else {
            return false;
        }
    }

    public void clearCanvasCache() {

        mBitmap = Bitmap.createBitmap((int) myWidth, (int) myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }
}
于 2013-06-06T17:11:37.770 に答える
1

onDraw() メソッドで 2 つのビットマップ、ペイント、BitmapShader を再作成しています。これにより、パフォーマンスの問題が発生しています。これを試してください: - オブジェクトの作成をコンストラクターに移動します。- この部分を完全に削除できると思います:

mBitmap = Bitmap.createBitmap((int)myWidth, (int)myHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

(キャンバスからビットマップを取得する必要がある場合は、そのための別のメソッドを作成し、必要に応じて呼び出します)

于 2013-06-06T14:11:22.903 に答える