編輯:關於Android編程
一.ViewPager頁面滑動切換實現:
借鑒了別人的源碼,還是比較容易實現的,而且這種效果還是經常使用的,特此記錄一下:
效果圖:
主界面:
public class MainActivity extends Activity { private ViewPager mPager;// 頁卡內容 private ListlistViews; // Tab頁面列表 private ImageView cursor;// 動畫圖片 private TextView t1, t2, t3;// 頁卡頭標 private int offset = 0;// 動畫圖片偏移量 private int currIndex = 0;// 當前頁卡編號 private int bmpW;// 動畫圖片寬度 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_main); InitImageView(); InitTextView(); InitViewPager(); } // 初始選項 private void InitTextView() { t1 = (TextView) findViewById(R.id.text1); t2 = (TextView) findViewById(R.id.text2); t3 = (TextView) findViewById(R.id.text3); t1.setOnClickListener(new MyOnClickListener(0)); t2.setOnClickListener(new MyOnClickListener(1)); t3.setOnClickListener(new MyOnClickListener(2)); } // 初始化ViewPager private void InitViewPager() { mPager = (ViewPager) findViewById(R.id.vPager); listViews = new ArrayList (); LayoutInflater mInflater = getLayoutInflater(); listViews.add(mInflater .inflate(R.layout.activity_tab_one_pager, null)); listViews.add(mInflater .inflate(R.layout.activity_tab_two_pager, null)); listViews.add(mInflater .inflate(R.layout.activity_tab_three_pager, null)); mPager.setAdapter(new MyViewPagerAdapter(listViews, this)); //默認選中第一個頁面 mPager.setCurrentItem(0); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); } // 初始化動畫 private void InitImageView() { cursor = (ImageView) findViewById(R.id.cursor); bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.icon_tab_page_bg).getWidth();// 獲取圖片寬度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels;// 獲取分辨率寬度 offset = (screenW / 3 - bmpW) / 2;// 計算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); cursor.setImageMatrix(matrix);// 設置動畫初始位置 } // 頁卡監聽 public class MyOnClickListener implements View.OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } public void onClick(View v) { mPager.setCurrentItem(index); } }; // 滑動監聽 public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量 int two = one * 2;// 頁卡1 -> 頁卡3 偏移量 //arg0表示頁數 public void onPageSelected(int arg0) { // TranslateAnimation位移動畫 Animation animation = null; //表示從第一個頁卡跳轉到當前頁卡 switch (arg0) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, 0, 0, 0); } break; case 1: if (currIndex == 0) { animation = new TranslateAnimation(offset, one, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, one, 0, 0); } break; case 2: if (currIndex == 0) { animation = new TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1) { animation = new TranslateAnimation(one, two, 0, 0); } break; } currIndex = arg0; animation.setFillAfter(true);// true:表示圖片停在動畫結束位置 animation.setDuration(300); cursor.startAnimation(animation); } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageScrollStateChanged(int arg0) { } } }
public class MyViewPagerAdapter extends PagerAdapter { private ListmListViews; private Context context; public MyViewPagerAdapter(List mListViews, Context context) { this.mListViews = mListViews; this.context = context; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(mListViews.get(arg1)); } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { return mListViews.size(); } @Override public Object instantiateItem(View arg0, int arg1) { if (arg1 < 3) { ((ViewPager) arg0).addView(mListViews.get(arg1 ), 0); } // 頁卡內點擊事件 if (arg1 == 0) { Button btn = (Button) arg0.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(1); } }); } if (arg1 == 1) { Button btn = (Button) arg0.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(2); } }); } if (arg1 == 2) { Button btn = (Button) arg0.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(3); } }); } return mListViews.get(arg1); } public void showDialog(String arg) { new AlertDialog.Builder(context).setTitle(說明) .setMessage(這是第+arg+個頁面) .setNegativeButton(確定, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } }
大家可能在做app的時候,或多或少需要使用聯系人,而根據google提供的api,你需要編寫大量的代碼,例如首先需要查詢數據庫,涉及到數據庫表和字段以及對應的SQL語句。
一、 View 的基礎知識View 是 Android 中所有空間的基類。1、 View 的位置參數View 的位置主要有四個頂點決定的, top、left、right、
最近接了一個項目其中有功能要實現一個清理內存,要求和微信的效果一樣。於是想到用surfaceView而不是繼承view。下面小編給大家解析下實現思路。surfaceVie
1.視圖 其實就是View 視圖組由多個視圖組成2.控件 常用控件包括button,TextView,EditView,ListView等,所以的控件都繼承與View,都