在Android的顺利滚动画布 [英] Scrolling a Canvas smoothly in Android

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

问题描述

我是新来的Andr​​oid。

I'm new to Android.

我画的位图,线条和图形上的我的观点的OnDraw(帆布油画)方法中一个画布。我寻求帮助如何响应拖累用户实现平滑滚动。我已搜查,但没有发现任何教程,以帮助我。

I am drawing bitmaps, lines and shapes onto a Canvas inside the OnDraw(Canvas canvas) method of my view. I am looking for help on how to implement smooth scrolling in response to a drag by the user. I have searched but not found any tutorials to help me with this.

有关画布参考似乎是说,如果一个Canvas是从位图构造(称之为bmpBuffer,说的),那么什么画在画布也被绘制在bmpBuffer。有没有可能使用bmpBuffer来实现滚动...也许把它复制到一个时移几个像素的画布?但是,如果我用Canvas.drawBitmap画bmpBuffer回到画布几个像素移位,不会bmpBuffer被破坏?因此,或许我应该复制bmpBuffer到bmpBuffer2然后绘制bmpBuffer2回到画布。

The reference for Canvas seems to say that if a Canvas is constructed from a Bitmap (called bmpBuffer, say) then anything drawn on the Canvas is also drawn on bmpBuffer. Would it be possible to use bmpBuffer to implement a scroll ... perhaps copy it back to the Canvas shifted by a few pixels at a time? But if I use Canvas.drawBitmap to draw bmpBuffer back to Canvas shifted by a few pixels, won't bmpBuffer be corrupted? Perhaps, therefore, I should copy bmpBuffer to bmpBuffer2 then draw bmpBuffer2 back to the Canvas.

有一个更简单的方法是将绘制线条,形状等直入缓冲区位图,然后绘制缓冲区(带班)到Canvas但到目前为止,我所看到的各种方法:的drawLine() drawShape()等不可用于绘制到一个位图...只有一个画布。

A more straightforward approach would be to draw the lines, shapes, etc. straight into a buffer Bitmap then draw that buffer (with a shift) onto the Canvas but so far as I can see the various methods: drawLine(), drawShape() and so on are not available for drawing to a Bitmap ... only to a Canvas.

我能有2画布?其中之一将来自缓冲器的位图来构造和简单地用于绘制线条,形状等,然后将缓冲的位图将被绘制到其他画布在查看显示?

Could I have 2 Canvases? One of which would be constructed from the buffer bitmap and used simply for plotting the lines, shapes, etc. and then the buffer bitmap would be drawn onto the other Canvas for display in the View?

我应该欢迎任何意见!

I should welcome any advice!

答案在这里(和其他网站)类似的问题参见块传输。我理解的概念,却找不到有关的blit或BitBlt的Android的文档中的任何内容。是否Canvas.drawBitmap和Bitmap.Copy Android的等价物?

Answers to similar questions here (and on other websites) refer to "blitting". I understand the concept but can't find anything about "blit" or "bitblt" in the Android documentation. Are Canvas.drawBitmap and Bitmap.Copy Android's equivalents?

推荐答案

我有这个问题太,

我不喜欢这样的画:

Canvas BigCanvas = new Canvas();
Bitmap BigBitmap = new Bitmap(width,height);

int ScrollPosX , ScrollPosY  // (calculate these with the onScrollEvent handler)

void onCreate()
{
   BigCanvas.SetBitmap(BigBitmap);
}

onDraw(Canvas TargetCanvas)
{
   // do drawing stuff
   // ie.  BigCanvas.Draw.... line/bitmap/anything

   //draw to the screen with the scrolloffset

   //drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)
   TargetCanvas.DrawBitmap(BigBitmap(new Rect(ScrollPosX,ScrollPosY,ScrollPosX + BigBitmap.getWidth(),ScrollPosY + BigBitmap.getHeight(),new Rect(0,0,ScreenWidth,ScreenHeight),null);
}

平滑滚动你需要做出某种方法是滚动(即第一个滚动点和10日)后,需要几个点,减去那些和滚动通过这个数字在每个循环,使得它逐渐慢(ScrollAmount - 打开 - 摩擦)。

for smooth scrolling you'd need to make some sort of method that takes a few points after scrolling (i.e the first scroll point and the 10th) , subtract those and scroll by that number in a for each loop that makes it gradually slower ( ScrollAmount - turns - Friction ).

我希望这给一些更深入的了解。

I Hope this gives some more insight.

这篇关于在Android的顺利滚动画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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