最近为这个事情焦头烂额,原因无他。原来打算是把ViewPager放在WindowManager中,再设定一个定时器,让图片自动切换,但是搞了很久,发现无论如何,这个图片只显示一张。虽然日志看得出来图片确实在切换,但是除了这初始化的这张图片之外,其他的都是黑窗口,也就是没有显示出来。

此路不通,另找出路。其实未必用得着PagerAdapter。安卓就是这点方便,反正View,不管咋整,都是可以往窗口里面放的,我对界面不懂,所以这里也说不出个所以然来。但是这种需求,估计也是有的(比如有些比较恶心的广告程序,就是想要独占最上层,那用windowManager就是比较合适的方式)。

废话不多说,直接上代码

 public class MainActivity extends Activity {
private Runnable viewpagerRunnable;
private Handler MainThreadhandler;
private WindowManager mWndMgr;
private ViewPager mViewPager;
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();
prepareImageView();
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 prepareImageView(){
mViewPager = 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;
//mViewPager.setLayoutParams(mLayoutParams);
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);
}
//mViewPager.setAdapter(new ViewAdapha(mImgViewList));
} public void initView(){
//mWndMgr.addView(mViewPager, mLayoutParams);
mImgView = mImgViewList.get(CurrentIndex);
mImgView.setLayoutParams(mLayoutParams);
mWndMgr.addView(mImgView, mLayoutParams);
//mWndMgr.updateViewLayout(mImgView, mLayoutParams);
} private void startAutomaticRoll() {
viewpagerRunnable = new Runnable() {
@Override
public void run() {
//int nowIndex = mViewPager.getCurrentItem();
//int count = mViewPager.getAdapter().getCount();
if (CurrentIndex + 1 >= 3) {
//mViewPager.setCurrentItem(0);
CurrentIndex = 0;
} else {
CurrentIndex = CurrentIndex + 1;
}
mWndMgr.removeView(mImgView);
mImgView = mImgViewList.get(CurrentIndex);
mImgView.setLayoutParams(mLayoutParams);
//mViewPager.removeAllViews();
Log.i("index", "index="+CurrentIndex);
//mViewPager.removeAllViews();
//mViewPager.addView(mImgViewList.get(CurrentIndex));
mWndMgr.addView(mImgView, mLayoutParams);
//mWndMgr.updateViewLayout(mImgView, mLayoutParams);
MainThreadhandler.postDelayed(viewpagerRunnable, 3000);
}
};
MainThreadhandler.postDelayed(viewpagerRunnable, 3000);
}
}

这种方式比较戳,在图片切换之间,会有闪断的现象,也就是切换的不平滑。如果有知道的朋友,还请指点下是什么情况。或者是如何才能做到在我这种方式下,把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);
}
}

  

通过WindowManager图片切换的效果的更多相关文章

  1. JQuery图片切换动画效果

    由于博主我懒,所以页面画的比较粗糙,但是没关系,因为我主要讲的是如何实现图片动画切换. 思路:想必大家都逛过淘宝或者其他的一些网站,一般都会有图片动画切换的效果,那是怎样实现的呢?博主我呢,技术不是很 ...

  2. 自动播放——幻灯片缓冲效果&&带Loading效果的图片切换&&移动效果(按轨迹移动)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JS图片切换效果

    源地址:http://www.codefans.net/jscss/code/4699.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  4. jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发

    详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的ban ...

  5. 移动web:图片切换(焦点图)

    在web页面上图片切换(焦点图)效果实在是太常见了,PC端.移动端到处都有它的身影. 上次写了个tab选项卡的效果,在这里延续一下,改成图片切换的效果. 如果不需要自动播放,稍微修改下html标签.和 ...

  6. app引导页(背景图片切换加各个页面动画效果)

    前言:不知不觉中又加班到了10点半,整个启动页面做了一天多的时间,一共有三个页面,每个页面都有动画效果,动画效果调试起来麻烦,既要跟ios统一,又要匹配各种不同的手机,然后产品经理还有可能在中途改需求 ...

  7. Android中使用ImageViewSwitcher实现图片切换轮播导航效果

    前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接: Android中使用ViewFlipper实现屏幕切换 Android中使用V ...

  8. js鼠标滚轮滚动图片切换效果

    效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  9. javascript马赛克遮罩图片切换效果:XMosaic.js(转)

    新鲜出炉的javascript图片切换特效,实现的是马赛克遮罩切换.在flash里,好实现遮罩动画很简单,不过JS实现起来就有些困难了. XMosaic.js,与XScroll.js和XScroll2 ...

随机推荐

  1. Using a USB host controller security extension for controlling changes in and auditing USB topology

    Protecting computer systems from attacks that attempt to change USB topology and for ensuring that t ...

  2. Yii createCommand CURD操作

    本文用作工作记录,也许有人会问为什么不用 Yii 的 Model 去操作 DB,原因很简单,Yii 的 Model 写法上是方便了很多,但是会执行多余的 SQL,打开 Yii 的执行 log 就会发现 ...

  3. POJ - 2391 最大流

    题目链接:http://poj.org/problem?id=2391 今天掉坑多次. 做了几道题,发现从源点出来的边和进入汇点的边都在题目中出来过. POJ真是坑,交G++一直wa,检查代码检查了好 ...

  4. SPOJ 1825 Free tour II (树的点分治)

    题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...

  5. Usaco_Contest_2013_Open_Bovine Problem 1. Bovine Ballet

    Problem 1: Bovine Ballet [Brian Dean, 2013] In an attempt to challenge the stereotypical perception ...

  6. 在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤

    ---恢复内容开始--- 1.首先准备一个需要统计的单词文件 word.txt,我们的单词是以空格分开的,统计时按照空格分隔即可 hello hadoop hello yarnhello zookee ...

  7. go 依赖包管理工具gb安装报错

    尝试了下gb工具,发现有个问题: [root@etcd1 test]# go get github.com/constabulary/gb/... /home/gopath/src/github.co ...

  8. myBatis-plus异常提示For input string: "{0=null}"

    异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...

  9. RecyclerView onItemClick button和布局都有单击事件时的处理方式

    RecyclerView为了给开发人员提供更大的自由度.没有默认的提供onItemClick接口. 网上有一种比較简单的实现方式 , 适用于不须要针对item里面某个button做特殊处理的情况 我眼 ...

  10. JrtpLib vs2012环境下编译及使用 GotoFirstSourceWithData 方法 进不去

    项目中有一项功能是接收rtp数据,接收rtp的可以用PJMedia,可以用JrtpLib.PJMedia库无法解决内外网的问题,只有用Jrtp库了. 首先说下Jrtp 的编译问题,我是在windows ...