Hey folks,

A few weeks ago I posted a view I was working on and in that time I
have improved the view a lot, but I've been asked to add a new
features of zooming into the image.

The view is a custom image viewer that allows large images (such as
640x1000) to be loaded, and the user to scroll around using either the
touchscreen or trackball on the device.  The code below is reasonably
succesful in that it works at the default scale level of 1.

But with addign zooming, instead of zooming into the 'centre' of the
location the user is looking at, and allowing the user to move around
the whole of the image at this level it instead zooms into the top and
left of the location, and in doing so it starts to cut off the bottom
and right of the image whole, so while zoomed in you cannot go any
ruther right or down in the image, but you can go to the top and left
easily enough.

The best example of what I am trying to achive is the Image viewer in
the camera application.

So my two questions are:

1) Can anyone suggest how I can fix this in the canvas or
2) Is there a better way to do this with an existing feature in the
framework?

I'm probably thinking it's something to do with the modifierValue when
scrolling around that I somehow need to increase this, but trying this
causes my activity to crash.

Code:

public class ZN5ScrollView extends View {

        public ZN5ScrollView(Context context, AttributeSet attrs) {
                super(context, attrs);

                scrollX = 0;
                scrollY = 0;
                scale = 1.0f;
                modifierValue = 50;

                ViewPaint = new Paint();

                TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ZN5ScrollView);
                LoadedBitmap = 
BitmapFactory.decodeResource(context.getResources(),
a.getResourceId(R.styleable.ZN5ScrollView_src, R.drawable.icon));

                IMAGE_WIDTH = LoadedBitmap.getWidth();
                IMAGE_HEIGHT = LoadedBitmap.getHeight();

                ViewBitmap = Bitmap.createBitmap(LoadedBitmap);
        }

        protected void onSizeChanged  (int w, int h, int oldw, int oldh) {
                SCREEN_WIDTH = w;
                SCREEN_HEIGHT = h;

                if (IMAGE_WIDTH < SCREEN_WIDTH) {
                        IMAGE_WIDTH = SCREEN_WIDTH - scrollX;
                }
                if (IMAGE_HEIGHT < SCREEN_HEIGHT) {
                        IMAGE_HEIGHT = SCREEN_HEIGHT - scrollY;
                }

        }

        @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.scale(scale, scale);
        canvas.drawBitmap(ViewBitmap, 0f, 0f, ViewPaint);
    }

public void handleView(int zoomType) {
        switch (zoomType) {
        case ZOOM_IN:
                if (scale <= 1.5f) {
                        scale = scale + 0.1f;
                }
                break;
        case ZOOM_OUT:
                if (scale > 1.0f) {
                        scale = scale -0.1f;
                }
                break;
        }
        invalidate();
}


public void handleScroll(float distX, float distY) {
        /* X-Axis */
        if(distX > 6.0) {
                if(scrollX < IMAGE_WIDTH) {
                        scrollX = Math.min(IMAGE_WIDTH - SCREEN_WIDTH, scrollX +
modifierValue);
                }
        }
        else if(distX < -6.0) {
                if(scrollX >= 50) {
                        scrollX = Math.min(IMAGE_WIDTH + SCREEN_WIDTH, scrollX -
modifierValue);
                } else {
                        scrollX = 0;
                }
        }

        /* Y-Axis*/
        if(distY > 6.0) {
                if(scrollY < IMAGE_HEIGHT) {
                        scrollY = Math.min(IMAGE_HEIGHT - SCREEN_HEIGHT, 
scrollY +
modifierValue);
                }
        }
        else if(distY < -6.0) {
                if(scrollY >= 50) {
                        scrollY = Math.min(IMAGE_HEIGHT + SCREEN_HEIGHT, 
scrollY -
modifierValue);
                } else {
                        scrollY = 0;
                }
        }

        if((scrollX <= IMAGE_WIDTH) && (scrollY <= IMAGE_HEIGHT)) {
                ViewBitmap = Bitmap.createBitmap(LoadedBitmap, scrollX, scrollY,
SCREEN_WIDTH, SCREEN_HEIGHT);
                invalidate();
        }
}

private int modifierValue;

private float scale;
public final int ZOOM_IN = 1;
public final int ZOOM_OUT = 2;

private int SCREEN_WIDTH;
private int SCREEN_HEIGHT;

private int IMAGE_WIDTH;
private int IMAGE_HEIGHT;

private int scrollX;
private int scrollY;

private Bitmap LoadedBitmap;
private Bitmap ViewBitmap;
private Paint ViewPaint;
}


Regards,
Tane
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to