public class MyScrollView_1Activity extends Activity { int[] ids = new int[] { R.drawable.a1, R.drawable.a3, R.drawable.a4, R.drawable.a5}; private MyScrollView scrollView; private Context ctx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); ctx = this; scrollView = new MyScrollView(this); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, -1); for (int i = 0; i < ids.length; i++) { ImageView iv = new ImageView(this); iv.setBackgroundResource(ids[i]); scrollView.addView(iv, params); } setContentView(scrollView); } }
public class MyScrollView extends ViewGroup { private GestureDetector detector; private MyScrollView_1Activity ctx; private Scroller scroller; public MyScrollView(Context context) { super(context); this.ctx = (MyScrollView_1Activity) context; initView(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { for (int i = 0; i < getChildCount(); i++) { View view = getChildAt(i); view.layout(1 + i * getWidth(), t, r + i * getWidth(), b); } } private void initView() { scroller = new Scroller(ctx); detector = new GestureDetector(new OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { scrollBy((int) distanceX, 0); return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } }); } @Override public boolean onTouchEvent(MotionEvent event) { detector.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: moveToDest(); break; } return true; } public void moveToDest() { int dest = (getScrollX() + getWidth() / 2) / getWidth(); moveToDest(dest); } private void moveToDest(int dest) { int distance = dest * getWidth() - getScrollX(); scroller.startScroll(getScrollX(), 0, distance, 0); invalidate(); } @Override public void computeScroll() { if (scroller.computeScrollOffset()) { scrollTo(scroller.getCurrX(), 0); postInvalidate(); } } }創(chuàng)新互聯(lián)公司長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為浦城企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、成都做網(wǎng)站,浦城網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。