Android实时更新界面问题
我用触摸事件更新屏幕上的PercentRelativeLayout布局的MarginTop。但是在滑动的过程中PercentRelativeLayout布局的顶部出现跳动的现象(就是感觉有拖影),请问我要怎么解决这个问题。
具体bug效果录屏地址:http://pan.baidu.com/s/1hsOAnWg
一下是Java代码:
@OnTouch(R.id.percentRelativeLayout_listView) boolean percentRelativeLayout_listView_OnTouchListener(View v, MotionEvent event) { //滑动位置 float currentY; //结束位置 float endY; if (event.getAction() == MotionEvent.ACTION_DOWN) { initY = event.getY(); Log.d("按下手势位置", initY + ""); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { currentY = event.getY(); //Log.d("滑动手势位置", currentY + ""); operationOnTouch(currentY); } else if (event.getAction() == MotionEvent.ACTION_UP) { endY = event.getY(); //Log.d("结束手势位置", endY + ""); operationOnTouch(endY); } return true; } //触摸方向计算 public void operationOnTouch(float Y) { //MarginTop动态值 int margin; //向上滑动 if (initY > Y && (initY - Y) > 5) { //获取原始Margin值 PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) percentRelativeLayout_listView.getLayoutParams(); int upInitMargin = params.topMargin; //滑动距离 int currentMargin = Math.round(initY - Y); if (upInitMargin > 400) { //设置Margin值 if (upInitMargin - currentMargin != 0 || upInitMargin - currentMargin > 0) { margin = upInitMargin - currentMargin; PercentRelativeLayout.LayoutParams lp = (PercentRelativeLayout.LayoutParams) percentRelativeLayout_listView.getLayoutParams(); lp.setMargins(0, margin, 0, 0); percentRelativeLayout_listView.setLayoutParams(lp); } } //重置触摸起点位置,使触摸方向计算更加准确 initY = Y; //向下滑动 } else if (initY < Y && (Y - initY) > 5) { //获取原始Marin值 PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) percentRelativeLayout_listView.getLayoutParams(); int downInitMargin = params.topMargin; //滑动后的位置 int currentMargin = Math.round(downInitMargin + (Y - initY)); if (downInitMargin - (initY - Y) > 0) { PercentRelativeLayout.LayoutParams lp = (PercentRelativeLayout.LayoutParams) percentRelativeLayout_listView.getLayoutParams(); lp.setMargins(0, currentMargin, 0, 0); percentRelativeLayout_listView.setLayoutParams(lp); } //重置触摸起点位置,使触摸方向计算更加准确 initY = Y; } }
以下是布局文件:
<?xml version="1.0" encoding="utf-8"?> <com.zhy.android.percent.support.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.aijiao.amap.myapplication.MainActivity"> <!--地图和抽屉开始--> <com.zhy.android.percent.support.PercentRelativeLayout android:id="@+id/percentRelativeLayout_amap" android:layout_width="0dp" android:layout_height="0dp" app:layout_heightPercent="100%" app:layout_widthPercent="100%"> <!--主地图开始--> <com.zhy.android.percent.support.PercentRelativeLayout android:layout_width="0dp" android:layout_height="0dp" android:background="@color/colorPrimary" android:visibility="invisible" app:layout_heightPercent="100%" app:layout_widthPercent="100%"> <!--主地图布局--> </com.zhy.android.percent.support.PercentRelativeLayout> <!--主地图结束--> <!--小地图开始--> <com.zhy.android.percent.support.PercentRelativeLayout android:layout_width="0dp" android:layout_height="0dp" android:visibility="invisible" app:layout_heightPercent="30%" app:layout_widthPercent="100%"> <!--小地图布局--> </com.zhy.android.percent.support.PercentRelativeLayout> <!--小地图结束--> <!--抽屉开始--> <com.zhy.android.percent.support.PercentRelativeLayout android:id="@+id/percentRelativeLayout_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="300dp" android:background="@color/colorAccent"> <!--android:layout_marginTop="300dp"--> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" app:srcCompat="@drawable/up" /> </com.zhy.android.percent.support.PercentRelativeLayout> <!--抽屉结束--> </com.zhy.android.percent.support.PercentRelativeLayout> <!--地图和抽屉结束--> </com.zhy.android.percent.support.PercentRelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其实就是事件的相应速度要比屏幕绘制要快,事件就是个简单的数据传递,而屏幕绘制涉及到硬件,要复杂缓慢得多。转发事件的时候可以wait一下,不要让事件发送过于频繁,太频繁,绘制那块,CPU、GPU也忙不过来,所以会有拖影现象了。
再就是下面这边文章的情况
http://blog.csdn.net/g475266696/article/details/7580969