java - 图像过度缩放

标签 java android zooming

我从 tut 中获得了这段代码,它可以滚动和缩放图像,但即使图像很小,代码也会拉伸(stretch)图像以使其填满整个屏幕。我希望它有一个可能包含图像的对话框,并通过修改代码而不占用整个屏幕来启用缩放和滚动。

编辑:是否可以使用下面的代码限制滚动或限制图像的大小? 第一个显示器上有一个默认图像大小(居中),这也是最小缩小。当图像达到其最大可滚动尺寸(实际图像尺寸)时,它将恢复为默认尺寸。如果可能的话,还要控制图片可以滚动的区域,这样我就不会在放大时迷路了。

public class ZoomHelloActivity extends Activity {

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


    static String img="";

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

        Display display = ((WindowManager)
                          getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        displayWidth = display.getWidth();
        displayHeight = display.getHeight();

        //get intent
        Bundle extras = getIntent().getExtras(); 

        if (extras != null) {
            Toast.makeText(this.getBaseContext(),"Inside: "+img.toString(),
                   Toast.LENGTH_SHORT).show();
            img = extras.getString("img");
            // and get whatever type user account id is
        }
        else{   Toast.makeText(this.getBaseContext(),"ERROR ni",
                   Toast.LENGTH_SHORT).show();}
        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.
            int assignImg;
            assignImg = Integer.parseInt(img);
            bmLargeImage = BitmapFactory.decodeResource(getResources(),
                    assignImg);
        }

        @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) {
            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;
        }
    }
}

编辑: 想要的输出: enter image description here

最佳答案

也许可以尝试以下方法:

OnCreate() 方法中代替这个:

displayWidth = display.getWidth();
displayHeight = display.getHeight();

输入以下内容:

private static Bitmap bmLargeImage;
bmLargeImage = BitmapFactory.decodeResource(getResources(), assignImg);
int bmWidth  = bmLargeImage.getWidth();
int bmHeight = bmLargeImage.getHeight();

displayWidth = display.getWidth();
displayHeight = display.getHeight();

if (bmWidth < displayWidth){
  displayWidth = bmWidth;
}
if (bmHeight < displayHeight){
  displayHeight = bmHeight;
}

看看这是否可以绕过 tut 的算法。

关于java - 图像过度缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17234421/

相关文章:

java - 如何将 JComboBox(Object[] items) 添加到 jComboBox1.addItem(String items);

android - Android:录制和收听音频

qt - 缩放 QtQuick 绘制的项目

javascript - 为什么 HTML Canvas 中的对象在放大时不显示

android - AltBeacon 测距从不返回超过 1 个信标

JavaFX 8 - 相对于鼠标指针缩放

java - 从 JButton ActionPerformed 调用耗时的方法

java - 来自外部 jar 的 seam i18n 属性文件

java - 自从更改 SpringBoot 依赖性以来,在空字段上加入时出现 Freemarker 错误

android - 读取命令时没有输出?