1

Im trying to build a piano app, i have gone through this link

i was able to create view which draws series of black and white keys.I dont know how to scroll horizontally, Since iam not using any xml file for the view.

Here is the code for My MainActivity:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyTouch(this));
//setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Here is the code for MyTouch: the same code specified in the above link.

public class MyTouch extends View {
public MyTouch(Context context) {
    super(context);
}
Bitmap whiteKey, blackKey;
Paint paint = new Paint();

public void draw(Canvas canvas) {
    if (whiteKey == null) {
        whiteKey = BitmapFactory.decodeResource(getResources(), R.drawable.white);
    }
    if (blackKey == null) {
        blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.black);
    }

    int keys = 5;

    // draw white keys
    for (int i = 0; i < keys; i++) {
        canvas.drawBitmap(whiteKey, i * whiteKey.getWidth(), 0, paint);
    }
    // draw black keys
    for (int i = 0; i < keys; i++) {
        if (i != 3 && i != 7) {
            canvas.drawBitmap(blackKey, i * blackKey.getWidth()+blackKey.getWidth()*0.5f, 0, paint);
        }
    }
}

};

I have searched Google,and tried many examples, but none of them is working for me.More over this view contains a drawn canvas, and iam new to this canvas and drawings !!

Community
  • 1
  • 1
checki big
  • 45
  • 2
  • 10

2 Answers2

1
class MyScrollView extends View
{
    int mTouchSlop;
    public MyScrollView(Context context) {
        super(context);
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop                            = configuration.getScaledTouchSlop();
        // TODO Auto-generated constructor stub
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    float mLastMotionY;
    boolean mIsScrolling;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        final int action = ev.getAction();
        final float y = ev.getY();
        switch (action) {
        case MotionEvent.ACTION_DOWN: 
        {
            mLastMotionY = y;
            mIsScrolling = false;
            break;
        }

        case MotionEvent.ACTION_MOVE: 
        {
            final int deltaY = (int) (mLastMotionY - y);
            mIsScrolling = (!mIsScrolling) ? (Math.abs(deltaY) > mTouchSlop)
                    : mIsScrolling;

            if ( mIsScrolling ) 
            {
                scrollBy(0, deltaY);
                mLastMotionY = y;
            }
            break;
        }

        case MotionEvent.ACTION_UP: 
        {
            mIsScrolling = false;
            break;
        }
        case MotionEvent.ACTION_CANCEL: {
            break;
        }
        }
        invalidate();
        return true;
    }

    @Override
    public void scrollBy(int x, int y) {
        // TODO Auto-generated method stub
        super.scrollBy(x, y);//You can validate the scroll set the minimum and maximum scroll.
    }
}

This will allow you to scroll in vertically. If you want to scroll horizontally consider the touchX values changes.

Triode
  • 11,309
  • 2
  • 38
  • 48
0

Here is the final view which scrolls Horizontally..

class MyTouch extends View
{
int mTouchSlop;
public MyTouch(Context context) {
   super(context);
   final ViewConfiguration configuration = ViewConfiguration.get(getContext());
   mTouchSlop                            = configuration.getScaledTouchSlop();
}


@Override
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
}

Bitmap whiteKey, blackKey;
Paint paint = new Paint();
public void draw(Canvas canvas) {
   if (whiteKey == null) {
       whiteKey = BitmapFactory.decodeResource(getResources(), R.drawable.white);
   }
   if (blackKey == null) {
       blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.black);
   }

   int keys = 10;

   // draw white keys
   for (int i = 0; i < keys; i++) {
       canvas.drawBitmap(whiteKey, i * whiteKey.getWidth(), 0, paint);
   }
   // draw black keys
   for (int i = 0; i < keys; i++) {
       if (i != 3 && i != 7) {
           canvas.drawBitmap(blackKey, i * blackKey.getWidth()+blackKey.getWidth()*0.5f, 0, paint);
       }
   }
 }



 float mLastMotionX;
 boolean mIsScrolling;
 @Override
 public boolean onTouchEvent(MotionEvent ev) {

   final int action = ev.getAction();
   final float x = ev.getX();
   switch (action) {
   case MotionEvent.ACTION_DOWN: 
   {
       mLastMotionX = x;
       mIsScrolling = false;
       break;
   }

   case MotionEvent.ACTION_MOVE: 
   {
       final int deltaX = (int) (mLastMotionX - x);
       mIsScrolling = (!mIsScrolling) ? (Math.abs(deltaX) > mTouchSlop)
               : mIsScrolling;

       if ( mIsScrolling ) 
       {
           scrollBy(deltaX,0);
           mLastMotionX = x;
       }
       break;
   }

   case MotionEvent.ACTION_UP: 
   {
       mIsScrolling = false;
       break;
   }
   case MotionEvent.ACTION_CANCEL: {
       break;
   }
   }
   invalidate();
   return true;
 }

@Override
public void scrollBy(int x, int y) {
   super.scrollBy(x, y);
}
}

Thanks to Rajesh for this reply.I hope this would be useful for someone....

Community
  • 1
  • 1
checki big
  • 45
  • 2
  • 10