Move+Zoom App

  • Hallo erstmal an alle:) Habe eine App geschrieben die Bilder verschieb, also auf dem Display und touch events erkennt...Nun hab ich aus einem Tutorial noch eine Zoom Class herauskopiert und das funktioniert auch für sich aleine supper....Nun weiß ich nur nicht wie ich die beiden verbinden koennte, hoffe ihr koennt mir da helfen! Mfg Lukas


    Move Class:



    package com.example.largeimagescroller;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.AlertDialog.Builder;
    import android.os.Bundle;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.text.style.ScaleXSpan;
    import android.view.Display;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import android.widget.Toast;
    import android.widget.ZoomButtonsController;
    import android.widget.ZoomButtonsController.OnZoomListener;
    import android.widget.ZoomControls;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.ZoomButtonsController;
    import android.widget.ZoomButtonsController.OnZoomListener;

    public class LargeImageScroller extends Activity {

    // Physical display width and height.
    private static int displayWidth = 0;
    private static int displayHeight = 0;
    private static int view =0;
    static int updo =0;
    static float[] zw = new float[1];
    static String Text;
    static Variables v = new Variables();


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(new Zoom(this));
    // 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));
    // setContentView(new Zoom(this));




    }

    private static class SampleView extends View {
    private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
    private static Bitmap dummyBitmap;

    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
    int dummy = 0;

    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.
    if(dummy ==0){
    int dWidth = displayWidth;
    int dHeigth = displayHeight;
    //Erzeugt zunächst eine "Dummy" Bitmap aus dem Bild und Scaliert diese Dann im nächsten Schritt
    dummyBitmap = BitmapFactory.decodeResource(getResources(),
    R.drawable.ando);
    bmLargeImage = Bitmap.createScaledBitmap(dummyBitmap, dWidth,dHeigth,false);
    }else{
    bmLargeImage = BitmapFactory.decodeResource(getResources(),
    R.drawable.testlargeimg);


    }
    }

    @SuppressWarnings("unused")
    @Override
    public boolean onTouchEvent(MotionEvent event) {



    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setCancelable(true);
    //builder.setIcon(R.drawable.dialog_question);
    //builder.setTitle(Text + " Field");
    builder.setInverseBackgroundForced(true);
    builder.setMessage("A Lot of Crazy Information about: " + Text);// Text);
    builder.setPositiveButton("More Information", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
    });
    builder.setNegativeButton("Continue", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
    });

    AlertDialog alert = builder.create();

    switch (event.getAction()) {



    case MotionEvent.ACTION_MOVE:

    alert.dismiss();
    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;


    case MotionEvent.ACTION_DOWN:





    // Remember our initial down event location.
    startX = event.getRawX();
    startY = event.getRawY();
    if(dummy==0){
    dummy=1;
    bmLargeImage = BitmapFactory.decodeResource(getResources(),
    R.drawable.testlargeimg);
    invalidate();

    }


    float w = bmLargeImage.getWidth();
    float h =bmLargeImage.getHeight();
    float px= (100/w)*(scrollRectX+ event.getRawX());
    float py =(100/h)*(scrollRectY + event.getRawY());

    if(zw[0]==0){
    zw[0] = startX;
    }else{

    }




    CharSequence text = "X:" + px + " Y:" + py;
    int duration = Toast.LENGTH_SHORT;
    //Text = check((int)px,(int)py);
    v.fill();
    int[]cor = new int[2];
    getLocationOnScreen(cor);
    Text = v.check(scrollRectX+ event.getRawX(),scrollRectY + event.getRawY(),w,h);

    break;


    case MotionEvent.ACTION_UP:

    if(zw[0] == event.getRawX()&& Text != "false"){
    alert.show();
    }else{
    alert.dismiss();
    }
    zw[0]=0;
    //Toast toast = Toast.makeText(getContext(),""+zw[0]+" " + event.getRawX() + " " + bmLargeImage.getWidth() , 100);
    //toast.show();

    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;




    }
    }}


    Zoom Class:



    package com.example.largeimagescroller;


    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.ZoomButtonsController;
    import android.widget.ZoomButtonsController.OnZoomListener;

    public class Zoom extends View {
    private Drawable image;
    private int zoomControler=20;
    private ZoomButtonsController zoomController ;

    public Zoom(Context context)
    {
    super(context);
    image=context.getResources().getDrawable(R.drawable.testlargeimg);
    setFocusable(true);
    zoomController = new ZoomButtonsController(this);
    zoomController.setOnZoomListener(new OnZoomListener() {
    @Override
    public void onZoom(boolean zoomIn) {
    //onZoom(zoomIn);
    zoomControler+=10;
    invalidate();
    }
    @Override
    public void onVisibilityChanged(boolean visible) {
    }
    });}


    public boolean onTouchEvent(MotionEvent event){
    if(MotionEvent.ACTION_DOWN== event.getAction()){
    zoomController.setVisible(true);
    }
    return true;
    }



    @Override
    protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    //here u can control the width and height of the images........ this line is very important
    image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
    image.draw(canvas);

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
    zoomControler+=10;
    if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
    zoomControler-=10;
    if(zoomControler<10)
    zoomControler=10;

    invalidate();
    return true;
    }
    }

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!