1 package mhr.appandroid.views;
3 import java.util.ArrayList;
5 import mhr.appandroid.R;
6 import android.content.Context;
7 import android.graphics.Bitmap;
8 import android.graphics.BitmapFactory;
9 import android.graphics.Canvas;
10 import android.graphics.Paint;
11 import android.graphics.Paint.Style;
12 import android.graphics.Path;
13 import android.graphics.Rect;
14 import android.graphics.RectF;
15 import android.graphics.drawable.ShapeDrawable;
16 import android.graphics.drawable.shapes.PathShape;
17 import android.util.AttributeSet;
18 import android.view.GestureDetector;
19 import android.view.MotionEvent;
20 import android.view.View;
21 import android.view.ViewParent;
28 public boolean onDown(MotionEvent e) {
30 int X = (int) e.getX();
31 int Y = (int) e.getY();
33 for (
int i = 0; i <
x.length; i++) {
35 if (
cRect.contains(X, Y)) {
60 public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX,
float distanceY) {
68 y[
selIndex] = (tmpy < 0) ? 0 : (tmpy > 1) ? 1 : tmpy;
71 y[
selIndex] = (tmpy < 0) ? 0 : (tmpy > 1) ? 1 : tmpy;
73 tmpx = (tmpx < 0) ? 0 : (tmpx > 1) ? 1 : tmpx;
75 y[
selIndex] = (tmpy < 0) ? 0 : (tmpy > 1) ? 1 : tmpy;
89 int X = (int) e.getX();
90 int Y = (int) e.getY();
92 for (
int i = 0; i <
x.length; i++) {
94 if (
cRect.contains(X, Y)) {
112 if (selIndex < 1 || selIndex >
x.length - 2) {
116 int ptsCount =
x.length;
117 float[] tx =
new float[ptsCount - 1];
118 float[] ty =
new float[ptsCount - 1];
126 while (i <
x.length) {
133 y2 =
new float[ptsCount - 1];
134 xc1 =
new float[ptsCount - 2];
135 xc2 =
new float[ptsCount - 2];
136 yc1 =
new float[ptsCount - 2];
137 yc2 =
new float[ptsCount - 2];
145 if (xx < 0 || xx > 1 || yy < 0 || yy > 1) {
149 for (
int i = 1; i <
x.length; i++) {
162 int ptsCount =
x.length;
163 float[] tx =
new float[ptsCount + 1];
164 float[] ty =
new float[ptsCount + 1];
173 while (i <
x.length) {
180 y2 =
new float[ptsCount + 1];
181 xc1 =
new float[ptsCount];
182 xc2 =
new float[ptsCount];
183 yc1 =
new float[ptsCount];
184 yc2 =
new float[ptsCount];
217 protected float[]
y2;
245 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
246 cPressed = BitmapFactory.decodeResource(getResources(),
R.drawable.scrubber_control_pressed_bw);
247 cNormal = BitmapFactory.decodeResource(getResources(),
R.drawable.scrubber_control_normal_bw);
252 x =
new float[] {0.0f, 1.0f};
253 y =
new float[] {1.0f, 0.0f};
254 y2 =
new float[ptsCount];
257 xc1 =
new float[ptsCount - 1];
258 xc2 =
new float[ptsCount - 1];
259 yc1 =
new float[ptsCount - 1];
260 yc2 =
new float[ptsCount - 1];
275 frame.setColor(0xFFFFFFFF);
276 frame.setStyle(Style.STROKE);
277 frame.setStrokeWidth(2);
278 frame.setAntiAlias(
true);
293 super(context, attrs);
297 public CurvesView(Context context, AttributeSet attrs,
int defStyle) {
298 super(context, attrs, defStyle);
324 for (
int i = 0; i <
x.length - 1; i++) {
325 p.moveTo(
x[i],
y[i]);
328 curve =
new ShapeDrawable(
new PathShape(p, 1, 1));
329 Paint paint =
curve.getPaint();
330 paint.setStyle(Style.STROKE);
331 paint.setColor(0xFFFFFFFF);
332 paint.setAntiAlias(
true);
339 double p, qn, sig, un;
340 double[] u =
new double[
x.length];
341 int valsCount =
x.length;
342 int lastVal = valsCount - 1;
345 for (i = 1; i < lastVal; i++) {
346 sig = (
x[i] -
x[i - 1]) / (
x[i + 1] -
x[i - 1]);
347 p = sig *
y2[i - 1] + 2.0;
348 y2[i] =(float)((sig - 1.0) / p);
349 u[i] = (
y[i + 1] -
y[i]) / (
x[i + 1] -
x[i])
350 - (
y[i] -
y[i - 1]) / (
x[i] -
x[i - 1]);
351 u[i] = (6.0 * u[i] / (
x[i + 1] -
x[i - 1]) - sig * u[i - 1]) / p;
354 y2[lastVal] =(float) ( (un - qn * u[valsCount - 2]) / (qn *
y2[valsCount - 2] + 1.0));
355 for (k = valsCount - 2; k >= 0; k--) {
356 y2[k] = (float) (
y2[k] *
y2[k + 1] + u[k]);
369 int n =
x.length - 1;
375 for (
int i = 0; i < n; i++) {
377 d0 = (
y[i+1] -
y[i]) / (
x[i+1] -
x[i]) - 2.0/6.0 * (
x[i+1] -
x[i]) *
y2[i] - 1.0/6.0 * (
x[i+1] -
x[i]) *
y2[i+1];
378 d1 = (
y[i+1] -
y[i]) / (
x[i+1] -
x[i]) + 1.0/6.0 * (
x[i+1] -
x[i]) *
y2[i] + 2.0/6.0 * (
x[i+1] -
x[i]) *
y2[i+1];
380 delta =
x[i + 1] -
x[i];
382 yc1[i] = (float) (
y[i] + d0 * (delta) / 3.0);
383 yc2[i] = (float) (
y[i+1] - d1 * (delta) / 3.0);
385 xc1[i] = (float) (x[i] + delta / 3.0);
386 xc2[i] = (float) (x[i+1] - delta / 3.0);
396 float[][] retVal =
new float[2][
x.length];
398 for (
int i = 0; i <
x.length; i++) {
400 retVal[1][i] = 1.0f -
y[i];
407 int ptsCount = pts[0].length;
408 x =
new float[ptsCount];
409 y =
new float[ptsCount];
410 for (
int i = 0; i < ptsCount; i++) {
412 y[i] = 1 - pts[1][i];
415 y2 =
new float[ptsCount];
417 xc1 =
new float[ptsCount - 1];
418 xc2 =
new float[ptsCount - 1];
419 yc1 =
new float[ptsCount - 1];
420 yc2 =
new float[ptsCount - 1];
429 public void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec) {
430 int width = MeasureSpec.getSize(widthMeasureSpec);
431 int height = MeasureSpec.getSize(heightMeasureSpec);
433 if (width < height) {
438 setMeasuredDimension(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
444 super.onSizeChanged(w, h, oldw, oldh);
450 curvesRect =
new Rect(xOffset, yOffset, w - xOffset, h - yOffset);
463 Paint paint =
curve.getPaint();
464 paint.setStrokeWidth(2.0f /
xLength);
468 for (
int i = 0; i <
x.length; i++) {
480 ViewParent parent = getParent();
481 if (parent != null) {
482 parent.requestDisallowInterceptTouchEvent(
true);
484 if (ev.getAction() == MotionEvent.ACTION_UP) {