例:
现在窗口下有两个VideoView正在播放视频。vv1和vv2
我拖动vv1到vv2的上方,或有交界处后,vv1和vv2进行互换位置和大小。达到的效果是2个视频互换了位置。这是交换的控件本身还是数据源?我知道是实现onTouch接口里的方法,可是具体该如何实现交换?要保证2个视频不会间断,不会出现短暂黑屏。
androidvideoview拖动交换
现在窗口下有两个VideoView正在播放视频。vv1和vv2
我拖动vv1到vv2的上方,或有交界处后,vv1和vv2进行互换位置和大小。达到的效果是2个视频互换了位置。这是交换的控件本身还是数据源?我知道是实现onTouch接口里的方法,可是具体该如何实现交换?要保证2个视频不会间断,不会出现短暂黑屏。
androidvideoview拖动交换
解决方案 »
- 关于android线程问题
- 高手请进,关于Android3.0版本中PreferenceFragment横竖屏切换dialog消失问题!!!!!!!急~~~~~
- Android 如何逐渐显示一张圆形图片?
- String 编码的问题
- 如何实现 手动停止应用程序后 应用程序的自动重启?
- android 环境搭建问题
- 如何调WindowManagerService里的PerformLayout或者relayoutWindow
- 安卓地图求助!!!
- Android 关于蓝牙mic和speaker的输入输出
- android默认浏览器是否支持本地缓存?
- android json如何解析?
- android 线程池的问题
会不会出现拖动vv1到vv2的上方,但视频内容vv1还是在vv2下方的情况?
现在做到的是vv1可以拖动到vv2上方,但是松手后就回到了原处。
目前只是直接用videoview的setvideopath()播放本地视频来测试拖动效果,以后是在播放rtsp流的基础上实现拖动交换。
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.VideoView;public class MainActivity extends Activity implements OnTouchListener{
GestureDetector mGesture = null;
int eve;
int sl=0,sr=0,st=0,sb=0;
int lastX,lastY;
private FrameLayout ff_vv1;
private FrameLayout ff_vv2;
private VideoView vv1;
private VideoView vv2;
// private VideoView[] btns=new VideoView[2];
private FrameLayout[] btns = new FrameLayout[2];
private int mScremWidth,mScremHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ff_vv1 = (FrameLayout)findViewById(R.id.frameLayout1);
ff_vv2 = (FrameLayout)findViewById(R.id.frameLayout2); vv1 = (MyVideoView)findViewById(R.id.videoView1);
vv2 = (MyVideoView)findViewById(R.id.videoView2);
vv1.setVideoPath("sdcard/v1.mp4");
vv1.start();
vv1.setOnTouchListener(this);
vv2.setOnTouchListener(this);
ff_vv1.setOnTouchListener(this);
ff_vv2.setOnTouchListener(this);
btns[0] = ff_vv1;
btns[1] = ff_vv2;
DisplayMetrics dm=getResources().getDisplayMetrics();
mScremWidth=dm.widthPixels;
mScremHeight=dm.heightPixels-50;
mGesture = new GestureDetector(this, new GestureListener());
}
private void changeset(View btnmove,MotionEvent event){
int eve=event.getAction();
switch (eve) {
case MotionEvent.ACTION_DOWN:
lastX=(int)event.getRawX();
lastY=(int)event.getRawY();
sl=btnmove.getLeft();
sr=btnmove.getRight();
st=btnmove.getTop();
sb=btnmove.getBottom();
btnmove.bringToFront();
break;
case MotionEvent.ACTION_MOVE:
int dx=(int)event.getRawX()-lastX;
int dy=(int)event.getRawY()-lastY;
int l=btnmove.getLeft()+dx;
int r=btnmove.getRight()+dx;
int t=btnmove.getTop()+dy;
int b=btnmove.getBottom()+dy;
if(l<0){
l=0;
r=l+btnmove.getWidth();
}
if(r>mScremWidth){
r=mScremWidth;
l=r-btnmove.getWidth();
}
if(t<0){
t=0;
b=t+btnmove.getHeight();
}
if(b>mScremHeight){
b=mScremHeight;
t=b-btnmove.getHeight();
}
btnmove.layout(l, t, r, b);
lastX=(int)event.getRawX();
lastY=(int)event.getRawY();
break;
case MotionEvent.ACTION_UP:
//无偏移坐标不变
int tt=btnmove.getTop();
if(Math.abs(tt-st)>9){
int min=abstop(btnmove);
FrameLayout btn=btns[min];
if(btnmove.getId()!=btn.getId()){
btnmove.layout(btn.getLeft(), btn.getTop(),btn.getRight(), btn.getBottom());
btn.layout(sl, st, sr, sb);
}else{
Log.i("bbb","btnmove===btn"+btn.getId());
btnmove.layout(sl, st, sr, sb);
}
}else{
//无偏移坐标归原
btnmove.layout(sl, st, sr, sb);
}
break;
default:
break;
}
}
//算出非本按钮次小坐标
private int abstop(View bt){
int btl=bt.getBottom();
int[] tops=new int[btns.length];
Log.i("length", "btns.length="+btns.length);
for(int i=0;i<btns.length;i++){
int temp=btns[i].getBottom();
int abs=Math.abs(btl-temp);
Log.i("abs", "btns["+i+"],abs==="+abs);
tops[i]=abs;
}
int min=0;
int index=0;
int indexz=0;
for(int i=0;i<tops.length;i++){
if(tops[i]==0){
indexz=i;
}
}
int[] temp=tops;
for(int i=0;i<temp.length;i++){
for(int j=i+1;j<temp.length;j++){
if(temp[i]>temp[j]){
int t=temp[i];
temp[i]=temp[j];
temp[j]=t;
}
}
}
////确保都为0是temp[1]不是按钮本身 此处处理让不能底部重合
if(temp[1]!=0){
min=temp[1];
}
Log.i("min", "min=="+min);
tops=new int[btns.length];
Log.i("length", "btns.length="+btns.length);
for(int i=0;i<btns.length;i++){
int tem=btns[i].getBottom();
int abs=Math.abs(btl-tem);
Log.i("abs", "btns["+i+"],abs==="+abs);
tops[i]=abs;
}
for(int i=0;i<tops.length;i++){
if(tops[i]==min){
index=i;
}
}
Log.i("aaa", "index========================"+index);
return index;
}
class GestureListener extends SimpleOnGestureListener{
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub eve=event.getAction();
changeset(v,event);
return mGesture.onTouchEvent(event);
}
}