In this blog implement the drag image onTouch ImageView and Move the same image. the image show as motion Animation on touch finger effect. explain code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:textSize="30dip"
android:text="Android Beginner point" />
<RelativeLayout
android:id="@+id/im"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:gravity="center" >
<ImageView
android:id="@+id/imageView1d"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/gh" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1d"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:src="@drawable/dsf" />
</RelativeLayout>
</RelativeLayout>
Drag.java
create the Drag Class inside src/..folder
..................................................................................................................
package com.androidbeginner.testcode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
@SuppressLint("ClickableViewAccessibility")
public class Drag extends Activity implements View.OnTouchListener {
private ImageView mImageView1, mImageView13;
private ViewGroup mRrootLayout, mRrootLayouts;
private int _xDelta;
private int _yDelta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag);
mRrootLayout = (ViewGroup) findViewById(R.id.root);
mRrootLayouts = (ViewGroup) findViewById(R.id.im);
mImageView1 = (ImageView) mRrootLayouts.findViewById(R.id.imageView1);
mImageView1.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
120, 120);
mImageView1.setLayoutParams(layoutParams1);
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_CANCEL: {
mImageView13.getBackground().clearColorFilter();
mImageView13.invalidate();
break;
}
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
mRrootLayouts.invalidate();
return true;
}
}
0 comments