Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

android - How can i create snap layout like this

ImageView snap in center with two vertical and horizontal line

I want to put ImageView in the layout and move it with my fingers.Then snap to center when image near to center


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found something for you. Let me implement some source code.

 private OnTouchListener onTouchListener() {


return new OnTouchListener() {

   @SuppressLint("ClickableViewAccessibility")
   @Override
   public boolean onTouch(View view, MotionEvent event) {
    
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:
 Toast.makeText(TouchActivity.this,
   "thanks for new location!", Toast.LENGTH_SHORT)
   .show();
 break;
 
case MotionEvent.ACTION_MOVE:
 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
   .getLayoutParams();
 layoutParams.leftMargin = x - xDelta;
 layoutParams.topMargin = y - yDelta;
 layoutParams.rightMargin = 0;
 layoutParams.bottomMargin = 0;
 view.setLayoutParams(layoutParams);
 break;
}
mainLayout.invalidate();
return true;
}


};
 }

To run the source code on imageView

imageView.setOnTouchListener(onTouchListener());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...