Android View 滑屏初探

来源:互联网 发布:2017网络热门词汇 编辑:程序博客网 时间:2024/05/21 09:59


效果就是类似launcher, 可以左右滑动一屏。

思路:

自定义ViewGroup,  从左到右摆放子View, 每个子View的宽高等于屏幕宽高, 滑动时通过scrollTo / scrollBy移动内容来显示每一子View(每个子View占满一屏), 做到切换的效果。


public class MultiViewGroup extends ViewGroup {private Context mContext;private static String TAG = "MultiViewGroup";public MultiViewGroup(Context context) {super(context);mContext = context;init();}public MultiViewGroup(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;init();}private void init() {// 初始化3个 LinearLayout控件LinearLayout oneLL = new LinearLayout(mContext);oneLL.setBackgroundColor(Color.RED);        addView(oneLL);LinearLayout twoLL = new LinearLayout(mContext);twoLL.setBackgroundColor(Color.YELLOW);addView(twoLL);LinearLayout threeLL = new LinearLayout(mContext);threeLL.setBackgroundColor(Color.BLUE);addView(threeLL);}// measure过程@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {Log.i(TAG, "--- start onMeasure --");// 设置该ViewGroup的大小int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(width, height);int childCount = getChildCount();Log.i(TAG, "--- onMeasure childCount is -->" + childCount);for (int i = 0; i < childCount; i++) {View child = getChildAt(i);// 设置每个子视图的大小 , 即全屏child.measure(MultiScreenActivity.screenWidth, MultiScreenActivity.scrrenHeight);}}// layout过程@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubLog.i(TAG, "--- start onLayout --");int startLeft = 0; // 每个子视图的起始布局坐标int startTop = 10; // 间距设置为10px 相当于 android:marginTop= "10px"int childCount = getChildCount();Log.i(TAG, "--- onLayout childCount is -->" + childCount);for (int i = 0; i < childCount; i++) {View child = getChildAt(i);child.layout(startLeft, startTop, startLeft + MultiScreenActivity.screenWidth, startTop + MultiScreenActivity.scrrenHeight);startLeft = startLeft + MultiScreenActivity.screenWidth ; //校准每个子View的起始布局位置//三个子视图的在屏幕中的分布如下 [0 , 320] / [320,640] / [640,960]}}}


布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent" android:id="@+id/tv_hello"android:layout_height="wrap_content" android:text="@string/hello" /><com.qin.scrollerview.MultiViewGroupandroid:id="@+id/mymultiViewGroup" android:layout_below="@id/tv_hello"android:layout_height="wrap_content" android:layout_width="wrap_content"></com.qin.scrollerview.MultiViewGroup><Button android:id="@+id/bt_scrollLeft"android:layout_alignParentBottom="true" android:layout_width="200dip"android:layout_height="wrap_content" android:text="Prev" /><Button android:id="@+id/bt_scrollRight" android:layout_width="200dip"android:layout_toRightOf="@id/bt_scrollLeft"android:layout_alignParentBottom="true" android:layout_height="wrap_content"android:text="Next" /></RelativeLayout>

Activity:

//带有可以切换屏的Activitypublic class MultiScreenActivity extends Activity implements OnClickListener {private Button bt_scrollLeft;private Button bt_scrollRight;private MultiViewGroup mulTiViewGroup  ;public static int screenWidth  ;  // 屏幕宽度public static int scrrenHeight ;  //屏幕高度private int curscreen = 0;   // 当前位于第几屏幕  ,共3个"屏幕", 3个LinearLayoutprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);        //获得屏幕分辨率大小DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenWidth = metric.widthPixels ;scrrenHeight = metric.heightPixels;System.out.println("screenWidth * scrrenHeight --->" + screenWidth + " * " +scrrenHeight);setContentView(R.layout.multiview);         //获取自定义视图的空间引用mulTiViewGroup = (MultiViewGroup)findViewById(R.id.mymultiViewGroup);bt_scrollLeft = (Button) findViewById(R.id.bt_scrollLeft);bt_scrollRight = (Button) findViewById(R.id.bt_scrollRight);bt_scrollLeft.setOnClickListener(this);bt_scrollRight.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.bt_scrollLeft:if(curscreen > 0) {  //防止屏幕越界    curscreen -- ;    Toast.makeText(MultiScreenActivity.this, "第" +(curscreen+1) + "屏", 300).show();}elseToast.makeText(MultiScreenActivity.this, "当前已是第一屏",300).show();mulTiViewGroup.scrollTo(curscreen * screenWidth , 0);break;case R.id.bt_scrollRight:if (curscreen < 2 ){ //防止屏幕越界curscreen ++ ;Toast.makeText(MultiScreenActivity.this, "第" + (curscreen+1) + "屏", 300).show();}elseToast.makeText(MultiScreenActivity.this, "当前已是最后一屏",300).show();mulTiViewGroup.scrollTo(curscreen * screenWidth, 0);break;}}}


0 0