扩展视图不滚动

时间:2013-04-17 06:30:04

标签: android view scroll

我正在尝试建立一个钢琴应用程序,我已经完成了this link

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

以下是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;
}
}

以下是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);
        }
    }
}

};

我搜索了Google,并尝试了很多例子,但是没有一个能为我工作。更多关于此视图的内容包含绘制的画布,而且我对此画布和图纸不熟悉!

2 个答案:

答案 0 :(得分: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.
    }
}

这将允许您垂直滚动。如果要水平滚动,请考虑touchX值更改。

答案 1 :(得分: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的回复。我希望这对某人有用....