最近为这个事情焦头烂额,原因无他。原来打算是把ViewPager放在WindowManager中,再设定一个定时器,让图片自动切换,但是搞了很久,发现无论如何,这个图片只显示一张。虽然日志看得出来图片确实在切换,但是除了这初始化的这张图片之外,其他的都是黑窗口,也就是没有显示出来。
此路不通,另找出路。其实未必用得着PagerAdapter。安卓就是这点方便,反正View,不管咋整,都是可以往窗口里面放的,我对界面不懂,所以这里也说不出个所以然来。但是这种需求,估计也是有的(比如有些比较恶心的广告程序,就是想要独占最上层,那用windowManager就是比较合适的方式)。
废话不多说,直接上代码
1 public class MainActivity extends Activity { 2 private Runnable viewpagerRunnable; 3 private Handler MainThreadhandler; 4 private WindowManager mWndMgr; 5 private ViewPager mViewPager; 6 private ImageView mImgView; 7 private WindowManager.LayoutParams mLayoutParams; 8 private Context mContext; 9 private List<ImageView> mImgViewList; 10 private int[] picRes = {R.drawable.first,R.drawable.second,R.drawable.third}; 11 int CurrentIndex = 0; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 mContext = this.getApplicationContext(); 17 MainThreadhandler = new Handler(); 18 prepareImageView(); 19 initView(); 20 startAutomaticRoll(); 21 } 22 23 24 @Override 25 public boolean onCreateOptionsMenu(Menu menu) { 26 // Inflate the menu; this adds items to the action bar if it is present. 27 getMenuInflater().inflate(R.menu.main, menu); 28 return true; 29 } 30 31 @Override 32 public boolean onOptionsItemSelected(MenuItem item) { 33 // Handle action bar item clicks here. The action bar will 34 // automatically handle clicks on the Home/Up button, so long 35 // as you specify a parent activity in AndroidManifest.xml. 36 int id = item.getItemId(); 37 if (id == R.id.action_settings) { 38 return true; 39 } 40 return super.onOptionsItemSelected(item); 41 } 42 43 private void prepareImageView(){ 44 mViewPager = new ViewPager(mContext); 45 mWndMgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 46 DisplayMetrics outMetrics = new DisplayMetrics(); 47 mWndMgr.getDefaultDisplay().getMetrics(outMetrics); 48 mLayoutParams = new WindowManager.LayoutParams(); 49 mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 50 mLayoutParams.width = outMetrics.widthPixels; 51 mLayoutParams.height = outMetrics.heightPixels - 150; 52 //mViewPager.setLayoutParams(mLayoutParams); 53 mImgViewList = new ArrayList<ImageView>(); 54 int len = picRes.length; 55 for(int i = 0; i < len ;i++){ 56 ImageView view = new ImageView(mContext); 57 view.setBackgroundResource(picRes[i]); 58 mImgViewList.add(view); 59 } 60 //mViewPager.setAdapter(new ViewAdapha(mImgViewList)); 61 } 62 63 public void initView(){ 64 //mWndMgr.addView(mViewPager, mLayoutParams); 65 mImgView = mImgViewList.get(CurrentIndex); 66 mImgView.setLayoutParams(mLayoutParams); 67 mWndMgr.addView(mImgView, mLayoutParams); 68 //mWndMgr.updateViewLayout(mImgView, mLayoutParams); 69 } 70 71 private void startAutomaticRoll() { 72 viewpagerRunnable = new Runnable() { 73 @Override 74 public void run() { 75 //int nowIndex = mViewPager.getCurrentItem(); 76 //int count = mViewPager.getAdapter().getCount(); 77 if (CurrentIndex + 1 >= 3) { 78 //mViewPager.setCurrentItem(0); 79 CurrentIndex = 0; 80 } else { 81 CurrentIndex = CurrentIndex + 1; 82 } 83 mWndMgr.removeView(mImgView); 84 mImgView = mImgViewList.get(CurrentIndex); 85 mImgView.setLayoutParams(mLayoutParams); 86 //mViewPager.removeAllViews(); 87 Log.i("index", "index="+CurrentIndex); 88 //mViewPager.removeAllViews(); 89 //mViewPager.addView(mImgViewList.get(CurrentIndex)); 90 mWndMgr.addView(mImgView, mLayoutParams); 91 //mWndMgr.updateViewLayout(mImgView, mLayoutParams); 92 MainThreadhandler.postDelayed(viewpagerRunnable, 3000); 93 } 94 }; 95 MainThreadhandler.postDelayed(viewpagerRunnable, 3000); 96 } 97 }
这种方式比较戳,在图片切换之间,会有闪断的现象,也就是切换的不平滑。如果有知道的朋友,还请指点下是什么情况。或者是如何才能做到在我这种方式下,把ViewPager放到WindowManager里面,再把ImageView数组放在ViewPager里面,然后还能实现正常的切换的效果。
===========================================
又修改了下,效果更好了。哈哈~~~
public class MainActivity extends Activity { private Runnable viewpagerRunnable; private Handler MainThreadhandler; private WindowManager mWndMgr; private ImageView mImgView; private WindowManager.LayoutParams mLayoutParams; private Context mContext; private List<ImageView> mImgViewList; private int[] picRes = {R.drawable.first,R.drawable.second,R.drawable.third}; int CurrentIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this.getApplicationContext(); MainThreadhandler = new Handler(); prepareView(); initView(); startAutomaticRoll(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void prepareView(){ new ViewPager(mContext); mWndMgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); mWndMgr.getDefaultDisplay().getMetrics(outMetrics); mLayoutParams = new WindowManager.LayoutParams(); mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; mLayoutParams.width = outMetrics.widthPixels; mLayoutParams.height = outMetrics.heightPixels - 150; mImgViewList = new ArrayList<ImageView>(); int len = picRes.length; for(int i = 0; i < len ;i++){ ImageView view = new ImageView(mContext); view.setBackgroundResource(picRes[i]); mImgViewList.add(view); } } public void initView(){ mImgView = mImgViewList.get(CurrentIndex); mImgView.setLayoutParams(mLayoutParams); mWndMgr.addView(mImgView, mLayoutParams); } private void startAutomaticRoll() { viewpagerRunnable = new Runnable() { @Override public void run() { if (CurrentIndex + 1 >= 3) { CurrentIndex = 0; } else { CurrentIndex = CurrentIndex + 1; } mImgView.setBackgroundResource(picRes[CurrentIndex]); Log.i("index", "index="+CurrentIndex); MainThreadhandler.postDelayed(viewpagerRunnable, 3000); } }; MainThreadhandler.postDelayed(viewpagerRunnable, 3000); } }
时间: 2024-10-09 15:30:47