android 学习: 背景滚动效果
来源:互联网 发布:云购开奖计算公式 编辑:程序博客网 时间:2024/03/29 14:32
1. 无缝接图, 背景图片上下需要能对接上. 可以想象成一个传送带, 而传送带上有一个小框(手机屏幕) 那么依次从这个小框中过去, 当然, 是一个接着一个的.很容易想到, 手机右上角为 0点坐标, 往下为正,上为负, 那么当向邻近的两个图片, y1 和y2相距一个图片高的距离, 当y1 = 0, 那么 y2 = y1 - heightBitmap, 然后以此为依据,让y1 不断往下移动, 那么y2也会跟着往下移动, 当y1 移出屏幕后,此时y1 可以接着y2 的后面继续往下移动, 若是这样,代码写起来比较麻烦, 那么可以让y1替代y2的位置往下移动,就好像y1 插在了y2的前面,只是y1的位置是y2的,而y2往后挪了一下,于是我们有回到了原来的状态, y2又跟着y1继续往下移动...
2. 原理图
3. Background
/* * 无缝背景滚动效果 */public class Background {private Bitmap bmp;private int x1, x2;private int y1, y2;private int speed;private int widthScreen, heightScreen;private int widthBitmap, heigthtBitmap;public Background(Bitmap bmp, int speed, int widthScreen, int heightScreen) {this.bmp = bmp;widthBitmap = bmp.getWidth();heigthtBitmap = bmp.getHeight();x1 = 0;y1 = 0;x2 = 0;y2 = y1+heigthtBitmap;this.speed = speed;this.widthScreen = widthScreen;this.heightScreen = heightScreen;}/** * 贴图逻辑 * y1初始从坐标0 开始贴图, 慢慢往下移动, y2则补齐y1 往下移动留下的空白. * 当y1 移除到界面时,y1 此时应该从y2 位置的开始不能从0开始, y2继续补齐y1 留下的空白 */public void logic() {//y1 = y1 > heightScreen ? 0 : y1+speed;y1 = y1 > heightScreen ? y2 : y1+speed;y2 = y1- heigthtBitmap;}public void draw(Canvas canvas) {canvas.drawBitmap(bmp, x1, y1, null);canvas.drawBitmap(bmp, x2, y2, null);}}
/** * * @author Luv * */public class MySurfaceView extends SurfaceView implements Callback, Runnable { private SurfaceHolder sHolder; private Canvas canvas; private int widthScreen, heightScreen; private Thread thread; private boolean flag; private Background background;private Resources res; public MySurfaceView(Context context) { super(context); sHolder = getHolder(); sHolder.addCallback(this); res = getResources(); } public void draw() { try { canvas = sHolder.lockCanvas(); //draw something background.draw(canvas); } catch (Exception e) { e.printStackTrace(); } finally { if (canvas != null) { sHolder.unlockCanvasAndPost(canvas); } } } private void logic() { background.logic(); } @Override public void run() { while (flag) { long start = System.currentTimeMillis(); //do something...or draw() logic(); draw(); long end = System.currentTimeMillis(); long sleep = 50 - end + start; try { if (sleep>0) { Thread.sleep(sleep); } } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void surfaceCreated(SurfaceHolder arg0) { heightScreen = getHeight(); widthScreen = getWidth(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.background); background = new Background(bmp, 3, widthScreen, heightScreen); thread = new Thread(this); thread.start(); flag = true; } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceDestroyed(SurfaceHolder arg0) { flag = false; }}
5. MainActivity
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐去电池等图标和一切修饰部分(状态栏部分) this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new MySurfaceView(this));}}
6. 背景图(Himi)
0 0
- android 学习: 背景滚动效果
- CCParallaxNode-创建滚动背景效果
- cocos2d-x 多层背景滚动效果
- Android毛玻璃背景效果
- ANDROID-漂浮背景效果
- android背景滚动+Matrix实现图片拉伸(实现近大远小效果)
- Android TextView滚动效果
- TextView 滚动效果 android
- android 滑轮滚动效果
- Android实现滚动效果
- Android TextView滚动效果
- android 按钮点击背景效果
- android按钮背景+点击效果
- Android金钱滚动效果-滚动的textView
- Android TextView实现滚动效果
- Android 歌词同步滚动效果
- android 图片横向滚动效果
- Android ListView 滚动翻页效果
- OOP 原则
- Android调用webservice示例教程
- Oracle中删除某个用户下的所有数据呢
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示
- VS2005(C#)里读取及改变App.config里appSettings节的值
- android 学习: 背景滚动效果
- LCD的接口类型详解
- visual studio 自动排版快捷键
- JAVA的字符串拼接与性能
- TS
- Myeclipse8.5中svn插件安装方法总结
- 《黑马程序员》 使用阻塞队列实现同步通知
- FLV
- NVD3.js 中 图例 (legend) 中文字符 被图标遮住