平滑滚动endles背景画布? [英] Smooth endles scrolling background with canvas?

查看:150
本文介绍了平滑滚动endles背景画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎所有

我有一个背景图像和我想要为它被慢慢向右移动,当图像具有达到与图像左开始侧的端部的画面的右端,图像必须启动再次展示了右侧的开始,作为一个无限的水平滚动。

I have a background image and what I want is for it to be moving slowly to the right and when the image has reach the right end of the screen with the end of the left starting side of the image, the image must start again showing the start of the right side, as an infinite horizontal scroll.

这怎么能不产生溢位内存异常实现?

How can this be achieved without generating bitmap overflow memory exceptions?

我trye​​d它绘制的帆布位图的两倍...但它并不顺利,这是非常让人毛骨悚然带跳,而不是优化:

I tryed it drawing two times the bitmap with canvas... but it is not smooth, it is very creepy with jumps and not optimized:

universeBitmap = Bitmap.createScaledBitmap(universeBitmap, sw, sh, false); 

    universeView = new View(this){

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (paused==true){
                canvas.drawBitmap(universeBitmap, universeX, 0, null);          
                return;
            }
            long currentTime=System.currentTimeMillis();
            if ((currentTime-lastdrawTime)>100){                    
                if (universeX>sw){
                    universeX=0;
                }
                lastdrawTime=currentTime;
                universeX+=1;
            }
            canvas.drawBitmap(universeBitmap, universeX, 0, null);
            canvas.drawBitmap(universeBitmap, universeX-sw, 0, null);
            invalidate();
        }   
    };

我也试过没有每100毫秒无效的观点,但有一个线程和处理程序,同样的结果....令人毛骨悚然的非平稳运动:

I also tried without invalidating the view each 100ms, but with a thread and a handler, and same result.... creepy non smooth movement:

universeBitmap = Bitmap.createScaledBitmap(universeBitmap, sw, sh, false); 

            universeView = new View(this){

                @Override
                protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);               
                    canvas.drawBitmap(universeBitmap, universeX, 0, null);
                    canvas.drawBitmap(universeBitmap, universeX-sw, 0, null);
                }   
            };
.
.
.
        Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (universeX>sw){
                    universeX=0;
                }
                universeView.invalidate();
                universeX+=1;
            }
        };  

.
.
.

     public void run() {
            try {
               while( !backgroundThread.interrupted() ) {
                   synchronized(this){  
                       handler.sendEmptyMessage(0);   
                       wait(100);
                   }    
               }
    }

感谢

推荐答案

假设你不希望用户通过自己的滚动这样的背景下,你可以使用的 TranslateAnimation 与<一个href=\"http://developer.android.com/reference/android/view/animation/Animation.html#setRepeatMode%28int%29\"相对=nofollow> INFINITE 参数对重复模式,这将是平滑的。创建2 的ImageView 取值水平(甚至是简单的查看和制定位图为背景),移动他们的父母。当第一个下车的屏幕上删除,并添加另一个在另一边。这是为背景的照片/图片填充整个屏幕,如果你有其他尺寸的背景只需添加更多的的ImageView s到创建2倍或小于屏幕的宽度更简易的长度(可在的onCreate 上伊斯利测量或其它地方。只要创建类似适配器的背景图片,甚至您可以托盘现有项目/库通常命名为如 Horizo​​ntalScrollView 未便与适配器 getCount将,例如 Integer.MAX_VALUE的

assuming you don't want for user to scroll this background by own you may use TranslateAnimation with INFINITE param for repeat mode, it will be smooth. create 2 ImageViews horizontally (or even simple Views and setting Bitmap as background) and move their parent. when first get off the screen remove it and add another on other side. this is for background photo/image filling whole screen, if you have other size background just add more ImageViews to create 2x or more summary length than width of the screen (which can be easly measured on in onCreate or wherever. Just create something like Adapter for background image or even you may tray existing projects/libs usually named smth like HorizontalScrollView with Adapter returning some large numer in getCount, e.g. Integer.MAX_VALUE

关于位图错误 - 请注意,适配器不破坏丢失/ lefting 查看 S,而是把它们作为下一个(再现) - 这是 convertView getView(...)方法。使用 ViewHolder 模式,你只需要使用两个查看 s的单位图设置(同refrence)。您可以创建只有一次在构造函数中静态背景位图并保持的 LruCache preventing从回收。每个设备可以处理位图与尺寸相同的屏幕

about Bitmap errors - note that Adapter don't destroy lost/lefting Views, but use them as next (recreates) - this is convertView in getView(...) method. use ViewHolder pattern and you will use only two Views with single Bitmap setting (same refrence). you may create just once your static background Bitmap in constructor and keep in LruCache preventing from recycle it. every device can handle bitmap with same size as screen

这篇关于平滑滚动endles背景画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆