通过WindowManager图片切换的效果

最近为这个事情焦头烂额,原因无他。原来打算是把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

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

图片切换的效果

效果 源码 https://github.com/YouXianMing/Animations // // LiveImageView.h // Animations // // Created by YouXianMing on 15/12/27. // Copyright © 2015年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface LiveImageView : UIImageView @p

图片切换特效(1):原生JS图片切换效果

转自:http://www.codefans.net/jscss/code/4699.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

JS图片切换效果

源地址:http://www.codefans.net/jscss/code/4699.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&g

js实现图片切换效果

用js实现点击按钮,图片切换的效果: 1 <div class="box" id="box"> 2 <div class="img_box" id="img_box"> 3 <img src="../raw/b1.jpg" class="image" > 4 <img src="../raw/b2.jpg" class=&qu

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

详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的banner图片切换效果. 写法很简单,网页初学者们或者wenbapp初学者们可以拿去学习学习,也可以在这基础上加上手机的图片滑动切换效果.废话不多说,下面说说这个简单的图片切换的效果吧! 首先以下是图片切换的效果图:   图片切换效果html内容: <div class="tyna2"&

2016/1/28 codes(slicebox-3d 图片切换index1.html)

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Slicebox-3d 图片切换立体效果</title> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name

基于原生js的图片轮播效果简单实现

基本效果如下: 实现了三张图片自动轮播+按键点击切换的效果. 基本思想: 图片轮播的效果和老式电影院的胶片放映形式很相似,在老式的电影院放映中,使用长长的胶片记录影片,胶片上是按顺序排列的一系列图片,通过快速通过放映机放映口,使这些图片产生一个连贯的切换效果,形成了动态的影片.所以,这里图片轮播的形式也可以采用这种方式来形成动画效果. 形式如下图: (黑框即我们的最外层的容器,充当放映机的存在:绿框就是胶片,上面搭载着很多的图片:粉框内即我们要轮播的图片) 从上图出发,我们要做到图片轮播,那么只

jQuery演示10种不同的切换图片列表动画效果

经常用到的图片插件演示jQuery十种不同的切换图片列表动画效果 在线演示 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 <!DOCTYPE html> <html lang="en" class="no-