I m trying to draggable and resizeable textview inside image on android. im able to draggable on touch of TextView but its dragging beyond the screen.Could you help me drag textview within the image. code is here
public void drag(MotionEvent event, View v){
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
Log.v("touch","move");
params.topMargin = (int)event.getRawY() - (textView.getHeight());
params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);
int y= (int)event.getRawY();
int x=(int)event.getRawX();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
if(params.topMargin>=0 && params.leftMargin>=0 )
textView.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_UP:
{
Log.v("touch","up");
params.topMargin = (int)event.getRawY() - (textView.getHeight());
params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);
Log.v("touch", "params.topMargin"+params.topMargin);
Log.v("touch", " params.leftMargin"+ params.leftMargin);
if( ( params.topMargin>=0) && (params.leftMargin>=0))
textView.setLayoutParams(params);
// textView.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_DOWN:
{
textView.setLayoutParams(params);
break;
}
}
}
Do you really need drag for move TextView? You may extend TextView, redefine public boolean onTouch(View v, MotionEvent event), calculate offsetX and offsetY in it, add interface, for example, OnPositionChangeListener(int offsetX, int offsetY) and lets activity contains this RelativeLayout implements this interface. It's works for me.
private void initDragView() {
// text view drag
_root = (ViewGroup) findViewById(R.id.tblDragView);
_root.addView(txtContent);
_root.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX() - _xDelta;
int y = (int) event.getY() - _yDelta;
int w = backgroundImageWidth;// getWindowManager().getDefaultDisplay().getWidth();
int h = backgroundImageHeight;// getWindowManager().getDefaultDisplay().getHeight();
if (x < 0 || y < 0 || x > w || y > h)
return false;
if (x > w) {
x = w;
return true;
}
if (y > h) {
// Log.d("TAG", "Ouch i am out side height");
y = h;
return true;
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
if (lp.leftMargin < x && lp.topMargin < y)
lp.setMargins(x, y, 0, 0);
// txtContent.setBackgroundColor(getResources().getColor(
// R.color.blue));
txtContent.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
txtContent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
_xDelta = (int) event.getX();
_yDelta = (int) event.getY();
txtContent = (TextView) v;
break;
default:
break;
}
return false;
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.