关于android中将imageview通过手势进行上下移动的问题
android编程小白一个。求指点:我编了一个程序,实现imageview通过手势进行上下移动,但是现在显示不出效果。求解附上无知凌乱的代码:publicclassMai...
android编程小白一个。求指点:我编了一个程序,实现imageview通过手势进行上下移动,但是现在显示不出效果。求解
附上 无知凌乱 的代码:
public class MainActivity extends Activity implements GestureDetector.OnGestureListener,OnTouchListener {
public ImageView imageView;
public int mtopmargin=700;
public GestureDetector mGestureDetector;
private float mScrollY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageview);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(lp);
lp.height=50;
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
MainActivity.this.mGestureDetector.onTouchEvent(event); return false;
} } ); }
以下由于字数限制我只贴出onsroll部分的代码:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
mScrollY+=distanceY;
LayoutParams lp=(LayoutParams) imageView.getLayoutParams();
if (lp.topMargin >0 && mScrollY > 0) {
lp.topMargin = lp.topMargin + (int) mScrollY;
imageView.setLayoutParams(lp);
}
else if (lp.topMargin < mtopmargin && mScrollY < 0 ) {
lp.topMargin = lp.topMargin + (int) mScrollY;
imageView.setLayoutParams(lp);
}
return false;
} 展开
附上 无知凌乱 的代码:
public class MainActivity extends Activity implements GestureDetector.OnGestureListener,OnTouchListener {
public ImageView imageView;
public int mtopmargin=700;
public GestureDetector mGestureDetector;
private float mScrollY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageview);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(lp);
lp.height=50;
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
MainActivity.this.mGestureDetector.onTouchEvent(event); return false;
} } ); }
以下由于字数限制我只贴出onsroll部分的代码:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
mScrollY+=distanceY;
LayoutParams lp=(LayoutParams) imageView.getLayoutParams();
if (lp.topMargin >0 && mScrollY > 0) {
lp.topMargin = lp.topMargin + (int) mScrollY;
imageView.setLayoutParams(lp);
}
else if (lp.topMargin < mtopmargin && mScrollY < 0 ) {
lp.topMargin = lp.topMargin + (int) mScrollY;
imageView.setLayoutParams(lp);
}
return false;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询