首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android :停止缩容

Android :停止缩容
EN

Stack Overflow用户
提问于 2010-06-15 20:47:57
回答 3查看 1.1K关注 0票数 7

我正在尝试在视图中绘制图像,但在尝试保持原始图像的比例时遇到问题。基本上,我有一个小视图,我想在视图中显示图像的一部分。然后,意图是对图像执行平移,以便在视图中显示不同的部分。

不管我怎么尝试,要么图像会自动缩小以适应视图,要么整个图像都是可见的。我尝试过在BitmapDrawable、ImageView和Layout上进行设置,但都无济于事。

有人知道实现这一点的好方法吗?

EN

回答 3

Stack Overflow用户

发布于 2010-06-18 22:53:30

希望这段代码能有所帮助。我一个月前就用谷歌搜索过了。它的滚动性能为较大的Images.Here整个显示尺寸设置为视图的高度和宽度。你可以改变你知道的。并且还可以维护缩放控件。

代码语言:javascript
运行
复制
public class LargeImageScroller extends Activity {

// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // displayWidth and displayHeight will change depending on screen
     // orientation. To get these dynamically, we should hook onSizeChanged().
     // This simple example uses only landscape mode, so it's ok to get them
     // once on startup and use those values throughout.
     Display display = ((WindowManager)
          getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
     displayWidth = display.getWidth();
     displayHeight = display.getHeight();

     // SampleView constructor must be constructed last as it needs the
     // displayWidth and displayHeight we just got.
     setContentView(new SampleView(this));
}

private static class SampleView extends View {
     private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
     private static Rect displayRect = null; //rect we display to
     private Rect scrollRect = null; //rect we scroll over our bitmap with
     private int scrollRectX = 0; //current left location of scroll rect
     private int scrollRectY = 0; //current top location of scroll rect
     private float scrollByX = 0; //x amount to scroll by
     private float scrollByY = 0; //y amount to scroll by
     private float startX = 0; //track x from one ACTION_MOVE to the next
     private float startY = 0; //track y from one ACTION_MOVE to the next

     public SampleView(Context context) {
          super(context);

          // Destination rect for our main canvas draw. It never changes.
          displayRect = new Rect(0, 0, displayWidth, displayHeight);
          // Scroll rect: this will be used to 'scroll around' over the
          // bitmap in memory. Initialize as above.
          scrollRect = new Rect(0, 0, displayWidth, displayHeight);

          // Load a large bitmap into an offscreen area of memory.
          bmLargeImage = BitmapFactory.decodeResource(getResources(),
               R.drawable.testlargeimage);
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {

          switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN:
                    // Remember our initial down event location.
                    startX = event.getRawX();
                    startY = event.getRawY();
                    break;

               case MotionEvent.ACTION_MOVE:
                    float x = event.getRawX();
                    float y = event.getRawY();
                    // Calculate move update. This will happen many times
                    // during the course of a single movement gesture.
                    scrollByX = x - startX; //move update x increment
                    scrollByY = y - startY; //move update y increment
                    startX = x; //reset initial values to latest
                    startY = y;
                    invalidate(); //force a redraw
                    break;
          }
          return true; //done with this event so consume it
     }

     @Override
     protected void onDraw(Canvas canvas) {

          // Our move updates are calculated in ACTION_MOVE in the opposite direction
          // from how we want to move the scroll rect. Think of this as dragging to
          // the left being the same as sliding the scroll rect to the right.
          int newScrollRectX = scrollRectX - (int)scrollByX;
          int newScrollRectY = scrollRectY - (int)scrollByY;

          // Don't scroll off the left or right edges of the bitmap.
          if (newScrollRectX < 0)
               newScrollRectX = 0;
          else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth))
               newScrollRectX = (bmLargeImage.getWidth() - displayWidth);

          // Don't scroll off the top or bottom edges of the bitmap.
          if (newScrollRectY < 0)
               newScrollRectY = 0;
          else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight))
               newScrollRectY = (bmLargeImage.getHeight() - displayHeight);

          // We have our updated scroll rect coordinates, set them and draw.
          scrollRect.set(newScrollRectX, newScrollRectY,
               newScrollRectX + displayWidth, newScrollRectY + displayHeight);
          Paint paint = new Paint();
          canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);

          // Reset current scroll coordinates to reflect the latest updates,
          // so we can repeat this update process.
          scrollRectX = newScrollRectX;
          scrollRectY = newScrollRectY;

     }
}
}
票数 2
EN

Stack Overflow用户

发布于 2010-06-15 20:55:07

这可能不是最有效的方法,但如果您不打算移动太多,它就可以了。把它当做一个spritesheet,使用Bitmap.createBitmap(位图源,int x,int y,int width,int height)从原始的位图中获取你想要的部分作为它自己的位图,然后传递给它。然后传递给它,而不是整个位图,它将只处理您希望它显示的部分。

票数 0
EN

Stack Overflow用户

发布于 2010-06-17 18:40:49

听起来你想在一个较小的视图中拥有一个大的ImageView控件(就像一个窗口),然后在里面移动ImageView控件,这样一次只能看到它的一部分。

虽然不太确定您将如何做到这一点,但我非常确定使用AbsoluteLayout和一些修补工作是可能的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3045227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档