Rename PitchView
app/src/main/java/eu/lepiller/nani/PitchDiagramView.java unknown status 1
| 1 | + | package eu.lepiller.nani; | |
| 2 | + | ||
| 3 | + | import android.content.Context; | |
| 4 | + | import android.content.res.TypedArray; | |
| 5 | + | import android.graphics.Canvas; | |
| 6 | + | import android.graphics.Color; | |
| 7 | + | import android.graphics.Paint; | |
| 8 | + | import android.util.AttributeSet; | |
| 9 | + | import android.util.Pair; | |
| 10 | + | import android.view.View; | |
| 11 | + | import android.widget.TextView; | |
| 12 | + | ||
| 13 | + | import androidx.annotation.Nullable; | |
| 14 | + | ||
| 15 | + | import java.util.ArrayList; | |
| 16 | + | ||
| 17 | + | public class PitchDiagramView extends View { | |
| 18 | + | private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| 19 | + | private int textColor = Color.BLACK; | |
| 20 | + | private int pitchColor = Color.BLACK; | |
| 21 | + | private ArrayList<Pair<String, Boolean>> pitchedMora = new ArrayList<>(); | |
| 22 | + | private int accent; | |
| 23 | + | private String text; | |
| 24 | + | ||
| 25 | + | private float textSize = 32; | |
| 26 | + | ||
| 27 | + | private static final String TAG = "PITCHVIEW"; | |
| 28 | + | ||
| 29 | + | public PitchDiagramView(Context context) { | |
| 30 | + | super(context); | |
| 31 | + | setDefaults(); | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | public PitchDiagramView(Context context, @Nullable AttributeSet attrs) { | |
| 35 | + | super(context, attrs); | |
| 36 | + | setDefaults(); | |
| 37 | + | setupAttributes(attrs, 0); | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | public PitchDiagramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
| 41 | + | super(context, attrs, defStyleAttr); | |
| 42 | + | setDefaults(); | |
| 43 | + | setupAttributes(attrs, defStyleAttr); | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | private void setDefaults() { | |
| 47 | + | textColor = getContext().getResources().getColor(android.R.color.tab_indicator_text); | |
| 48 | + | pitchColor = getContext().getResources().getColor(android.R.color.background_dark); | |
| 49 | + | textSize = new TextView(getContext()).getTextSize(); | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | private void setupAttributes(AttributeSet attrs, int defStyleAttr) { | |
| 53 | + | TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.PitchDiagramView, defStyleAttr, 0); | |
| 54 | + | textColor = typedArray.getColor(R.styleable.PitchDiagramView_textColor, textColor); | |
| 55 | + | pitchColor = typedArray.getColor(R.styleable.PitchDiagramView_pitchColor, pitchColor); | |
| 56 | + | textSize = typedArray.getDimension(R.styleable.PitchDiagramView_textSize, textSize); | |
| 57 | + | text = typedArray.getString(R.styleable.PitchDiagramView_text); | |
| 58 | + | accent = typedArray.getInt(R.styleable.PitchDiagramView_pitch, -1); | |
| 59 | + | updatePitchedMora(); | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | private static boolean is_mora(char c) { | |
| 63 | + | ArrayList<Character> not_mora = new ArrayList<>(); | |
| 64 | + | not_mora.add("???".charAt(0)); | |
| 65 | + | not_mora.add("???".charAt(0)); | |
| 66 | + | not_mora.add("???".charAt(0)); | |
| 67 | + | not_mora.add("???".charAt(0)); | |
| 68 | + | not_mora.add("???".charAt(0)); | |
| 69 | + | not_mora.add("???".charAt(0)); | |
| 70 | + | return !not_mora.contains(c); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | public void setText(String text) { | |
| 74 | + | this.text = text; | |
| 75 | + | updatePitchedMora(); | |
| 76 | + | notify(); | |
| 77 | + | } | |
| 78 | + | ||
| 79 | + | public void setPitch(int pitch) { | |
| 80 | + | this.accent = pitch; | |
| 81 | + | updatePitchedMora(); | |
| 82 | + | notify(); | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | public String getText() { | |
| 86 | + | return text; | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | public int getPitch() { | |
| 90 | + | return accent; | |
| 91 | + | } | |
| 92 | + | ||
| 93 | + | private void updatePitchedMora() { | |
| 94 | + | ArrayList<String> moras = new ArrayList<>(); | |
| 95 | + | ||
| 96 | + | if(accent < 0 || text == null) | |
| 97 | + | return; | |
| 98 | + | ||
| 99 | + | for(int i=0; i<text.length(); i++) { | |
| 100 | + | char here = text.charAt(i); | |
| 101 | + | if(is_mora(here) && (i == text.length()-1 || is_mora(text.charAt(i+1)))) | |
| 102 | + | moras.add(text.substring(i,i+1)); | |
| 103 | + | else if(is_mora(here)) | |
| 104 | + | moras.add(text.substring(i,i+2)); | |
| 105 | + | } | |
| 106 | + | moras.add(null); | |
| 107 | + | pitchedMora = new ArrayList<>(); | |
| 108 | + | if(accent == 0) | |
| 109 | + | setHeiban(pitchedMora, moras); | |
| 110 | + | else if(accent == 1) | |
| 111 | + | setAtamadaka(pitchedMora, moras); | |
| 112 | + | else | |
| 113 | + | setOtherAccent(pitchedMora, moras, accent); | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | public void setTextColor(int textColor) { | |
| 117 | + | this.textColor = textColor; | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | public void setPitchColor(int pitchColor) { | |
| 121 | + | this.pitchColor = pitchColor; | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | public void setTextSize(float textSize) { | |
| 125 | + | this.textSize = textSize; | |
| 126 | + | notify(); | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | private static float getCharWidth(float textSize) { | |
| 130 | + | return (float)2.25 * textSize; | |
| 131 | + | } | |
| 132 | + | ||
| 133 | + | private static float getPitchRadius(float textSize) { | |
| 134 | + | return textSize / 3; | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | private static float getStrokeWidth(float textSize) { | |
| 138 | + | return getPitchRadius(textSize) / 2; | |
| 139 | + | } | |
| 140 | + | ||
| 141 | + | private static float getParticleRadius(float textSize) { | |
| 142 | + | return 2 * getPitchRadius(textSize) / 3; | |
| 143 | + | } | |
| 144 | + | ||
| 145 | + | private static float getLowPosition(float textSize) { | |
| 146 | + | return 2 * textSize + getParticleRadius(textSize); | |
| 147 | + | } | |
| 148 | + | ||
| 149 | + | private static float getHighPosition(float textSize) { | |
| 150 | + | return getPitchRadius(textSize) + getParticleRadius(textSize); | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | private static float getMoraPosition(float textSize) { | |
| 154 | + | return (float)3.5 * textSize + getParticleRadius(textSize); | |
| 155 | + | } | |
| 156 | + | ||
| 157 | + | private static void setHeiban(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras) { | |
| 158 | + | text.add(new Pair<>(moras.get(0), false)); | |
| 159 | + | for(int i=1; i<moras.size(); i++) { | |
| 160 | + | text.add(new Pair<>(moras.get(i), true)); | |
| 161 | + | } | |
| 162 | + | } | |
| 163 | + | ||
| 164 | + | private static void setAtamadaka(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras) { | |
| 165 | + | text.add(new Pair<>(moras.get(0), true)); | |
| 166 | + | for(int i=1; i<moras.size(); i++) { | |
| 167 | + | text.add(new Pair<>(moras.get(i), false)); | |
| 168 | + | } | |
| 169 | + | } | |
| 170 | + | ||
| 171 | + | private static void setOtherAccent(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras, int accent) { | |
| 172 | + | text.add(new Pair<>(moras.get(0), false)); | |
| 173 | + | int i; | |
| 174 | + | for(i=1; i<moras.size() && i < accent; i++) { | |
| 175 | + | text.add(new Pair<>(moras.get(i), true)); | |
| 176 | + | } | |
| 177 | + | for(; i<moras.size(); i++) { | |
| 178 | + | text.add(new Pair<>(moras.get(i), false)); | |
| 179 | + | } | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | @Override | |
| 183 | + | protected void onDraw(Canvas canvas) { | |
| 184 | + | super.onDraw(canvas); | |
| 185 | + | if(pitchedMora == null || pitchedMora.size() == 0) | |
| 186 | + | return; | |
| 187 | + | ||
| 188 | + | paint.setStrokeWidth(getParticleRadius(textSize)); | |
| 189 | + | paint.setTextSize(textSize); | |
| 190 | + | boolean previous = true; | |
| 191 | + | ||
| 192 | + | for(int i = 0; i< pitchedMora.size(); i++) { | |
| 193 | + | paint.setColor(pitchColor); | |
| 194 | + | Pair<String, Boolean> mora = pitchedMora.get(i); | |
| 195 | + | paint.setStyle(mora.first==null? Paint.Style.STROKE: Paint.Style.FILL_AND_STROKE); | |
| 196 | + | float x = getX(i, textSize); | |
| 197 | + | float y = getY(mora.second, textSize); | |
| 198 | + | canvas.drawCircle(x, y, getPitchRadius(textSize), paint); | |
| 199 | + | ||
| 200 | + | if(i>0) { | |
| 201 | + | paint.setStyle(Paint.Style.STROKE); | |
| 202 | + | paint.setStrokeWidth(getStrokeWidth(textSize)); | |
| 203 | + | double radius = getPitchRadius(textSize) + getParticleRadius(textSize)/2; | |
| 204 | + | drawLine(canvas, paint, i-1, previous, mora.second, radius, textSize); | |
| 205 | + | paint.setStrokeWidth(getParticleRadius(textSize)); | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | if(mora.first != null) { | |
| 209 | + | paint.setColor(textColor); | |
| 210 | + | float width = paint.measureText(mora.first); | |
| 211 | + | paint.setStyle(Paint.Style.FILL); | |
| 212 | + | canvas.drawText(mora.first, x - width/2, getMoraPosition(textSize), paint); | |
| 213 | + | } | |
| 214 | + | ||
| 215 | + | previous = mora.second; | |
| 216 | + | } | |
| 217 | + | } | |
| 218 | + | ||
| 219 | + | private static void drawLine(Canvas canvas, Paint paint, int position, boolean a, boolean b, double radius, float textSize) { | |
| 220 | + | float startX = getX(position, textSize); | |
| 221 | + | float startY = getY(a, textSize); | |
| 222 | + | float endX = getX(position + 1, textSize); | |
| 223 | + | float endY = getY(b, textSize); | |
| 224 | + | ||
| 225 | + | if(a == b) { | |
| 226 | + | startX += radius; | |
| 227 | + | endX -= radius; | |
| 228 | + | } else { | |
| 229 | + | double dist = Math.sqrt(Math.pow(startX-endX, 2)+Math.pow(startY-endY, 2)); | |
| 230 | + | startY += (endY - startY) * radius / dist; | |
| 231 | + | startX += (endX - startX) * radius / dist; | |
| 232 | + | ||
| 233 | + | endX -= (endX - startX) * radius / dist; | |
| 234 | + | endY -= (endY - startY) * radius / dist; | |
| 235 | + | } | |
| 236 | + | ||
| 237 | + | canvas.drawLine(startX, startY, endX, endY, paint); | |
| 238 | + | } | |
| 239 | + | ||
| 240 | + | private static float getX(int i, float textSize) { | |
| 241 | + | return i*getCharWidth(textSize) + getCharWidth(textSize)/2; | |
| 242 | + | } | |
| 243 | + | ||
| 244 | + | private static float getY(boolean high, float textSize) { | |
| 245 | + | return high? getHighPosition(textSize): getLowPosition(textSize); | |
| 246 | + | } | |
| 247 | + | ||
| 248 | + | @Override | |
| 249 | + | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| 250 | + | super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
| 251 | + | ||
| 252 | + | if(pitchedMora != null) | |
| 253 | + | setMeasuredDimension((int)getX(pitchedMora.size(), textSize), (int)(getMoraPosition(textSize) + textSize/2)); | |
| 254 | + | else | |
| 255 | + | setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); | |
| 256 | + | } | |
| 257 | + | } |
app/src/main/java/eu/lepiller/nani/PitchView.java unknown status 2
| 1 | - | package eu.lepiller.nani; | |
| 2 | - | ||
| 3 | - | import android.content.Context; | |
| 4 | - | import android.content.res.TypedArray; | |
| 5 | - | import android.graphics.Canvas; | |
| 6 | - | import android.graphics.Color; | |
| 7 | - | import android.graphics.Paint; | |
| 8 | - | import android.util.AttributeSet; | |
| 9 | - | import android.util.Log; | |
| 10 | - | import android.util.Pair; | |
| 11 | - | import android.view.View; | |
| 12 | - | import android.widget.TextView; | |
| 13 | - | ||
| 14 | - | import androidx.annotation.Nullable; | |
| 15 | - | ||
| 16 | - | import java.util.ArrayList; | |
| 17 | - | ||
| 18 | - | public class PitchView extends View { | |
| 19 | - | private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| 20 | - | private int textColor = Color.BLACK; | |
| 21 | - | private int pitchColor = Color.BLACK; | |
| 22 | - | private ArrayList<Pair<String, Boolean>> pitchedMora = new ArrayList<>(); | |
| 23 | - | private int accent; | |
| 24 | - | private String text; | |
| 25 | - | ||
| 26 | - | private float textSize = 32; | |
| 27 | - | ||
| 28 | - | private static final String TAG = "PITCHVIEW"; | |
| 29 | - | ||
| 30 | - | public PitchView(Context context) { | |
| 31 | - | super(context); | |
| 32 | - | setDefaults(); | |
| 33 | - | } | |
| 34 | - | ||
| 35 | - | public PitchView(Context context, @Nullable AttributeSet attrs) { | |
| 36 | - | super(context, attrs); | |
| 37 | - | setDefaults(); | |
| 38 | - | setupAttributes(attrs, 0); | |
| 39 | - | } | |
| 40 | - | ||
| 41 | - | public PitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
| 42 | - | super(context, attrs, defStyleAttr); | |
| 43 | - | setDefaults(); | |
| 44 | - | setupAttributes(attrs, defStyleAttr); | |
| 45 | - | } | |
| 46 | - | ||
| 47 | - | private void setDefaults() { | |
| 48 | - | textColor = getContext().getResources().getColor(android.R.color.tab_indicator_text); | |
| 49 | - | pitchColor = getContext().getResources().getColor(android.R.color.background_dark); | |
| 50 | - | textSize = new TextView(getContext()).getTextSize(); | |
| 51 | - | } | |
| 52 | - | ||
| 53 | - | private void setupAttributes(AttributeSet attrs, int defStyleAttr) { | |
| 54 | - | TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.PitchView, defStyleAttr, 0); | |
| 55 | - | textColor = typedArray.getColor(R.styleable.PitchView_textColor, textColor); | |
| 56 | - | pitchColor = typedArray.getColor(R.styleable.PitchView_pitchColor, pitchColor); | |
| 57 | - | textSize = typedArray.getDimension(R.styleable.PitchView_textSize, textSize); | |
| 58 | - | text = typedArray.getString(R.styleable.PitchView_text); | |
| 59 | - | accent = typedArray.getInt(R.styleable.PitchView_pitch, -1); | |
| 60 | - | updatePitchedMora(); | |
| 61 | - | } | |
| 62 | - | ||
| 63 | - | private static boolean is_mora(char c) { | |
| 64 | - | ArrayList<Character> not_mora = new ArrayList<>(); | |
| 65 | - | not_mora.add("???".charAt(0)); | |
| 66 | - | not_mora.add("???".charAt(0)); | |
| 67 | - | not_mora.add("???".charAt(0)); | |
| 68 | - | not_mora.add("???".charAt(0)); | |
| 69 | - | not_mora.add("???".charAt(0)); | |
| 70 | - | not_mora.add("???".charAt(0)); | |
| 71 | - | return !not_mora.contains(c); | |
| 72 | - | } | |
| 73 | - | ||
| 74 | - | public void setText(String text) { | |
| 75 | - | this.text = text; | |
| 76 | - | updatePitchedMora(); | |
| 77 | - | notify(); | |
| 78 | - | } | |
| 79 | - | ||
| 80 | - | public void setPitch(int pitch) { | |
| 81 | - | this.accent = pitch; | |
| 82 | - | updatePitchedMora(); | |
| 83 | - | notify(); | |
| 84 | - | } | |
| 85 | - | ||
| 86 | - | public String getText() { | |
| 87 | - | return text; | |
| 88 | - | } | |
| 89 | - | ||
| 90 | - | public int getPitch() { | |
| 91 | - | return accent; | |
| 92 | - | } | |
| 93 | - | ||
| 94 | - | private void updatePitchedMora() { | |
| 95 | - | ArrayList<String> moras = new ArrayList<>(); | |
| 96 | - | ||
| 97 | - | if(accent < 0 || text == null) | |
| 98 | - | return; | |
| 99 | - | ||
| 100 | - | for(int i=0; i<text.length(); i++) { | |
| 101 | - | char here = text.charAt(i); | |
| 102 | - | if(is_mora(here) && (i == text.length()-1 || is_mora(text.charAt(i+1)))) | |
| 103 | - | moras.add(text.substring(i,i+1)); | |
| 104 | - | else if(is_mora(here)) | |
| 105 | - | moras.add(text.substring(i,i+2)); | |
| 106 | - | } | |
| 107 | - | moras.add(null); | |
| 108 | - | pitchedMora = new ArrayList<>(); | |
| 109 | - | if(accent == 0) | |
| 110 | - | setHeiban(pitchedMora, moras); | |
| 111 | - | else if(accent == 1) | |
| 112 | - | setAtamadaka(pitchedMora, moras); | |
| 113 | - | else | |
| 114 | - | setOtherAccent(pitchedMora, moras, accent); | |
| 115 | - | } | |
| 116 | - | ||
| 117 | - | public void setTextColor(int textColor) { | |
| 118 | - | this.textColor = textColor; | |
| 119 | - | } | |
| 120 | - | ||
| 121 | - | public void setPitchColor(int pitchColor) { | |
| 122 | - | this.pitchColor = pitchColor; | |
| 123 | - | } | |
| 124 | - | ||
| 125 | - | public void setTextSize(float textSize) { | |
| 126 | - | this.textSize = textSize; | |
| 127 | - | notify(); | |
| 128 | - | } | |
| 129 | - | ||
| 130 | - | private static float getCharWidth(float textSize) { | |
| 131 | - | return (float)2.25 * textSize; | |
| 132 | - | } | |
| 133 | - | ||
| 134 | - | private static float getPitchRadius(float textSize) { | |
| 135 | - | return textSize / 3; | |
| 136 | - | } | |
| 137 | - | ||
| 138 | - | private static float getStrokeWidth(float textSize) { | |
| 139 | - | return getPitchRadius(textSize) / 2; | |
| 140 | - | } | |
| 141 | - | ||
| 142 | - | private static float getParticleRadius(float textSize) { | |
| 143 | - | return 2 * getPitchRadius(textSize) / 3; | |
| 144 | - | } | |
| 145 | - | ||
| 146 | - | private static float getLowPosition(float textSize) { | |
| 147 | - | return 2 * textSize + getParticleRadius(textSize); | |
| 148 | - | } | |
| 149 | - | ||
| 150 | - | private static float getHighPosition(float textSize) { | |
| 151 | - | return getPitchRadius(textSize) + getParticleRadius(textSize); | |
| 152 | - | } | |
| 153 | - | ||
| 154 | - | private static float getMoraPosition(float textSize) { | |
| 155 | - | return (float)3.5 * textSize + getParticleRadius(textSize); | |
| 156 | - | } | |
| 157 | - | ||
| 158 | - | private static void setHeiban(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras) { | |
| 159 | - | text.add(new Pair<>(moras.get(0), false)); | |
| 160 | - | for(int i=1; i<moras.size(); i++) { | |
| 161 | - | text.add(new Pair<>(moras.get(i), true)); | |
| 162 | - | } | |
| 163 | - | } | |
| 164 | - | ||
| 165 | - | private static void setAtamadaka(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras) { | |
| 166 | - | text.add(new Pair<>(moras.get(0), true)); | |
| 167 | - | for(int i=1; i<moras.size(); i++) { | |
| 168 | - | text.add(new Pair<>(moras.get(i), false)); | |
| 169 | - | } | |
| 170 | - | } | |
| 171 | - | ||
| 172 | - | private static void setOtherAccent(ArrayList<Pair<String, Boolean>> text, ArrayList<String> moras, int accent) { | |
| 173 | - | text.add(new Pair<>(moras.get(0), false)); | |
| 174 | - | int i; | |
| 175 | - | for(i=1; i<moras.size() && i < accent; i++) { | |
| 176 | - | text.add(new Pair<>(moras.get(i), true)); | |
| 177 | - | } | |
| 178 | - | for(; i<moras.size(); i++) { | |
| 179 | - | text.add(new Pair<>(moras.get(i), false)); | |
| 180 | - | } | |
| 181 | - | } | |
| 182 | - | ||
| 183 | - | @Override | |
| 184 | - | protected void onDraw(Canvas canvas) { | |
| 185 | - | super.onDraw(canvas); | |
| 186 | - | if(pitchedMora == null || pitchedMora.size() == 0) | |
| 187 | - | return; | |
| 188 | - | ||
| 189 | - | paint.setStrokeWidth(getParticleRadius(textSize)); | |
| 190 | - | paint.setTextSize(textSize); | |
| 191 | - | boolean previous = true; | |
| 192 | - | ||
| 193 | - | for(int i = 0; i< pitchedMora.size(); i++) { | |
| 194 | - | paint.setColor(pitchColor); | |
| 195 | - | Pair<String, Boolean> mora = pitchedMora.get(i); | |
| 196 | - | paint.setStyle(mora.first==null? Paint.Style.STROKE: Paint.Style.FILL_AND_STROKE); | |
| 197 | - | float x = getX(i, textSize); | |
| 198 | - | float y = getY(mora.second, textSize); | |
| 199 | - | canvas.drawCircle(x, y, getPitchRadius(textSize), paint); | |
| 200 | - | ||
| 201 | - | if(i>0) { | |
| 202 | - | paint.setStyle(Paint.Style.STROKE); | |
| 203 | - | paint.setStrokeWidth(getStrokeWidth(textSize)); | |
| 204 | - | double radius = getPitchRadius(textSize) + getParticleRadius(textSize)/2; | |
| 205 | - | drawLine(canvas, paint, i-1, previous, mora.second, radius, textSize); | |
| 206 | - | paint.setStrokeWidth(getParticleRadius(textSize)); | |
| 207 | - | } | |
| 208 | - | ||
| 209 | - | if(mora.first != null) { | |
| 210 | - | paint.setColor(textColor); | |
| 211 | - | float width = paint.measureText(mora.first); | |
| 212 | - | paint.setStyle(Paint.Style.FILL); | |
| 213 | - | canvas.drawText(mora.first, x - width/2, getMoraPosition(textSize), paint); | |
| 214 | - | } | |
| 215 | - | ||
| 216 | - | previous = mora.second; | |
| 217 | - | } | |
| 218 | - | } | |
| 219 | - | ||
| 220 | - | private static void drawLine(Canvas canvas, Paint paint, int position, boolean a, boolean b, double radius, float textSize) { | |
| 221 | - | float startX = getX(position, textSize); | |
| 222 | - | float startY = getY(a, textSize); | |
| 223 | - | float endX = getX(position + 1, textSize); | |
| 224 | - | float endY = getY(b, textSize); | |
| 225 | - | ||
| 226 | - | if(a == b) { | |
| 227 | - | startX += radius; | |
| 228 | - | endX -= radius; | |
| 229 | - | } else { | |
| 230 | - | double dist = Math.sqrt(Math.pow(startX-endX, 2)+Math.pow(startY-endY, 2)); | |
| 231 | - | startY += (endY - startY) * radius / dist; | |
| 232 | - | startX += (endX - startX) * radius / dist; | |
| 233 | - | ||
| 234 | - | endX -= (endX - startX) * radius / dist; | |
| 235 | - | endY -= (endY - startY) * radius / dist; | |
| 236 | - | } | |
| 237 | - | ||
| 238 | - | canvas.drawLine(startX, startY, endX, endY, paint); | |
| 239 | - | } | |
| 240 | - | ||
| 241 | - | private static float getX(int i, float textSize) { | |
| 242 | - | return i*getCharWidth(textSize) + getCharWidth(textSize)/2; | |
| 243 | - | } | |
| 244 | - | ||
| 245 | - | private static float getY(boolean high, float textSize) { | |
| 246 | - | return high? getHighPosition(textSize): getLowPosition(textSize); | |
| 247 | - | } | |
| 248 | - | ||
| 249 | - | @Override | |
| 250 | - | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| 251 | - | super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
| 252 | - | ||
| 253 | - | if(pitchedMora != null) | |
| 254 | - | setMeasuredDimension((int)getX(pitchedMora.size(), textSize), (int)(getMoraPosition(textSize) + textSize/2)); | |
| 255 | - | else | |
| 256 | - | setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); | |
| 257 | - | } | |
| 258 | - | } |
app/src/main/res/layout/activity_help_pitch.xml
| 126 | 126 | android:layout_height="wrap_content" | |
| 127 | 127 | android:background="@android:color/white"> | |
| 128 | 128 | ||
| 129 | - | <eu.lepiller.nani.PitchView | |
| 129 | + | <eu.lepiller.nani.PitchDiagramView | |
| 130 | 130 | android:layout_width="wrap_content" | |
| 131 | 131 | android:layout_height="wrap_content" | |
| 132 | 132 | android:layout_gravity="center" | |
… | |||
| 150 | 150 | android:layout_height="wrap_content" | |
| 151 | 151 | android:background="@color/colorGrey"> | |
| 152 | 152 | ||
| 153 | - | <eu.lepiller.nani.PitchView | |
| 153 | + | <eu.lepiller.nani.PitchDiagramView | |
| 154 | 154 | android:layout_width="wrap_content" | |
| 155 | 155 | android:layout_height="wrap_content" | |
| 156 | 156 | android:layout_gravity="center" | |
… | |||
| 182 | 182 | android:layout_height="wrap_content" | |
| 183 | 183 | android:background="@android:color/white"> | |
| 184 | 184 | ||
| 185 | - | <eu.lepiller.nani.PitchView | |
| 185 | + | <eu.lepiller.nani.PitchDiagramView | |
| 186 | 186 | android:layout_width="wrap_content" | |
| 187 | 187 | android:layout_height="wrap_content" | |
| 188 | 188 | android:layout_gravity="center" | |
… | |||
| 206 | 206 | android:layout_height="wrap_content" | |
| 207 | 207 | android:background="@color/colorGrey"> | |
| 208 | 208 | ||
| 209 | - | <eu.lepiller.nani.PitchView | |
| 209 | + | <eu.lepiller.nani.PitchDiagramView | |
| 210 | 210 | android:layout_width="wrap_content" | |
| 211 | 211 | android:layout_height="wrap_content" | |
| 212 | 212 | android:layout_gravity="center" | |
… | |||
| 230 | 230 | android:layout_height="wrap_content" | |
| 231 | 231 | android:background="@color/colorDarkerGrey"> | |
| 232 | 232 | ||
| 233 | - | <eu.lepiller.nani.PitchView | |
| 233 | + | <eu.lepiller.nani.PitchDiagramView | |
| 234 | 234 | android:layout_width="wrap_content" | |
| 235 | 235 | android:layout_height="wrap_content" | |
| 236 | 236 | android:layout_gravity="center" | |
… | |||
| 260 | 260 | android:layout_height="wrap_content" | |
| 261 | 261 | android:background="@android:color/white"> | |
| 262 | 262 | ||
| 263 | - | <eu.lepiller.nani.PitchView | |
| 263 | + | <eu.lepiller.nani.PitchDiagramView | |
| 264 | 264 | android:layout_width="wrap_content" | |
| 265 | 265 | android:layout_height="wrap_content" | |
| 266 | 266 | android:layout_gravity="center" | |
… | |||
| 284 | 284 | android:layout_height="wrap_content" | |
| 285 | 285 | android:background="@color/colorGrey"> | |
| 286 | 286 | ||
| 287 | - | <eu.lepiller.nani.PitchView | |
| 287 | + | <eu.lepiller.nani.PitchDiagramView | |
| 288 | 288 | android:layout_width="wrap_content" | |
| 289 | 289 | android:layout_height="wrap_content" | |
| 290 | 290 | android:layout_gravity="center" | |
… | |||
| 308 | 308 | android:layout_height="wrap_content" | |
| 309 | 309 | android:background="@color/colorDarkGrey"> | |
| 310 | 310 | ||
| 311 | - | <eu.lepiller.nani.PitchView | |
| 311 | + | <eu.lepiller.nani.PitchDiagramView | |
| 312 | 312 | android:layout_width="wrap_content" | |
| 313 | 313 | android:layout_height="wrap_content" | |
| 314 | 314 | android:layout_gravity="center" | |
… | |||
| 332 | 332 | android:layout_height="wrap_content" | |
| 333 | 333 | android:background="@color/colorDarkerGrey"> | |
| 334 | 334 | ||
| 335 | - | <eu.lepiller.nani.PitchView | |
| 335 | + | <eu.lepiller.nani.PitchDiagramView | |
| 336 | 336 | android:layout_width="wrap_content" | |
| 337 | 337 | android:layout_height="wrap_content" | |
| 338 | 338 | android:layout_gravity="center" | |
… | |||
| 362 | 362 | android:background="@android:color/white" | |
| 363 | 363 | android:orientation="vertical"> | |
| 364 | 364 | ||
| 365 | - | <eu.lepiller.nani.PitchView | |
| 365 | + | <eu.lepiller.nani.PitchDiagramView | |
| 366 | 366 | android:layout_width="wrap_content" | |
| 367 | 367 | android:layout_height="wrap_content" | |
| 368 | 368 | android:layout_gravity="center" | |
… | |||
| 387 | 387 | android:layout_height="wrap_content" | |
| 388 | 388 | android:background="@color/colorGrey"> | |
| 389 | 389 | ||
| 390 | - | <eu.lepiller.nani.PitchView | |
| 390 | + | <eu.lepiller.nani.PitchDiagramView | |
| 391 | 391 | android:layout_width="wrap_content" | |
| 392 | 392 | android:layout_height="wrap_content" | |
| 393 | 393 | android:layout_gravity="center" | |
… | |||
| 411 | 411 | android:layout_height="wrap_content" | |
| 412 | 412 | android:background="@color/colorDarkGrey"> | |
| 413 | 413 | ||
| 414 | - | <eu.lepiller.nani.PitchView | |
| 414 | + | <eu.lepiller.nani.PitchDiagramView | |
| 415 | 415 | android:layout_width="wrap_content" | |
| 416 | 416 | android:layout_height="wrap_content" | |
| 417 | 417 | android:layout_gravity="center" | |
… | |||
| 435 | 435 | android:layout_height="wrap_content" | |
| 436 | 436 | android:background="@color/colorDarkGrey"> | |
| 437 | 437 | ||
| 438 | - | <eu.lepiller.nani.PitchView | |
| 438 | + | <eu.lepiller.nani.PitchDiagramView | |
| 439 | 439 | android:layout_width="wrap_content" | |
| 440 | 440 | android:layout_height="wrap_content" | |
| 441 | 441 | android:layout_gravity="center" | |
… | |||
| 459 | 459 | android:layout_height="wrap_content" | |
| 460 | 460 | android:background="@color/colorDarkerGrey"> | |
| 461 | 461 | ||
| 462 | - | <eu.lepiller.nani.PitchView | |
| 462 | + | <eu.lepiller.nani.PitchDiagramView | |
| 463 | 463 | android:layout_width="wrap_content" | |
| 464 | 464 | android:layout_height="wrap_content" | |
| 465 | 465 | android:layout_gravity="center" | |
app/src/main/res/values/attrs.xml
| 1 | 1 | <?xml version="1.0" encoding="utf-8"?> | |
| 2 | 2 | <resources> | |
| 3 | - | <declare-styleable name="PitchView"> | |
| 3 | + | <declare-styleable name="PitchDiagramView"> | |
| 4 | 4 | <attr name="textColor" format="color" /> | |
| 5 | 5 | <attr name="pitchColor" format="color" /> | |
| 6 | 6 | <attr name="text" format="string" /> |