Hi falks, this is my first post, I'm a newbie in android developement
and it's giving me terrible headaches.

I have to develop an app for a client that need something like a map
of a floor building, this map should contain interactive graphic
references such buttons rappresenting beds (it's an hospital) that
should be created programmatically.
I've thought the best way to do it is to create a big background image
of the floor map, and adding on top the beds buttons. The map image is
very big so it should scroll on touch, for that I've found a great
example here:
http://www.anddev.org/large_image_scrolling_using_low_level_touch_events-t11182.html

what I'd like to do now is to create an array of buttons overlaying
the map, but eclipse give an error if I try to create an instance of a
button inside the SampleView ( extends RelativeLayout) constructor.
Why? is this illegal? and how can I create this array of buttons?
maybe I have to create outside in the main activity class, but then I
don't know how to translate them following the map image.

following the code

thanks in advance


[code]package com.works.Unitel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

public class UnitelActivity extends Activity {
        /** Called when the activity is first created. */
        private static int displayWidth = 0;
        private static int displayHeight = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE))
                                .getDefaultDisplay();
                displayWidth = display.getWidth();
                displayHeight = display.getHeight();

                SampleView rl = new SampleView(this);
                setContentView(rl);
                rl.setBackgroundColor(Color.YELLOW); // very strange, it doesn't
show the image without setting a //background

        }

        private static class SampleView extends RelativeLayout {
                private static Button bt;
                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);

                         bt = new Button(this); // here is where eclipse throw 
the
error!!! :(

                        bmLargeImage = 
BitmapFactory.decodeResource(getResources(),
                                        R.drawable.piano);
                        displayRect = new Rect(0, 0, displayWidth, 
displayHeight);
                        scrollRect = new Rect(0, 0, displayWidth, 
displayHeight);

                }

                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;

                }

                @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
                }

        }

}
[/code]

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

Reply via email to