Android:ImageVIew onTouch拖动被ScrollView onTouch事件拦截
我在scrollview中使用imageview控制器。当涉及到实现和测试时,我发现滚动总是覆盖onTouch事件而不是imageView(dragImage) 我尝试使用它来启用滚动Android:ImageVIew onTouch拖动被ScrollView onTouch事件拦截,android,android-imageview,ontouchlistener,android-scrollview,Android,Android Imageview,Ontouchlistener,Android Scrollview,我在scrollview中使用imageview控制器。当涉及到实现和测试时,我发现滚动总是覆盖onTouch事件而不是imageView(dragImage) 我尝试使用它来启用滚动 scv.setOnTouchListener(null); 这是要禁用的 scv.setOnTouchListener( new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event)
scv.setOnTouchListener(null);
这是要禁用的
scv.setOnTouchListener( new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
它显示了拖动装置只拖动很小的运动,而不是自由拖动到我想要的地方。当我触摸ImageView而不是scrollView时,您能告诉我禁用滚动的更好方法吗
以下是我的工作经历:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_flight_bank, container, false);
r1 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt1);
r2 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt2);
r3 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt3);
r4 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt4);
r5 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt5);
rTop = (RelativeLayout) rootView.findViewById(R.id.rTop);
scv = (ScrollView) rootView.findViewById(R.id.scrollView1);
initialise(rootView);
return rootView;
}
private void initialise(View rootView) {
// TODO Auto-generated method stub
tvCustom= (TextView) rootView.findViewById(R.id.ttvCustomize);
tvRename= (TextView) rootView.findViewById(R.id.tvRename);
tvNormal= (TextView) rootView.findViewById(R.id.tvNorma);
tvExper= (TextView) rootView.findViewById(R.id.txExper);
...
dragImage = (ImageView) rootView.findViewById(R.id.dragImage);
//dragImage.setVisibility(View.GONE);
dragImage.setOnTouchListener(new OnTouchListener(){
private int _xDelta;
private int _yDelta;
@Override
public boolean onTouch(View v, 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) dragImage
.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_MOVE:
RelativeLayout.LayoutParams ParamsA = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();
ParamsA.leftMargin = X - _xDelta;
ParamsA.topMargin = Y - _yDelta;
ParamsA.rightMargin = -250;
ParamsA.bottomMargin = -250;
dragImage.setLayoutParams(ParamsA);
break;
}
return true;
}
});
解决方案:
在开始处插入(onCreteView)
在setOnTouchListener中,onTouch方法:
v.getParent().requestDisallowInterceptTouchEvent(true);
将以下代码添加到您的
MotionEvent.ACTION\u DOWN
和MotionEvent.ACTION\u MOVE
案例应该可以解决您的问题:
dragImage.getParent().requestDisallowInterceptTouchEvent(true);
我终于开始工作了,你也需要在onTouch事件中返回true。以下代码将缩放图像以填充imageview像素宽度。用户可以拖动封面图片来重新定位,您需要跟踪Y矩阵以便以后显示。ImageView比例类型设置为“矩阵”
//declare in activity
private float lastY = 0;
//Added to onCreate of Activity
if (CoverPic!=null){
CoverPic.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastY = event.getY();
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float[] values = new float[9];
Matrix m = CoverPic.getImageMatrix();
m.getValues(values);
float y = values[Matrix.MTRANS_Y];
float deltaY = lastY - event.getY();
y -= deltaY;
//original height / original width x new width = new height
int newwidth = CoverPic.getWidth();
float curwidth = CoverPic.getMeasuredWidth();
curwidth = CoverPic.getDrawable().getBounds().width();
float newscale = newwidth / curwidth;
//We need to limit y Max and Min
int viewheight = CoverPic.getHeight();
float curheight = CoverPic.getDrawable().getBounds().height() * newscale;
float limitmove = curheight - viewheight;
limitmove = limitmove * -1;
if (y<limitmove) y = limitmove;
if (y>0) y = 0;
Matrix matrix = new Matrix();
matrix.postScale(newscale, newscale);
matrix.postTranslate(values[Matrix.MTRANS_X], y);
profile.setCoverOffY(y); //Update profile cover for saving
CoverPic.setImageMatrix(matrix);
CoverPic.invalidate();
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
return false;
}
}
);
}
//在活动中声明
私人浮动弹性=0;
//添加到活动的onCreate中
if(CoverPic!=null){
CoverPic.setOnTouchListener(新的OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
lastY=event.getY();
CoverPic.getParent().RequestDisallowWinterCeptTouchEvent(true);
返回true;
}
else if(event.getAction()==MotionEvent.ACTION\u MOVE){
浮点[]值=新浮点[9];
矩阵m=CoverPic.getImageMatrix();
m、 获取值(值);
float y=值[Matrix.MTRANS_y];
float deltaY=lastY-event.getY();
y-=三角洲;
//原始高度/原始宽度x新宽度=新高度
int newwidth=CoverPic.getWidth();
float curwidth=CoverPic.getMeasuredWidth();
curwidth=CoverPic.getDrawable().getBounds().width();
float newscale=newwidth/curwidth;
//我们需要限制y Max和Min
int viewheight=CoverPic.getHeight();
float curheight=CoverPic.getDrawable().getBounds().height()*新闻缩放;
float limitmove=curheight-viewheight;
limitmove=limitmove*-1;
如果(y0)y=0;
矩阵=新矩阵();
矩阵。后标度(新标度,新标度);
矩阵后转换(值[matrix.MTRANS_X],y);
profile.setCoverOffY(y);//更新配置文件封面以保存
CoverPic.setImageMatrix(矩阵);
CoverPic.invalidate();
CoverPic.getParent().RequestDisallowWinterCeptTouchEvent(true);
返回true;
}
else if(event.getAction()==MotionEvent.ACTION\u UP){
CoverPic.getParent().RequestDisallowWinterCeptTouchEvent(true);
返回true;
}
返回false;
}
}
);
}
你能举个例子说明你想做什么吗?您是否有一个包含ImageViews
的列表,并且您正试图从一行中拖入图像?Thanx它为我工作
//declare in activity
private float lastY = 0;
//Added to onCreate of Activity
if (CoverPic!=null){
CoverPic.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastY = event.getY();
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float[] values = new float[9];
Matrix m = CoverPic.getImageMatrix();
m.getValues(values);
float y = values[Matrix.MTRANS_Y];
float deltaY = lastY - event.getY();
y -= deltaY;
//original height / original width x new width = new height
int newwidth = CoverPic.getWidth();
float curwidth = CoverPic.getMeasuredWidth();
curwidth = CoverPic.getDrawable().getBounds().width();
float newscale = newwidth / curwidth;
//We need to limit y Max and Min
int viewheight = CoverPic.getHeight();
float curheight = CoverPic.getDrawable().getBounds().height() * newscale;
float limitmove = curheight - viewheight;
limitmove = limitmove * -1;
if (y<limitmove) y = limitmove;
if (y>0) y = 0;
Matrix matrix = new Matrix();
matrix.postScale(newscale, newscale);
matrix.postTranslate(values[Matrix.MTRANS_X], y);
profile.setCoverOffY(y); //Update profile cover for saving
CoverPic.setImageMatrix(matrix);
CoverPic.invalidate();
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
return false;
}
}
);
}