我想对视频进行缩放缩放,我现在放大视频,但效果不正确,它不是在手指触摸的地方缩放视频,而是在视频的左下角,如何要解决这个问题?
现在看起来如何:https://www.youtube.com/watch?v=VufMUJaV2Nk
MainActivity代码:
public class MainActivity extends Activity {
// minimum video view width
static final int MIN_WIDTH = 100;
// Root view's LayoutParams
private FrameLayout.LayoutParams mRootParam;
// Custom Video View
private VodView mVodView;
// detector to pinch zoom in/out
private ScaleGestureDetector mScaleGestureDetector;
// detector to single tab
private GestureDetector mGestureDetector;
private String vidAddress= "rtsp://mpv.cdn3.bigCDN.com:554/bigCDN/definst/mp4:bigbuckbunnyiphone_400.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRootParam = (LayoutParams) ((View) findViewById(R.id.root_view)).getLayoutParams();
mVodView = (VodView) findViewById(R.id.vodView1);
// Video Uri
Uri vidUri = Uri.parse(vidAddress);
mVodView.setVideoURI(vidUri);
// set up gesture listeners
mScaleGestureDetector = new ScaleGestureDetector(this, new MyScaleGestureListener());
mGestureDetector = new GestureDetector(this, new MySimpleOnGestureListener());
mVodView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
mScaleGestureDetector.onTouchEvent(event);
return true;
}
});
}
@Override
protected void onResume() {
mVodView.start();
super.onResume();
}
@Override
protected void onPause() {
mVodView.pause();
super.onPause();
}
private class MySimpleOnGestureListener extends SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (mVodView == null)
return false;
if (mVodView.isPlaying())
mVodView.pause();
else
mVodView.start();
return true;
}
}
private class MyScaleGestureListener implements OnScaleGestureListener {
private int mW, mH;
@Override
public boolean onScale(ScaleGestureDetector detector) {
// scale our video view
mW *= detector.getScaleFactor();
mH *= detector.getScaleFactor();
if (mW < MIN_WIDTH) { // limits width
mW = mVodView.getWidth();
mH = mVodView.getHeight();
}
Log.d("onScale", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
mVodView.setFixedVideoSize(mW, mH); // important
mRootParam.width = mW;
mRootParam.height = mH;
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mW = mVodView.getWidth();
mH = mVodView.getHeight();
Log.d("onScaleBegin", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
Log.d("onScaleEnd", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
}
}
}
自定义视图:
public class VodView extends VideoView {
public VodView(Context context) {
super(context);
}
public VodView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VodView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Resize video view by using SurfaceHolder.setFixedSize(...). See {@link android.view.SurfaceHolder#setFixedSize}
* @param width
* @param height
*/
public void setFixedVideoSize(int width, int height)
{
getHolder().setFixedSize(width, height);
}
}
的xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.attracti.scalevideo.MainActivity" >
<com.example.attracti.scalevideo.VodView
android:id="@+id/vodView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>