1

我正在尝试构建一个钢琴应用程序,我已经通过这个链接

我能够创建绘制一系列黑白键的视图。我不知道如何水平滚动,因为我没有为视图使用任何 xml 文件。

这是我的 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;
}
}

这是 MyTouch 的代码: 与上述链接中指定的代码相同。

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);
        }
    }
}

};

我搜索了谷歌,并尝试了很多例子,但没有一个对我有用。这个视图的更多内容包含一个绘制的画布,我对这个画布和绘图很陌生!

4

2 回答 2

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.

于 2013-04-17T06:46:26.893 回答
0

这是水平滚动的最终视图..

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);
}
}

感谢Rajesh的回复。我希望这对某人有用....

于 2013-04-17T07:11:38.000 回答