3

I have implemented an application with image view in my application i would like to implement when user move the image right side then i would like to get the next acitvity. How can i write an event or any other code for move the image just right side?

I have used am imageView code as:

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

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

Please any body help me...

prasad.gai
  • 2,977
  • 10
  • 58
  • 93

3 Answers3

4

So, if i understood you correctly, you want to move an ImageView across the screen, drag it, and when you hit a certain spot with it you want to start another activity?

If so, you could try and use the code below...which let's you drag your imageview across the screen

Just add an onTouchListener to the imageView you're trying to slide (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 the 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's the ImageView go (raises finger)
    if ( event.getAction() == MotionEvent.ACTION_UP )
    {
       inDragMode = false; //Reset the variable which let's 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 imageview accross the screen.

You can also implement in the MotionEvent.ACTION_MOVE event .... to limit the movement of the ImageView accross the screen. So check the new coordinates of your finger across the screen and check if they are out of bounds. If so, then do nothing. If they aren't out of bounds, then change the left and upper margins of the ImaveView.

Also, on the MotionEvent.ACTION_UP event, check to see if the left margin of the ImaveView is over a predefined limit (which means the user slided the imageView over you intended position). If so, you can continue doing whatever you want, which means starting an activity. 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 that if () i've written under the MotionEvent.ACTION_MOVE event. Insert there the interval you want, so when the imageView is dragged inside that interval, the activity starts.

If you're confused, please ask :) It's very late here, and i'm tired, so i might have not explained it properly :D

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • yey... first accepted answer. Haha.. thanks. gl with your project :) – AndreiBogdan Mar 10 '12 at 10:04
  • What is the diffrence between "yourSlideImageViewObject" and "slide" or are they the same object . Also how would this work for a Linear layout? – user1163234 Mar 25 '12 at 14:54
  • @user1163234 yourSlideimageViewObject can be any imageView object. And no, i don't think this will work for a linear layout, but i'm not sure. You'll have to try it :) sry – AndreiBogdan Mar 26 '12 at 06:55
  • works for linearlayout...can yo give an example for" if( slideImageView_position is inside an interval )" I dont underrstand .. – user1163234 Mar 26 '12 at 12:21
  • prasad.gai needed to check if the user dragged it in a specific spot. For example you can get the imageView's position, top and left. And you could do something like. it the top value is between 100 and 200 and the left margin of the imageView is between 50 and 60 then it means the ImageView was dragged in a required place on the screen, (let's say in the top right corner). – AndreiBogdan Mar 27 '12 at 06:10
  • 1
    Good solution! However, I did require to set return true; in onTouch, so that the events could get registered with the OnTouchListener. Otherwise your code would only check for the ACTION_DOWN event. See also http://stackoverflow.com/a/5977889/831825 – Markus Rudel Mar 29 '12 at 18:08
0

I don't quite understand your question, but I interpret it as that you want to slide an ImageView containing Activity into another Activity.

If that is the case, I would actually convert those Activities to being Fragments and then use a 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'll want to implement a FragmentPagerAdapter with getCount being the number of pages the user can swipe between. 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 higher to have an interface that you can swipe between, left and right. This hopefully gives you the sliding user experience that you are looking for.

louielouie
  • 14,881
  • 3
  • 26
  • 31
-1

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 a view . Then in 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);
Jack Long
  • 69
  • 1
  • 6
  • my question is move the image view from left to right by user then i will start another activity like slider image.this is not my answer – prasad.gai Mar 10 '12 at 03:19
  • Why you don't you Event Ontouch of ImageView ? You can check slide from left to right with MotionEvent , MotionEvent.ActionDown , MotionEvent.ActionUp , – Jack Long Mar 10 '12 at 03:37
  • I have demo Code . How i send it to you ?? Send me your Email . – Jack Long Mar 10 '12 at 04:32