android 拖动imageview 如果没有拖动到指定的位置 返回到起点怎么实现
2个回答
展开全部
点击imageview,加一个边框,再点击,取消边框。拖动imageview,可以停在移动结束之后的位置上,而不是弹回移动之前的位置。
请问有什么好的实现办法。
public class MainActivity extends Activity {
private int screenWidth;
private int screenHeight;
private ImageView bgview;
private ImageView photoview;
private TextView txtview;
int top, right, bottom, left;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;
bgview = (ImageView) findViewById(R.id.bgview);
photoview = (ImageView) findViewById(R.id.photoview);
photoview.setOnTouchListener(new TouchHandler());
photoview.setOnClickListener(new ClickHandler());
}
class TouchHandler implements OnTouchListener{
int lastX , lastY, dx, dy;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// if(mode == NONE)
// if(dx < 2 && dy < 2)
{
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
}
break;
case MotionEvent.ACTION_MOVE:
dx = (int) event.getRawX() - lastX;
dy = (int) event.getRawY() - lastY;
// if(mode == DRAG)
{
// photoview.setBackgroundResource(R.drawable.img_border);
left = v.getLeft() + dx;
top = v.getTop() + dy;
right = v.getRight() + dx;
bottom = v.getBottom() + dy;
if(left < 0)
{
left = 0;
right = left + v.getWidth();
}
if(right > screenWidth)
{
right = screenWidth;
left = right - v.getWidth();
}
if(top < 0)
{
top = 0;
bottom = top + v.getHeight();
}
if(bottom > screenHeight)
{
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
}
break;
case MotionEvent.ACTION_UP:
break;
}
if(dx < 2 && dy < 2)
{
return false;
}
return true;
}
}
class ClickHandler implements OnClickListener{
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.photoview:
if( null == photoview.getBackground())
{
photoview.setBackgroundResource(R.drawable.img_border);
}else{
photoview.setBackgroundDrawable(null);
}
break;
}
v.layout(left, top, right, bottom);
System.out.println("left===" + left);
System.out.println("top===" + top);
}
}
}
请问有什么好的实现办法。
public class MainActivity extends Activity {
private int screenWidth;
private int screenHeight;
private ImageView bgview;
private ImageView photoview;
private TextView txtview;
int top, right, bottom, left;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 50;
bgview = (ImageView) findViewById(R.id.bgview);
photoview = (ImageView) findViewById(R.id.photoview);
photoview.setOnTouchListener(new TouchHandler());
photoview.setOnClickListener(new ClickHandler());
}
class TouchHandler implements OnTouchListener{
int lastX , lastY, dx, dy;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// if(mode == NONE)
// if(dx < 2 && dy < 2)
{
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
}
break;
case MotionEvent.ACTION_MOVE:
dx = (int) event.getRawX() - lastX;
dy = (int) event.getRawY() - lastY;
// if(mode == DRAG)
{
// photoview.setBackgroundResource(R.drawable.img_border);
left = v.getLeft() + dx;
top = v.getTop() + dy;
right = v.getRight() + dx;
bottom = v.getBottom() + dy;
if(left < 0)
{
left = 0;
right = left + v.getWidth();
}
if(right > screenWidth)
{
right = screenWidth;
left = right - v.getWidth();
}
if(top < 0)
{
top = 0;
bottom = top + v.getHeight();
}
if(bottom > screenHeight)
{
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
}
break;
case MotionEvent.ACTION_UP:
break;
}
if(dx < 2 && dy < 2)
{
return false;
}
return true;
}
}
class ClickHandler implements OnClickListener{
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.photoview:
if( null == photoview.getBackground())
{
photoview.setBackgroundResource(R.drawable.img_border);
}else{
photoview.setBackgroundDrawable(null);
}
break;
}
v.layout(left, top, right, bottom);
System.out.println("left===" + left);
System.out.println("top===" + top);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询