视频播放控件(一) 可拖动,变换SurfaceVIEw
1 public class DragSurfaceVIEw extends SurfaceVIEw implements VIEw.OntouchListener { 2 protected int screenWIDth; 3 screenHeight; 4 lastX; 5 lastY; 6 private orileft; 7 oriRight; 8 oritop; 9 oriBottom; 10 dragDirection; 11 static final int top = 0x15; 12 int left = 0x16 13 int BottOM = 0x17 14 int RIGHT = 0x18 15 int left_top = 0x11 16 int RIGHT_top = 0x12 17 int left_BottOM = 0x13 18 int RIGHT_BottOM = 0x14 19 int CENTER = 0x19 20 int offset = 20 21 22 /** 23 * 初始化获取屏幕宽高 24 */ 25 voID initScreenW_H() { 26 screenHeight = getResources().getdisplayMetrics().heightPixels - 40 27 screenWIDth = getResources().getdisplayMetrics().wIDthPixels; 28 Log.i("DragVIEwTAG","DragSurfaceVIEw.initScreenW_H: screenWIDth="+screenWIDth+",screenHeight="+screenHeight); 29 } 30 public DragSurfaceVIEw(Context context) { 31 super(context); 32 setontouchListener(this); 33 initScreenW_H(); 34 35 36 DragSurfaceVIEw(Context context,AttributeSet attrs) { 37 (context,attrs); 38 setontouchListener( 39 40 41 42 public DragSurfaceVIEw(Context context,AttributeSet attrs, defStyleAttr) { 43 44 setontouchListener( 45 46 47 48 49 @OverrIDe 50 boolean ontouch(VIEw v,MotionEvent event) { 51 int action = event.getAction(); 52 if (action == MotionEvent.ACTION_DOWN) { 53 orileft = v.getleft(); 54 oriRight = v.getRight(); 55 oritop = v.gettop(); 56 oriBottom = v.getBottom(); 57 lastY = () event.getRawY(); 58 lastX = () event.getRawX(); 59 dragDirection = getDirection(v,() event.getX(), 60 () event.getY()); 61 } 62 // 处理拖动事件 63 delDrag(v,event,action); 64 if(action==MotionEvent.ACTION_UP){ 65 dragDirection=0 66 67 invalIDate(); 68 return true 69 70 71 72 73 * 获取触摸点flag 74 * 75 * @param v 76 x 77 y 78 @return 79 80 int getDirection(VIEw v,1)">int x,1)"> y) { 81 int left = 82 int right = 83 int bottom = 84 int top = 85 if (x < 40 && y < 40) { 86 return left_top; 87 88 if (y < 40 && right - left - x < 40 89 RIGHT_top; 90 91 if (x < 40 && bottom - top - y < 40 92 left_BottOM; 93 94 if (right - left - x < 40 && bottom - top - y < 40 95 RIGHT_BottOM; 96 97 if (x < 40 98 left; 99 100 if (y < 40101 top;102 103 if (right - left - x < 40104 RIGHT;105 106 if (bottom - top - y < 40107 BottOM;108 109 CENTER;110 111 112 113 * 处理拖动事件114 115 116 event117 action118 119 voID delDrag(VIEw v,MotionEvent event,1)"> action) {120 switch (action) {121 case MotionEvent.ACTION_MOVE:122 int dx = (int) event.getRawX() -123 int dy = (int) event.getRawY() -124 (dragDirection) {125 case left: 左边缘126 left(v,dx);127 break128 case RIGHT: 右边缘129 right(v,1)">130 131 case BottOM: 下边缘132 bottom(v,dy);133 134 case top: 上边缘135 top(v,1)">136 137 case CENTER: 点击中心-->>移动138 center(v,dx,1)">139 140 case left_BottOM: 左下141 142 143 144 case left_top: 左上145 146 147 148 case RIGHT_BottOM: 右下149 150 151 152 case RIGHT_top: 右上153 154 155 156 default:157 158 }159 v.layout(orileft,oritop,oriRight,oriBottom);160 if (dragDirection != CENTER) {161 v.layout(orileft,1)">162 }163 lastX = (164 lastY = (165 Log.i("DragVIEwTAG","DragSurfaceVIEw.delDrag:ACTION_MOVE direction="+dragDirection+",left="+orileft+",top="+oritop+",right="+oriRight+",bottom="+oriBottom);166 167 MotionEvent.ACTION_UP:168 VIEwGroup.LayoutParams newLayoutParams = getNewLayoutParams();169 if(newLayoutParams!=null){170 Log.i("DragVIEwTAG","DragSurfaceVIEw.delDrag:ACTION_UP wIDth="+newLayoutParams.wIDth+",height="+newLayoutParams.height);171 setLayoutParams(newLayoutParams);172 }else {173 Log.e("DragVIEwTAG","DragSurfaceVIEw.delDrag: 父组件类型?"174 175 176 177 178 179 180 181 182 private VIEwGroup.LayoutParams getNewLayoutParams(){183 if(getLayoutParams() instanceof relativeLayout.LayoutParams){184 relativeLayout.LayoutParams lp = (relativeLayout.LayoutParams)getLayoutParams( );185 lp.leftmargin =186 lp.topmargin =187 lp.wIDth = oriRight-orileft;188 lp.height = oriBottom-oritop;189 lp;190 }else FrameLayout.LayoutParams) {191 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();192 lp.leftmargin =193 lp.topmargin =194 lp.wIDth = oriRight -195 lp.height = oriBottom -196 197 198 199 200 201 202 * 触摸点为中心->>移动203 204 205 dx206 dy207 208 voID center(VIEw v,1)">int dx,1)"> dy) {209 orileft += dx;210 oritop += dy;211 oriRight +=212 oriBottom +=213 Log.i("DragVIEwTAG","DragSurfaceVIEw.center: v.left="+v.getleft()+",v.top="+v.gettop());214 if (orileft < -offset) {215 Log.e("DragVIEwTAG","DragSurfaceVIEw.center: 左侧越界,offset="+offset);216 orileft = -offset;217 oriRight = orileft + v.getWIDth();218 219 if (oriRight > screenWIDth + offset) {220 Log.e("DragVIEwTAG","DragSurfaceVIEw.center: 右侧越界,screenWIDth="+screenWIDth+",1)">221 oriRight = screenWIDth + offset;222 orileft = oriRight -223 224 if (oritop < -225 Log.e("DragVIEwTAG","DragSurfaceVIEw.center: 顶部越界,1)">226 oritop = -227 oriBottom = oritop + v.getHeight();228 229 if (oriBottom > screenHeight +230 Log.e("DragVIEwTAG","DragSurfaceVIEw.center: 底部越界,bottom="+oriBottom+",screenHeight="+screenHeight+",1)">231 oriBottom = screenHeight +232 oritop = oriBottom -233 234 v.layout(left,top,right,bottom);235 236 237 238 239 * 触摸点为上边缘240 241 242 243 244 voID top(VIEw v,1)">245 oritop +=246 247 oritop = -248 249 if (oriBottom - oritop - 2 * offset < 200250 oritop = oriBottom - 2 * offset - 200251 252 253 254 255 * 触摸点为下边缘256 257 258 259 260 voID bottom(VIEw v,1)">261 oriBottom +=262 263 oriBottom = screenHeight +264 265 266 oriBottom = 200 + oritop + 2 *267 268 269 270 271 * 触摸点为右边缘272 273 274 275 276 voID right(VIEw v,1)"> dx) {277 oriRight +=278 279 oriRight = screenWIDth +280 281 if (oriRight - orileft - 2 * offset < 200282 oriRight = orileft + 2 * offset + 200283 284 285 286 287 * 触摸点为左边缘288 289 290 291 292 voID left(VIEw v,1)">293 orileft +=294 295 orileft = -296 297 298 orileft = oriRight - 2 * offset - 200299 300 301 302 303 * 获取截取宽度304 305 306 307 getCutWIDth() {308 return getWIDth() - 2 *309 310 311 312 * 获取截取高度313 314 315 316 getCutHeight() {317 return getHeight() - 2 *318 319 }
总结
以上是内存溢出为你收集整理的基于SurfaceView的可拖动视频控件全部内容,希望文章能够帮你解决基于SurfaceView的可拖动视频控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)