是否有更好/更清洁的方法来处理大量的Canvas方法?

时间:2014-08-31 10:56:56

标签: java android class canvas

我是Android编程的新手,并且一直致力于音乐符号软件应用程序。我只是想知道是否有人有更好/更有效的方式编写我的代码,以便将来更容易编辑。我是否可以将某些方法分组到一个类中以便于查找?我该怎么做?我还有多种复制代码,用于定位字体符号的多种方法......有更简洁的方法吗?我很感激任何输入...我真的想早点而不是稍后调整我的写作风格因为你可以想象3000+符号可能是准备可读代码的噩梦!谢谢!

    package com.example.renaymusic;

    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v4.app.Fragment;
    import android.app.Activity;
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.TextView;
    import android.os.Build;

    public class Workspace extends ActionBarActivity {

public int statusbar = 0;
public boolean keysignature = false;
public boolean timesignature = false;
public int keysigvalue = 0;
public int numberStaves = 1;
public int fZoom = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    SampleView viewWorkspace = new SampleView(this);
    setContentView(viewWorkspace);

    statusbar = getStatusBarHeight();

    /*
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "BravuraText.OTF");
    TextView tv = (TextView) findViewById(R.id.workspace);
    tv.setTypeface(tf);
    */



}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

public class SampleView extends View {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Typeface font;
    private Typeface font2;
    int viewWidth = 0;
    int viewHeight = 0;
    int StaffLinesPixels = 0;


    public SampleView(Context context) {

        super(context);
        setFocusable(true);
        paint.setColor(Color.BLACK);
        paint.setTextSize(150 / fZoom);
        paint.setAntiAlias(true);
        font = Typeface.createFromAsset(context.getAssets(), "Bravura.otf");
        font2 = Typeface.createFromAsset(context.getAssets(), "BravuraText.otf");
        paint.setTypeface(font);


    }

    @Override
    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {

        super.onSizeChanged(xNew, yNew, xOld, yOld);
        viewWidth = xNew;
        viewHeight = yNew;
    }

    @Override
    protected void onDraw(Canvas canvas) {


        /*
        String string2 = getResources().getString(R.string.clefAlto);
        String staveFive = getResources().getString(R.string.staveFive);
        int altoHeight = measureHeight(string2, paint);

        canvas.drawText(string2, viewWidth * 0.09f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(staveFive, viewWidth * 0.09f, (viewHeight + (altoHeight / 2)) * 0.5f, paint);
        */

        canvas.scale(0.7f, 0.7f, viewWidth * 0.5f, viewHeight * 0.5f);

        drawSingleStaff(canvas);
        drawTrebleClef(canvas, paint);
        drawKeySigBFlatTreble(canvas);
        drawTimeSignatureCustom(canvas, 8, 4);
        drawCrotchet(canvas);
        // paint.setStyle(Paint.Style.STROKE);
        // paint.setStrokeWidth(6);


        // canvas.drawRect(280, 520 - paint.getTextSize(), 280 + paint.measureText(staveFive), 520, paint);



    }

    public void drawSingleStaff(Canvas canvas) {


        float dptopx = 0.0f;
        float startBarPx = 0.0f;
        startBarPx = dpToPx(0.85f);
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5 / fZoom);
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f, viewWidth * 0.90f, viewHeight * 0.5f, paint);
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f + dptopx, viewWidth * 0.90f, viewHeight * 0.5f + dptopx, paint);
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f - dptopx, viewWidth * 0.90f, viewHeight * 0.5f - dptopx, paint);
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f - (dptopx * 2), viewWidth * 0.90f, viewHeight * 0.5f - (dptopx * 2), paint);
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f - (dptopx * 3), viewWidth * 0.90f, viewHeight * 0.5f - (dptopx * 3), paint);
        // Draw bar lines - start/end
        canvas.drawLine(viewWidth * 0.09f, viewHeight * 0.5f - (dptopx * 3) - startBarPx, viewWidth * 0.09f, viewHeight * 0.5f + dptopx + startBarPx, paint);
        canvas.drawLine(viewWidth * 0.86f, viewHeight * 0.5f - (dptopx * 3) - startBarPx, viewWidth * 0.86f, viewHeight * 0.5f + dptopx + startBarPx, paint);
    }

    public void drawKeySigGTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f - (dptopx), paint);
        keysignature = true;
        keysigvalue = 2;

    }

    // I need to calculate the available space left in the bar, which bar the crotchet is
    // being added, which stave position, which beat, which segment of the beat.
    // I need to also simplify and reduce the amount of code used for each method - it is messy.

    public void drawCrotchet(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(138);
        paint.setTypeface(font);
        String crotchet = getResources().getString(R.string.upCrotchet);
        canvas.drawText(crotchet, viewWidth * 0.32f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.55f), paint);

    }


    public void drawKeySigDTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp,  viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        keysignature = true;
        keysigvalue = 2;

    }

    public void drawKeySigATreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.194f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.90f), paint);
        keysignature = true;
        keysigvalue = 3;
    }

    public void drawKeySigETreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.194f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.90f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.216f, (viewHeight - statusbar) * 0.5f - (dptopx * 0.40f), paint);
        keysignature = true;
        keysigvalue = 4;
    }

    public void drawKeySigBTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.194f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.90f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.216f, (viewHeight - statusbar) * 0.5f - (dptopx * 0.40f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.238f, (viewHeight - statusbar) * 0.5f + (dptopx * 1.10f), paint);
        keysignature = true;
        keysigvalue = 5;
    }

    public void drawKeySigFSharpTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.194f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.90f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.216f, (viewHeight - statusbar) * 0.5f - (dptopx * 0.40f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.238f, (viewHeight - statusbar) * 0.5f + (dptopx * 1.10f), paint);
        canvas.drawText(accidentalSharp,  viewWidth * 0.260f,  (viewHeight - statusbar) * 0.5f - (dptopx * 0.95f), paint);
        keysignature = true;
        keysigvalue = 6;
    }

    public void drawKeySigCSharpTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190);
        paint.setTypeface(font);
        String accidentalSharp = getResources().getString(R.string.accidentalSharp);
        canvas.drawText(accidentalSharp, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.35f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.172f, (viewHeight - statusbar) * 0.5f, paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.194f, (viewHeight - statusbar) * 0.5f - (dptopx * 1.90f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.216f, (viewHeight - statusbar) * 0.5f - (dptopx * 0.40f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.238f, (viewHeight - statusbar) * 0.5f + (dptopx * 1.10f), paint);
        canvas.drawText(accidentalSharp,  viewWidth * 0.260f,  (viewHeight - statusbar) * 0.5f - (dptopx * 0.95f), paint);
        canvas.drawText(accidentalSharp, viewWidth * 0.282f,  (viewHeight - statusbar) * 0.5f + (dptopx * 0.60f), paint);
        keysignature = true;
        keysigvalue = 7;
    }

    public void drawKeySigFTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190);
        paint.setTypeface(font);
        String accidentalFlat = getResources().getString(R.string.accidentalFlat);
        canvas.drawText(accidentalFlat, viewWidth * 0.16f,  (viewHeight - statusbar) * 0.5f + (dptopx * 0.75f), paint);
        keysignature = true;
        keysigvalue = -1;
    }

    public void drawKeySigBFlatTreble(Canvas canvas) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font);
        String accidentalFlat = getResources().getString(R.string.accidentalFlat);
        canvas.drawText(accidentalFlat, viewWidth * 0.16f,  (viewHeight - statusbar) * 0.5f + (dptopx * 0.75f), paint);
        canvas.drawText(accidentalFlat, viewWidth * 0.18f,  (viewHeight - statusbar) * 0.5f - (dptopx * 0.79f), paint);
        keysignature = true;
        keysigvalue = -2;
    }


    public void drawSignatureCommon(Canvas canvas) {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font2);
        String timeCommon = getResources().getString(R.string.timeCommon);

        if (keysigvalue == 0) {

            canvas.drawText(timeCommon, viewWidth * 0.15f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 1) {

            canvas.drawText(timeCommon, viewWidth * 0.18f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == -1) {

            canvas.drawText(timeCommon, viewWidth * 0.20f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 2 || keysigvalue == -2) {

            canvas.drawText(timeCommon, viewWidth * 0.21f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 3 || keysigvalue == -3) {

            canvas.drawText(timeCommon, viewWidth * 0.23f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 4 || keysigvalue == -4) {

            canvas.drawText(timeCommon, viewWidth * 0.26f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 5 || keysigvalue == -5) {

            canvas.drawText(timeCommon, viewWidth * 0.29f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 6 || keysigvalue == -6) {

            canvas.drawText(timeCommon, viewWidth * 0.30f, (viewHeight - statusbar) * 0.5f, paint);
        }

        if(keysigvalue == 7 || keysigvalue == -7) {

            canvas.drawText(timeCommon,  viewWidth * 0.32f,  (viewHeight - statusbar) * 0.5f,  paint);
        }

        timesignature = true;
    }

    public void drawTimeSignatureCustom(Canvas canvas, int iDivisions, int iBeatLength) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(190 / fZoom);
        paint.setTypeface(font2);
        String sDivisions = "";
        String sBeatLength = "";
        String timeZero = getResources().getString(R.string.timeZero);
        String timeOne = getResources().getString(R.string.timeOne);
        String timeTwo = getResources().getString(R.string.timeTwo);
        String timeFour = getResources().getString(R.string.timeFour);
        String timeThree = getResources().getString(R.string.timeThree);
        String timeFive = getResources().getString(R.string.timeFive);
        String timeSix = getResources().getString(R.string.timeSix);
        String timeSeven = getResources().getString(R.string.timeSeven);
        String timeEight = getResources().getString(R.string.timeEight);
        String timeNine = getResources().getString(R.string.timeNine);

        if (iDivisions == 0) {
            sDivisions = timeZero;
        }

        if(iDivisions == 1) {
            sDivisions = timeOne;
        }

        if(iDivisions == 2) {
            sDivisions = timeTwo;
        }

        if(iDivisions == 3) {
            sDivisions = timeThree;
        }

        if(iDivisions == 4) {
            sDivisions = timeFour;
        }

        if(iDivisions == 5) {
            sDivisions = timeFive;
        }

        if(iDivisions == 6) {
            sDivisions = timeSix;
        }

        if(iDivisions == 7) {
            sDivisions = timeSeven;
        }

        if(iDivisions == 8) {
            sDivisions = timeEight;
        }

        if(iDivisions == 9) {
            sDivisions = timeNine;
        }

        if(iBeatLength == 0) {
            sBeatLength = timeZero;
        }

        if(iBeatLength == 1) {
            sBeatLength = timeOne;
        }

        if(iBeatLength == 2) {
            sBeatLength = timeTwo;
        }

        if(iBeatLength == 3) {
            sBeatLength = timeThree;
        }

        if(iBeatLength == 4) {
            sBeatLength = timeFour;
        }

        if(iBeatLength == 5) {
            sBeatLength = timeFive;
        }

        if(iBeatLength == 6) {
            sBeatLength = timeSix;
        }

        if(iBeatLength == 7) {
            sBeatLength = timeSeven;
        }

        if(iBeatLength == 8) {
            sBeatLength = timeEight;
        }

        if(iBeatLength == 9) {
            sBeatLength = timeNine;
        }


        if (keysigvalue == -2) {
            canvas.drawText(sDivisions, viewWidth * 0.22f,  (viewHeight - statusbar) * 0.5f - (dptopx * 1.02f), paint);
            canvas.drawText(sBeatLength, viewWidth * 0.22f, (viewHeight - statusbar) * 0.5f + (dptopx * 1.04f), paint);
        }

        if(keysigvalue == -1) {
            canvas.drawText(sDivisions, viewWidth * 0.20f,  (viewHeight - statusbar) * 0.5f - (dptopx * 1.02f), paint);
            canvas.drawText(sBeatLength, viewWidth * 0.20f, (viewHeight - statusbar) * 0.5f + (dptopx * 1.04f), paint);

        }

    }


    public void drawAltoClef(Canvas canvas, Paint paint) {

        String clefAlto = getResources().getString(R.string.clefAlto);
        canvas.drawText(clefAlto, viewWidth * 0.09f, (viewHeight - statusbar) * 0.5f, paint);
    }

    public void drawTrebleClef(Canvas canvas, Paint paint) {

        float dptopx = 0.0f;
        dptopx = dpToPx(12.4f);
        String clefTreble = getResources().getString(R.string.clefTreble);
        canvas.drawText(clefTreble, viewWidth * 0.09f, (viewHeight - statusbar) * 0.5f + dptopx, paint);
    }
}

public static float dpToPx(float dp)
{
    return (float) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static float pxToDp(float px)
{
    return (float) (px / Resources.getSystem().getDisplayMetrics().density);
}




public int measureHeight(String text, Paint brush) {

    Rect result = new Rect();
    // Measure the text rectangle to get the height
    brush.getTextBounds(text, 0, text.length(), result);

    return result.height();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.workspace, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

0 个答案:

没有答案