淘宝:模拟实现帮助提示页面
http://dl.iteye.com/upload/attachment/0062/3517/3e3c6a75-d36e-3fdf-a97e-29b3d01cc2bc.jpg先看效果:如上
FlowActivity.java
package com.mars;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class FlowPicActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ScrollView sv = (ScrollView) findViewById(R.id.scroll);ImageView iv = new ImageView(this);iv.setImageDrawable(this.getResources().getDrawable(R.drawable.a1));sv.addView(iv);sv.addView(View.inflate(this, R.layout.layout_0, null));ImageView iv2 = new ImageView(this);iv2.setImageDrawable(getResources().getDrawable(R.drawable.a2));sv.addView(iv2);PageControlView pageControl = (PageControlView) findViewById(R.id.pageControl);sv.setOnScreenChangeListener(pageControl);sv.initPageControlView();}}
PageControlView.java
package com.mars;import com.mars.ScrollView.OnScreenChangeListener;import android.content.Context;import android.util.AttributeSet;import android.widget.ImageView;import android.widget.LinearLayout;public class PageControlView extends LinearLayout implementsOnScreenChangeListener {private Context context;public PageControlView(Context context) {super(context);this.context = context;}public PageControlView(Context context, AttributeSet attr) {super(context, attr);this.context = context;}@Overridepublic void screenChange(int currentTab, int totalTab) {this.removeAllViews();for (int i = 0; i < totalTab; i++) {ImageView iv = new ImageView(context);if (i == currentTab) {iv.setImageResource(R.drawable.ball_blue);} else {iv.setImageResource(R.drawable.ball_gray);}this.addView(iv);}}}
3.ScrollView.java
package com.mars;import android.content.Context;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.widget.Scroller;public class ScrollView extends ViewGroup {private GestureDetector gesture;private Context context;private boolean fling;private Scroller scroller;private OnScreenChangeListener onScreenChangeListener;public ScrollView(Context context) {super(context);this.context = context;gesture = new GestureDetector(context, new GestureListener());scroller = new Scroller(context);}public ScrollView(Context context, AttributeSet att) {super(context, att);this.context = context;gesture = new GestureDetector(context, new GestureListener());scroller = new Scroller(context);}protected void onLayout(boolean changed, int l, int t, int r, int b) {for (int i = 0; i < getChildCount(); i++) {View child = (View) getChildAt(i);child.measure(r - l, b - t);child.layout(getWidth() * i, 0, getWidth() * (i + 1), getHeight());}}public boolean onTouchEvent(MotionEvent ev) {gesture.onTouchEvent(ev);switch (ev.getAction()) {case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:System.out.println("action_up...");if (!fling) {scrollToScreen();}fling = false;break;}return true;}private void scrollToScreen() {int leftWidth = getScrollX();int tabs = leftWidth / getWidth();int len = leftWidth - tabs * getWidth();if (len < getWidth() / 2) {} else {scroller.startScroll(leftWidth, 0, getWidth() - len, 0, len * 2);tabs = tabs + 1;}if (onScreenChangeListener != null) {onScreenChangeListener.screenChange(tabs, getChildCount());}invalidate();}public void computeScroll() {if (scroller.computeScrollOffset()) {scrollTo(scroller.getCurrX(), 0);postInvalidate();}}class GestureListener extends SimpleOnGestureListener {public boolean onDoubleTap(MotionEvent e) {return super.onDoubleTap(e);}public boolean onDown(MotionEvent e) {return super.onDown(e);}public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {if (Math.abs(velocityX) > ViewConfiguration.get(context).getScaledMinimumFlingVelocity()) {scrollToScreen();fling = true;}return true;}public void onShowPress(MotionEvent e) {super.onShowPress(e);}public void onLongPress(MotionEvent e) {super.onLongPress(e);}public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {if (distanceX > 0&& getScrollX() < (getChildCount() - 1) * getWidth()|| distanceX < 0 && getScrollX() > 0) {scrollBy((int) distanceX, 0);}return true;}public boolean onSingleTapUp(MotionEvent e) {return super.onSingleTapUp(e);}}public interface OnScreenChangeListener {void screenChange(int currentTab, int totalTab);}public void setOnScreenChangeListener(OnScreenChangeListener onScreenChangeListener) {this.onScreenChangeListener = onScreenChangeListener;}public void initPageControlView() {if (onScreenChangeListener != null) {onScreenChangeListener.screenChange(0, getChildCount());}}}
xml文件自己去想吧,给个main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.mars.ScrollView android:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <com.mars.PageControlView android:id="@+id/pageControl" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:background="#8f00000f" android:gravity="center" /></RelativeLayout>
页:
[1]