3

I am developing an application in which I want to add image which can slide from left to right and from right to left like below image. That inner white play image should be move from left to right and Vice Versa.

enter image description here

What I have did so far is, I am able to move single image from left to right and vice versa but I want set background image as well like above rounded shaped black background.

Here is my code:

imageView.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        int eid = event.getAction();
        switch (eid) {
        case MotionEvent.ACTION_MOVE:

            RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
            int x = (int) event.getRawX();
            mParams.leftMargin = x - 50;
            imageView.setLayoutParams(mParams);

            break;

        default:
            break;
        }
        return true;
    }
});

EDIT

I tried to manage background image by setting my layout xml like below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="49dp"
        android:background="@drawable/set_user_profile_back"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/hello_world"
            android:src="@drawable/next" />
    </RelativeLayout>

</RelativeLayout>

But now I am facing problem with image size, image size is decreased in right how to solve this and how to fix start and end point for image movement.

enter image description here

Any idea and advice will be apppreciated

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Juned
  • 6,290
  • 7
  • 45
  • 93
  • use SeekBar insted of imageview... http://www.edumobile.org/android/android-apps/seek-bar-example/ and http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/ – Dhaval Parmar Apr 24 '13 at 06:50
  • Thanks but i have already implemented Swipe functionality to move to next screen and if i use seekbar then i need to create custom seekbar for same. – Juned Apr 24 '13 at 06:59
  • you can try applying animation to the imageview!!! – Shrikant Apr 24 '13 at 07:25
  • @Shrikant it would be great if you can give any sample example for animation ? – Juned Apr 24 '13 at 07:31
  • @juned: i have used animation for activity change, but for image view i got this link http://stackoverflow.com/a/5151774/1457952, hope it helps you!!! – Shrikant Apr 24 '13 at 07:41
  • Thanks @Shrikant that animaations are for opening and closing screens but what about moving imageview ? – Juned Apr 24 '13 at 07:46
  • @Shrikant How to make the image move from right to left? – Srikanth Pai Oct 29 '13 at 08:10
  • @juned How to make the image move from right to left? – Srikanth Pai Oct 29 '13 at 08:11
  • @contactmeandroid you can download the xml from here http://junedk.blogspot.com/2013/10/slide-image-from-left-to-right-and.html – Juned Oct 29 '13 at 11:56
  • @juned - I want the image to move right to left via touchListner and not animation any idea? – Srikanth Pai Oct 29 '13 at 12:05
  • ur above code works perfectly when I place the Image ParentLeft but it doesnt drag whn I place it parentRight – Srikanth Pai Oct 29 '13 at 12:06
  • @contactmeandroid then you probably use gesture listener, if want swipe something using touch. and for you problem i need to find out ma project in which i have implemented this. – Juned Oct 29 '13 at 13:26
  • @juned check this http://stackoverflow.com/q/19649023/1538399 this is exactly i am trying to do – Srikanth Pai Oct 29 '13 at 13:29

1 Answers1

8

Can you please try this piece of code Juned

public class MainActivity extends Activity {

Button b1;
ImageView logo;
float width;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1 = (Button) findViewById(R.id.button1);
    logo = (ImageView) findViewById(R.id.logo);

    Display display = getWindowManager().getDefaultDisplay();
    width = display.getWidth();
    final Animation posX = new TranslateAnimation(0, width - 50, 0, 0);
    posX.setDuration(1000);
    posX.setFillAfter(true);

    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            logo.startAnimation(posX);
            logo.setVisibility(View.VISIBLE);
        }
    });
}}
Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • great codde @shrikan but how do i implement same for touch event, as i described i want to drag the image – Juned Apr 24 '13 at 08:56