Android view 터치 슬라이딩 효과

주요 사고방식 은 부모 구조의onTouch(),방법 을 통 해 미 끄 러 진 위치 와 클릭 한 위 치 를 얻 고 하위 view 의 위 치 를 설정 하 는 것 이다.나의 코드 에서 가장자리 상황 을 고려 했다.주의해 야 할 것 은 Relative Layout 를 사용 하여 imageView 를 예 로 들 면.테스트 결 과 를 보면 bottomMargin 과 rightMargin 의 성능 이 매우 떨 어 지 므 로 left Margin 과 topMargin 으로 포 지 셔 닝 하 는 것 이 좋 습 니 다.
다음은 실행 효과:
这里写图片描述
레이아웃 파일 안에 relativelayout 에 ImageView 가 있 습 니 다.아래 와 같다

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/relativeLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.xingyi.moveviewwithtouch.MainActivity">
<ImageView
 android:id="@+id/imageView"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:background="@android:color/black"/>
</RelativeLayout>
자바 코드 는 다음 과 같 습 니 다.가장자리 위치 가 미 끄 러 지 는 효 과 를 고려 했 습 니 다.고려 하면 가장 왼쪽 가장자리 에 있 는 imageView 의 절반 은 화면 밖 에 있 고 가장 오른쪽 가장자리 에 보이 지 않 을 때 까지 축 소 됩 니 다.

package com.xingyi.moveviewwithtouch;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
 ImageView imageView;
 RelativeLayout relativeLayout;
 int heightRL,widthRL;
 int halfHeight,halfWidth;
 boolean first=true;
 private int widthImg;
 private int heightImg;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView();
 }
 //     
 private void initView() {
  imageView = (ImageView) findViewById(R.id.imageView);
  relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
  //               ,   imageview
  relativeLayout.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
     case MotionEvent.ACTION_MOVE:
      moveView(imageView, motionEvent.getX(), motionEvent.getY());
      break;
     case MotionEvent.ACTION_DOWN:
      getWidthAndHeight();
      moveView(imageView, motionEvent.getX(), motionEvent.getY());
      break;
     default:
      break;
    }
    return true;
   }
  });
 }
 //               ,              
 private void getWidthAndHeight(){
  if(first){
   widthRL=relativeLayout.getWidth();
   heightRL=relativeLayout.getHeight();
   widthImg=imageView.getWidth();
   heightImg=imageView.getHeight();
   halfWidth = imageView.getWidth() / 2;//imageView     
   halfHeight = imageView.getHeight() / 2;//imageView     
   first=false;
  }
 }
 //    , x y   imageView     relativeLayout       
 private void moveView(View view, float x, float y) {
  RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
  //      
  if (x < halfWidth) {//   
   params.leftMargin = 0;//  imageview      0
  } else if (x > widthRL- halfWidth) {
   params.leftMargin = widthRL-widthImg;//  imageview        (params.rightMargin       )
  } else {
   params.leftMargin = (int) (x - halfWidth);//imageview   relativelayout    
  }
  //      
  if (y < halfHeight) {
   params.topMargin = 0;
  } else if (y > heightRL - halfHeight) {
   params.topMargin = heightRL-widthImg;//params.bottomMargin       
  } else {
   params.topMargin = (int) (y - halfHeight);
  }
  view.setLayoutParams(params);
 }
}
총결산
위 에서 말 한 것 은 편집장 님 께 서 소개 해 주신 안 드 로 이 드 뷰 가 터치 에 따라 미 끄 러 지 는 효과 입 니 다.여러분 께 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기