How to move image to the right to get activity in android?

I have applied an image view application in my application, which I would like to implement, when the user moves the right side of the image, then I would like to get the following utility. How can I write an event or any other code to move the image from the right side?

I used am imageView like:

   (ImageView)findViewById(R.id.slide) //event for move right here

     Intent it = new Intent(Slide.this,NextActivity.class);
     startActivity(it);  

      

Please help me with any body ...

+3


source to share


3 answers


So if I understand you correctly, you want to move the ImageView around the screen, drag it, and when you click on a specific spot with it, do you want to start another action?

If so, you can try and use the code below ... which allows you to drag the image around the screen.

Just add onTouchListener to the imageView you are trying to offset (move / drag)

yourSlideImageViewObject.setOnTouchListener(new View.OnTouchListener() 
{
          @Override
          public boolean onTouch(View v, MotionEvent event) 
          {
                onSlideTouch(v, event);
                return false;
          }
});

      

Then add this code to handle Touch events



public void onSlideTouch( View view, MotionEvent event )
{
    //When the user pushes down on an ImageView
    if ( event.getAction() == MotionEvent.ACTION_DOWN )
    {
       inDragMode = true; //Set a variable so we know we started draggin the imageView
       //Set the selected ImageView X and Y exact position
       selectedImageViewX = Math.abs((int)event.getRawX()-((ImageView)view).getLeft());
       selectedImageViewY = Math.abs((int)event.getRawY()-((ImageView)view).getTop());
       //Bring the imageView in front
       ((ImageView)view).bringToFront();
    }

    //When the user let the ImageView go (raises finger)
    if ( event.getAction() == MotionEvent.ACTION_UP )
    {
       inDragMode = false; //Reset the variable which let us know we're not in drag mode anymore
    }

    //When the user keeps his finger on te screen and drags it (slides it)
    if ( event.getAction() == MotionEvent.ACTION_MOVE )
    {
        //If we've started draggin the imageView
        if ( inDragMode )
        {
            //Get the imageView object
            ImageView slide = (ImageView)findViewById(R.id.slide);
            //Get a parameters object (THIS EXAMPLE IS FOR A RELATIVE LAYOUT)
            RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams)slide.getLayoutParams();
            //Change the position of the imageview accordingly
            params.setMargins((int)event.getRawX()-selectedImageViewX, (int)event.getRawY()-selectedImageViewY, 0, 0);
            //Set the new params
            slide.setLayoutParams(params);

            //If we hit a limit with our imageView position
            if( slideImageView_position is inside an interval )
            {
                //Open another activity
                Intent it = new Intent(Slide.this,NextActivity.class);
                startActivity(it);
            }
        }
    }

}

      

The above should drag your image around the screen.

You can also implement in the MotionEvent.ACTION_MOVE .... event to constrain the ImageView to move around the screen. So check the new coordinates of your finger on the screen and check if they are out of bounds. If so, then don't do anything. If they are not out of bounds, then change the left and top margins of the ImaveView.

Also, in the MotionEvent.ACTION_UP event, make sure that the left margin of the ImaveView exceeds a predefined limit (meaning the user has skewed the image in the right place). If so, you can continue to do whatever you want, which means getting started. OR if you don't want to open the activity while the user is dragging, not only after he has removed his finger from the imageView, then just do it if () I wrote in the MotionEvent.ACTION_MOVE event. Insert the required spacing there, so when the ImageView is dragged within that range, the action begins.

If you're confused, ask, "It's very late here and I'm tired, so I may not have explained it correctly: D

+3


source


I don't quite understand your question, but I am interpreting it to mean that you want to move the ImageView containing the Activity into another activity.

If so, I would actually convert these actions to Fragments and then use the ViewPager. Here is a good Google Developer blog post that introduces this concept: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html



You want to implement a FragmentPagerAdapter with getCount - the number of pages the user can swap between them. Then getItem should return a fragment containing the first ImageView for position 0 and a fragment containing what is in your NextActivity for position 1. You can return more fragments for positions 2 and up to have an interface that can be scrolled between left and right ... This hopefully gives you the sliding UI you are looking for.

0


source


package avatar .view.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class customview extends View {

private int PanX, PanY;
private GestureDetector gestureDetector;
private Bitmap mBitmap;
private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);

public customview(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO ่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใƒผใƒปใ‚นใ‚ฟใƒ–
}

public customview(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO ่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใƒผใƒปใ‚นใ‚ฟใƒ–s
}

public customview(Context context) {
    super(context);
    // TODO ่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใƒผใƒปใ‚นใ‚ฟใƒ–
}

private float pointX = 0, pointY = 0;

private void setPoint(MotionEvent e) {
    pointX = e.getX();
    pointY = e.getY();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mBitmap != null) {
        pointX = PanX - (mBitmap.getWidth() / 2);
        pointY = PanY - (mBitmap.getHeight() / 2);
        canvas.drawBitmap(mBitmap, pointX, pointY, mPaint);
    }
}

/*
 * (้ž Javadoc)
 * 
 * @see android.view.View#onMeasure(int, int)
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // ใƒ“ใƒฅใƒผใฎใ‚ตใ‚คใ‚บใ‚’่จญๅฎšใ™ใ‚‹
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec
            .getSize(heightMeasureSpec));
}

public void setPanX(int panX) {
    PanX = panX;
}

public int getPanX() {
    return PanX;
}

public void setPanY(int panY) {
    PanY = panY;
}

public int getPanY() {
    return PanY;
}

public void setmBitmap(Bitmap mBitmap) {
    this.mBitmap = mBitmap;
}

public Bitmap getmBitmap() {
    return mBitmap;
}

      

}

Custom view. Then in the Main Activity

 @Override
public boolean onTouch(View v, MotionEvent e) {
    // TODO ่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใƒกใ‚ฝใƒƒใƒ‰ใƒปใ‚นใ‚ฟใƒ–
    if (isSelected == false) {
        return false;
    }
    final int action = e.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        panX = (int) e.getX();
        panY = (int) e.getY();

        ctview.setPanX(panX);
        ctview.setPanY(panY);
        ctview.invalidate();
        Log.v("ACTION_DOWN", "ACTION_DOWN");

        break;
    case MotionEvent.ACTION_MOVE:
        panX = (int) e.getX();
        panY = (int) e.getY();

        ctview.setPanX(panX);
        ctview.setPanY(panY);
        ctview.invalidate();
        Log.v("ACTION_MOVE", "ACTION_MOVE");
        break;
    case MotionEvent.ACTION_UP:
        Log.v("ACTION_UP", "ACTION_UP");
        if (isSelected) {
            isSelected = false;
            ctview.setmBitmap(null);
            ctview.invalidate();
        }
        break;
    default:
        break;
    }
    return true;
}

      

And Oncreate

Customview.setmBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ImageSlide);

      

-1


source