Android视频的放大和缩小

这个还是在很久以前的时候写的,当时公司有一个需求,就是需要仿360或者是小蚁的app,做一个视频的放大缩小,当时是搜遍了,搜到的都是关于图片的放大缩小等,无奈之下,就自己去研究了一下,布局啊,自定义控件啊,手势啊,话说好久都没有做过纯上层的开发,现在做的智能家居一块的产品,更多的是倾向于底层着一块的实现,现在趁还没有怎么忘记,就把当时写的东西粘出来分享出来吧,希望能给大家啊一点小的帮助;
主要涉及到的东西就是滑动的算法,onLayout的使用,android系统手势的操作,自定义控件的开发等。
也写了好久了:不赘述,直接上代码:

这是自定义的CustomSurfaceView

package com.example.surfaceviewdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;public class CustomSurfaceView extends SurfaceView {public static final String TAG = "MyGesture";GestureDetector mGestureDetector = null;ScaleGestureDetector scaleGestureDetector = null;private int screenHeight = 0;private int screenWidth = 0;/** 记录是拖拉照片模式还是放大缩小照片模式 */private int mode = 0;// 初始状态/** 拖拉照片模式 */private static final int MODE_DRAG = 1;/** 放大缩小照片模式 */private static final int MODE_ZOOM = 2;private static final int MODE_DOUBLE_CLICK = 3;private long firstTime = 0;private static final float SCALE_MAX = 4.0f;private static float touchSlop = 0;private int start_Top = -1, start_Right = -1, start_Left = -1, start_Bottom = -1;private int start_x, start_y, current_x, current_y;View view;private int View_Width = 0;private int View_Height = 0;private int initViewWidth = 0;private int initViewHeight = 0;private int fatherView_W;private int fatherView_H;private int fatherTop;private int fatherBottom;int distanceX = 0;int distanceY = 0;private boolean isControl_Vertical = false;private boolean isControl_Horizal = false;private float ratio = 0.3f;public CustomSurfaceView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public CustomSurfaceView(Context context, AttributeSet attrs) {super(context, attrs);init();}public CustomSurfaceView(Context context) {super(context);init();}@SuppressWarnings("deprecation")private void init() {touchSlop = ViewConfiguration.getTouchSlop();this.setFocusable(true);this.setClickable(true);this.setLongClickable(true);view = CustomSurfaceView.this;screenHeight = ScreenUtils.getScreenHeight(getContext());screenWidth = ScreenUtils.getScreenWidth(getContext());scaleGestureDetector = new ScaleGestureDetector(getContext(), new simpleScaleGestueListener());mGestureDetector = new GestureDetector(new simpleGestureListener());}@Overridepublic boolean onTouchEvent(MotionEvent event) {mGestureDetector.onTouchEvent(event);switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:onTouchDown(event);break;case MotionEvent.ACTION_POINTER_DOWN:onPointerDown(event);break;case MotionEvent.ACTION_MOVE:onTouchMove(event);break;case MotionEvent.ACTION_UP:mode = 0;break;case MotionEvent.ACTION_POINTER_UP:mode = 0;break;default:break;}return scaleGestureDetector.onTouchEvent(event);}/*** 滑动事件分发* * @param event*/private void onTouchMove(MotionEvent event) {int left = 0, top = 0, right = 0, bottom = 0;if (mode == MODE_DRAG) {left = getLeft();right = getRight();top = getTop();bottom = getBottom();distanceX = (int) (event.getRawX() - current_x);distanceY = (int) (event.getRawY() - current_y);if(icallBack!=null){icallBack.getAngle((int)getX(),this.getWidth());}if (touchSlop <= getDistance(distanceX, distanceY)) {left = left + distanceX;right = right + distanceX;bottom = bottom + distanceY;top = top + distanceY;// 水平判断if (isControl_Horizal) {if (left >= 0) {left = 0;right = this.getWidth();}if (right <= screenWidth) {left = screenWidth - this.getWidth();right = screenWidth;}} else {left = getLeft();right = getRight();}// 垂直判断if (isControl_Vertical) {if (top > 0) {top = 0;bottom = this.getHeight();}if (bottom <= start_Bottom) {bottom = start_Bottom;top = fatherView_H - this.getWidth();}} else {top = this.getTop();bottom = this.getBottom();}if (isControl_Horizal || isControl_Vertical) {this.setPosition(left, top, right, bottom);}current_x = (int) event.getRawX();current_y = (int) event.getRawY();}}}/*** 多点触控时* * @param event*/private void onPointerDown(MotionEvent event) {if (event.getPointerCount() == 2) {mode = MODE_ZOOM;}}/*** 按下时的事件* * @param event*/private void onTouchDown(MotionEvent event) {mode = MODE_DRAG;start_x = (int) event.getRawX();start_y = (int) event.getRawY();current_x = (int) event.getRawX();current_y = (int) event.getRawY();View_Width = getWidth();View_Height = getHeight();if (View_Height > fatherView_H) {isControl_Vertical = true;} else {isControl_Vertical = false;}if (View_Width > fatherView_W) {isControl_Horizal = true;} else {isControl_Horizal = false;}}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);if (start_Top == -1) {start_Top = top;start_Left = left;start_Right = right;start_Bottom = bottom;initViewWidth = view.getWidth();initViewHeight = view.getHeight();}}/*** 缩放手势的监听事件* * @author Administrator* */private class simpleScaleGestueListener implements ScaleGestureDetector.OnScaleGestureListener {// 用到的放大缩小的方法@Overridepublic boolean onScale(ScaleGestureDetector detector) {int left = 0, right = 0, top = 0, bottom = 0;float length = 0;if (mode == MODE_ZOOM) {float ratio = detector.getScaleFactor();left = getLeft();top = getTop();bottom = getBottom();right = getRight();if (ratio > 1) { // 放大撞状态length = (int) ((getHeight() * (ratio - 1)) / 7.0);left = (int) (left - length / 2);right = (int) (right + length / 2);bottom = (int) (bottom + length / 2);top = (int) (top - length / 2);if (getWidth() <= (screenWidth * 3) && getHeight() <= (fatherView_H * 3)) {setPosition(left, top, right, bottom);}} else {length = (int) ((getHeight() * (1 - ratio)) / 7.0);left = (int) (left + length / 2);right = (int) (right - length / 2);bottom = (int) (bottom - length / 2);top = (int) (top + length / 2);if (left >= 0) {left = 0;}if (right <= screenWidth) {right = screenWidth;}if (top >= 0) {top = 0;}if (bottom <= fatherView_H) {bottom = fatherView_H;}if (getWidth() > initViewWidth && getHeight() > fatherView_H) {setPosition(left, top, right, bottom);} else {setPosition(start_Left, start_Top, start_Right, start_Bottom);}}}return false;}@Overridepublic boolean onScaleBegin(ScaleGestureDetector detector) {return true;}@Overridepublic void onScaleEnd(ScaleGestureDetector detector) {}}/*** 双击事件的处理* * @author Administrator* */private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener {// 用到的双击的方法public boolean onDoubleTap(MotionEvent e) {Log.i(TAG, "双击屏幕");// 双击屏幕int left = 0, top = 0, right = 0, bottom = 0;int length = 0;left = getLeft();top = getTop();bottom = getBottom();right = getRight();if (getHeight() > fatherView_H) {// 缩小模式Log.i(TAG, "缩小模式");while (getHeight() > fatherView_H) {length = (int) ((getHeight() * ratio) / 5.0);left = (int) (getLeft() + length / 2);right = (int) (getRight() - length / 2);bottom = (int) (getBottom() - length / 2);top = (int) (getTop() + length / 2);if (left >= 0) {left = 0;}if (right <= screenWidth) {right = screenWidth;}if (top >= 0) {top = 0;}if (bottom <= fatherView_H) {bottom = fatherView_H;}if (getWidth() > initViewWidth && getHeight() > fatherView_H) {setPosition(left, top, right, bottom);} else {setPosition(start_Left, start_Top, start_Right, start_Bottom);}try {Thread.sleep(10);} catch (InterruptedException e1) {e1.printStackTrace();}}} else {// 放大模式Log.i(TAG, "放大模式");if (getHeight() <= fatherView_H) {while (getHeight() < initViewHeight * 2) {length = (int) ((getHeight() * ratio) / 5.0);left = (int) (getLeft() - length / 2);right = (int) (getRight() + length / 2);bottom = (int) (getBottom() + length / 2);top = (int) (getTop() - length / 2);if (getWidth() <= (screenWidth * 3) && getHeight() <= (fatherView_H * 3)) {setPosition(left, top, right, bottom);}try {Thread.sleep(10);} catch (InterruptedException e1) {e1.printStackTrace();}}}}return true;}}/** 实现拖动的处理 */private void setPosition(int left, int top, int right, int bottom) {this.layout(left, top, right, bottom);}/*** surfaceView父控件的宽高* * @param fatherView_Width* @param fatherView_Height*/public void setFatherW_H(int fatherView_Width, int fatherView_Height) {this.fatherView_W = fatherView_Width;this.fatherView_H = fatherView_Height;}public void setLayoutParam(float scale) {LinearLayout.LayoutParams layoutParams = (LayoutParams) getLayoutParams();layoutParams.gravity = Gravity.CENTER;layoutParams.width = (int) (scale * (layoutParams.width));layoutParams.height = (int) (scale * (layoutParams.height));setLayoutParams(layoutParams);}/** 获取两点的距离 **/private float getDistance(float distanceX, float distanceY) {return FloatMath.sqrt(distanceX * distanceX + distanceY * distanceY);}public void setFatherTopAndBottom(int fatherTop, int fatherBottom) {this.fatherTop = fatherTop;this.fatherBottom = fatherBottom;}/*** 一定一个接口*/public interface ICoallBack {void getAngle(int angle, int viewW);}/*** 初始化接口变量*/ICoallBack icallBack = null;/*** 自定义控件的自定义事件*/public void setEvent(ICoallBack iBack) {icallBack = iBack;}}

仿360的那个小滑动条

package com.example.surfaceviewdemo;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;public class CustomMonitorMenu extends View {public final int R1 = 100;public final int R2 = 8;//每一格 cos y的差距public float width;public float height;public float screenWidth;public float screenHeight;public int startAngle = 235;  //新的角度 开始时30public float initialAngle = 0.1f;  //初始弧度30public int sweepAngle = 70;  //角度public double newX;public double newY;   //给小圆球提供位置 空间移动到这个位置上面去public Paint mPaint2;public Paint mPaint3;public Paint mPaint4;public boolean flag = false;public Paint mPaint;public Context mContext;public String Sangle = "0";public String Eangle = "120";/*** 一定一个接口*/public interface ICoallBack {void getAngle(int angle);}/*** 初始化接口变量*/ICoallBack icallBack = null;/*** 自定义控件的自定义事件*/public void setEvent(ICoallBack iBack) {icallBack = iBack;}public CustomMonitorMenu(Context context) {super(context);this.mContext = context;this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);initView();
//      setWillNotDraw(false);}public CustomMonitorMenu(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);initView();
//      setWillNotDraw(false);}public void setindex(int initAng, int viewW) {int viewX = (int) (viewW - screenWidth);if (viewX != 0) {initialAngle = Math.abs(initAng) * sweepAngle / viewX;}else {viewW = 1;}if (initialAngle >= 0 && initialAngle <= 70) {initialAngle = 35 - initialAngle;}Log.e("hgz------->", " initAng = " + Math.abs(initAng) + " initialAngle = " + initialAngle + " viewX = " + viewX);invalidate();}private void initView() {mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setColor(Color.BLACK);mPaint.setAntiAlias(true);mPaint.setStrokeWidth(2.0f);mPaint.setStyle(Paint.Style.STROKE);mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿mPaint2.setColor(Color.BLACK);mPaint2.setStrokeWidth(2.0f);mPaint2.setStrokeJoin(Paint.Join.ROUND); // 让画的线圆滑mPaint2.setStrokeCap(Paint.Cap.ROUND);mPaint3 = new Paint();mPaint3.setColor(Color.BLACK);mPaint3.setStrokeWidth(2.0f);mPaint3.setStyle(Paint.Style.STROKE);mPaint3.setAntiAlias(true);mPaint4 = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint4.setStrokeWidth(1);mPaint4.setTextSize(16);mPaint4.setStyle(Paint.Style.STROKE);mPaint4.setColor(Color.BLACK);
// 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX()
//      mPaint4.setTextAlign(Paint.Align.CENTER);//        根据屏幕的宽高确定圆心的坐标DisplayMetrics metric = new DisplayMetrics();((MainActivity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
//      屏幕的宽高screenWidth = metric.widthPixels;screenHeight = metric.heightPixels;
//       圆心位置坐标width = metric.widthPixels / 2.0f;height = metric.heightPixels / 8.0f;Log.e("aaa--->圆心的坐标:", width + "---" + height);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);RectF oval1 = new RectF(width - 100, height - 100, width + 100, height + 100);canvas.drawArc(oval1, startAngle, sweepAngle, false, mPaint);//小弧形Log.e("ooo--->", "圆环的圆心的坐标 : width " + width + "   height " + height);//        getNewLocation(initialAngle); //根据判断 来移动小球 获得新的位置canvas.drawLine(width, (height - R1), width, (height - R1) + 10, mPaint3);canvas.drawText(Sangle, (float) (width - Math.sin(35 * Math.PI / 180) * R1) - 20, (float) (height - Math.cos(35 * Math.PI / 180) * R1) + 10, mPaint4);canvas.drawText(Eangle, (float) (width + Math.sin(35 * Math.PI / 180) * R1) + 8, (float) (height - Math.cos(35 * Math.PI / 180) * R1) + 10, mPaint4);//        canvas.drawCircle((float) newX, (float) newY, R2, mPaint2);canvas.drawLine((float) (width - Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1),(float) (width - Math.sin(35 * Math.PI / 180) * R1) - 5.0f, (float) (height - Math.cos(35 * Math.PI / 180) * R1) - 6.0f, mPaint3);canvas.drawLine((float) (width - Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1),(float) (width - Math.sin(35 * Math.PI / 180) * R1) + 5.0f, (float) (height - Math.cos(35 * Math.PI / 180) * R1) + 6.0f, mPaint3);canvas.drawLine((float) (width + Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1),(float) (width + Math.sin(35 * Math.PI / 180) * R1) + 5.0f, (float) (height - Math.cos(35 * Math.PI / 180) * R1) - 6.0f, mPaint3);canvas.drawLine((float) (width + Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1),(float) (width + Math.sin(35 * Math.PI / 180) * R1) - 5.0f, (float) (height - Math.cos(35 * Math.PI / 180) * R1) + 6.0f, mPaint3);//        canvas.drawCircle((float) (width - Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1), R2, mPaint2);canvas.drawCircle((float) (width - Math.sin(initialAngle * Math.PI / 180) * R1), (float) (height - Math.cos(initialAngle * Math.PI / 180) * R1), R2, mPaint2);Log.e("hgz", "小球的坐标:" + (float) (width - Math.sin(initialAngle * Math.PI / 180) * R1) + "     " + (float) (height - Math.cos(initialAngle * Math.PI / 180) * R1));
//        canvas.drawCircle((float) (width + Math.sin(35 * Math.PI / 180) * R1), (float) (height - Math.cos(35 * Math.PI / 180) * R1), R2, mPaint2);}}

ScreenUtils尺寸工具类

package com.example.surfaceviewdemo;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;public class ScreenUtils {private ScreenUtils()  {  /* cannot be instantiated */  throw new UnsupportedOperationException("cannot be instantiated");  }  /** * 获得屏幕高度 *  * @param context * @return */  public static int getScreenWidth(Context context)  {  WindowManager wm = (WindowManager) context  .getSystemService(Context.WINDOW_SERVICE);  DisplayMetrics outMetrics = new DisplayMetrics();  wm.getDefaultDisplay().getMetrics(outMetrics);  return outMetrics.widthPixels;  }  /** * 获得屏幕宽度 *  * @param context * @return */  public static int getScreenHeight(Context context)  {  WindowManager wm = (WindowManager) context  .getSystemService(Context.WINDOW_SERVICE);  DisplayMetrics outMetrics = new DisplayMetrics();  wm.getDefaultDisplay().getMetrics(outMetrics);  return outMetrics.heightPixels;  }  /** * 获得状态栏的高度 *  * @param context * @return */  public static int getStatusHeight(Context context)  {  int statusHeight = -1;  try  {  Class<?> clazz = Class.forName("com.android.internal.R$dimen");  Object object = clazz.newInstance();  int height = Integer.parseInt(clazz.getField("status_bar_height")  .get(object).toString());  statusHeight = context.getResources().getDimensionPixelSize(height);  } catch (Exception e)  {  e.printStackTrace();  }  return statusHeight;  }  /** * 获取当前屏幕截图,包含状态栏 *  * @param activity * @return */  public static Bitmap snapShotWithStatusBar(Activity activity)  {  View view = activity.getWindow().getDecorView();  view.setDrawingCacheEnabled(true);  view.buildDrawingCache();  Bitmap bmp = view.getDrawingCache();  int width = getScreenWidth(activity);  int height = getScreenHeight(activity);  Bitmap bp = null;  bp = Bitmap.createBitmap(bmp, 0, 0, width, height);  view.destroyDrawingCache();  return bp;  }  /** * 获取当前屏幕截图,不包含状态栏 *  * @param activity * @return */  public static Bitmap snapShotWithoutStatusBar(Activity activity)  {  View view = activity.getWindow().getDecorView();  view.setDrawingCacheEnabled(true);  view.buildDrawingCache();  Bitmap bmp = view.getDrawingCache();  Rect frame = new Rect();  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  int statusBarHeight = frame.top;  int width = getScreenWidth(activity);  int height = getScreenHeight(activity);  Bitmap bp = null;  bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height  - statusBarHeight);  view.destroyDrawingCache();  return bp;  }
}

主Activity

package com.example.surfaceviewdemo;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class SplashActivity extends Activity{Button btnstartPortActivity;Button btnstartLandActivity;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.splash_layout);btnstartLandActivity = (Button) findViewById(R.id.start_landscape_activity);btnstartPortActivity = (Button) findViewById(R.id.start_portable_activity);btnstartPortActivity.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(SplashActivity.this,MainActivity.class);startActivity(intent);}});btnstartLandActivity.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(SplashActivity.this,DemoActivty.class);startActivity(intent);}});}
}

纵屏播放Activity

package com.example.surfaceviewdemo;import java.io.IOException;import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;import com.example.surfaceviewdemo.CustomSurfaceView.ICoallBack;public class MainActivity extends Activity {public static final String TAG = "MyGesture";Button btn_start;Button btn_end;CustomSurfaceView customSurfaceView;private SurfaceHolder surfaceHolder;private MediaPlayer mediaPlayer;String pathVideo;LinearLayout line_view;private int fatherView_W;private int fatherView_H;CustomMonitorMenu customMonitorMenu;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_start = (Button) findViewById(R.id.start_vedio);btn_end = (Button) findViewById(R.id.stop_vedio);line_view = (LinearLayout) findViewById(R.id.line_view);customSurfaceView = (CustomSurfaceView) findViewById(R.id.hv_scrollView);LayoutParams layoutParams = (LayoutParams) customSurfaceView.getLayoutParams();layoutParams.width = (int) (ScreenUtils.getScreenWidth(getApplicationContext()) * (1.2));layoutParams.gravity = Gravity.CENTER;customMonitorMenu = (CustomMonitorMenu) findViewById(R.id.custom_Menu);customSurfaceView.setLayoutParams(layoutParams);initgetViewW_H();btn_start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {playSurfaceView();}});btn_end.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {stopSurfaceView();}});surfaceHolder = customSurfaceView.getHolder();surfaceHolder.setFixedSize(800, 800);surfaceHolder.setKeepScreenOn(true);surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);surfaceHolder.addCallback(new SurfaceHolder.Callback() {@Overridepublic void surfaceCreated(SurfaceHolder holder) {}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();}}});customSurfaceView.setEvent(new ICoallBack() {@Overridepublic void getAngle(int angle, int viewW) {if (customMonitorMenu != null) {customMonitorMenu.setindex(angle, viewW);}}});}private void initgetViewW_H() {line_view.postDelayed(new Runnable() {@Overridepublic void run() {fatherView_W = line_view.getWidth();fatherView_H = line_view.getHeight();Log.i(TAG, "father Top" + line_view.getTop());Log.i(TAG, "father Bottom" + line_view.getBottom());customSurfaceView.setFatherW_H(line_view.getTop(), line_view.getBottom());customSurfaceView.setFatherTopAndBottom(line_view.getTop(), line_view.getBottom());}}, 100);}// 鏆傚仠public void stopSurfaceView() {if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();mediaPlayer.release();mediaPlayer = null;}}// 寮�濮嬫挱鏀捐棰�public void playSurfaceView() {try {if (mediaPlayer == null) {mediaPlayer = new MediaPlayer();}mediaPlayer.reset();AssetFileDescriptor in = getResources().getAssets().openFd("hgz.mp4");mediaPlayer.setDataSource(in.getFileDescriptor(), in.getStartOffset(), in.getLength());mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mediaPlayer.setDisplay(surfaceHolder);mediaPlayer.prepare();mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}}@Overrideprotected void onDestroy() {super.onDestroy();if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();mediaPlayer.release();mediaPlayer = null;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

横屏幕播放Activity

package com.example.surfaceviewdemo;import java.io.IOException;import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;public class DemoActivty extends Activity {public static final String TAG = "MyGesture";Button btn_start;Button btn_end;CustomSurfaceView customSurfaceView;private SurfaceHolder surfaceHolder;private MediaPlayer mediaPlayer;String pathVideo;LinearLayout line_view;private int fatherView_W;private int fatherView_H;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.demo_layout);btn_start = (Button) findViewById(R.id.start_vedio);btn_end = (Button) findViewById(R.id.stop_vedio);line_view = (LinearLayout) findViewById(R.id.line_view);customSurfaceView = (CustomSurfaceView) findViewById(R.id.hv_scrollView);LayoutParams layoutParams = (LayoutParams) customSurfaceView.getLayoutParams();layoutParams.gravity = Gravity.CENTER;customSurfaceView.setLayoutParams(layoutParams);initgetViewW_H();btn_start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {playSurfaceView();}});btn_end.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {stopSurfaceView();}});surfaceHolder = customSurfaceView.getHolder();surfaceHolder.setFixedSize(800, 800);surfaceHolder.setKeepScreenOn(true);surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);surfaceHolder.addCallback(new SurfaceHolder.Callback() {@Overridepublic void surfaceCreated(SurfaceHolder holder) {}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();}}});}private void initgetViewW_H() {line_view.postDelayed(new Runnable() {@Overridepublic void run() {fatherView_W = line_view.getWidth();fatherView_H = line_view.getHeight();Log.i(TAG, "father Top" + line_view.getTop());Log.i(TAG, "father Bottom" + line_view.getBottom());customSurfaceView.setFatherW_H(line_view.getTop(), line_view.getBottom());customSurfaceView.setFatherTopAndBottom(line_view.getTop(), line_view.getBottom());}}, 100);}// 鏆傚仠public void stopSurfaceView() {if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();mediaPlayer.release();mediaPlayer = null;}}// 寮�濮嬫挱鏀捐棰�public void playSurfaceView() {try {if (mediaPlayer == null) {mediaPlayer = new MediaPlayer();}mediaPlayer.reset();AssetFileDescriptor in = getResources().getAssets().openFd("hgz.mp4");mediaPlayer.setDataSource(in.getFileDescriptor(), in.getStartOffset(), in.getLength());mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mediaPlayer.setDisplay(surfaceHolder);mediaPlayer.prepare();mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}}@Overrideprotected void onDestroy() {super.onDestroy();if (mediaPlayer != null && mediaPlayer.isPlaying()) {mediaPlayer.stop();mediaPlayer.release();mediaPlayer = null;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}
}

因为这个代码写了已经有八九个月了,之前很多东西都是总结在有道云笔记当中,这段时间把它转移到Csdn上来,所以才把它拿出来,可能也有些写的不好,希望吧 能给大家一点帮助,源代码是可以直接run的,

源代码地址:源码地址

如果有什么疑问的或者缺陷的地方也可以及时联系作者

谢谢大家的观看

Android视频的放大和缩小相关推荐

  1. Android实现图片放大缩小

    蓝蓝的天 蓝蓝的天,白云朵朵. White clouds in the blue sky. 目录视图 摘要视图 订阅 新版极客头条上线,每天一大波干货     任玉刚:Android开发者的职场规划  ...

  2. Android实现网页的放大与缩小

    以前实现打开某一网的功能都是调用系统内的浏览器,这个实例实现的是利用Webview控件实现网页的打开,放大与缩小的功能. 实现的截图如下: 正常模式的大小: 放大网页: 缩小网页: 实现这个例子 的代 ...

  3. android 屏幕的放大缩小实现

    ios中 UIScrollView能很好的实现放大缩小功能,在anroid这里,scrollview并不能很好的支持放大缩小,而对于简单的图片放大,缩小,android可以使用 ZoomControl ...

  4. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

  5. FPGA视频拼接器的放大和缩小功能

    视频视频器能够把信号源放大和缩小. 对于我们的拼接器而言,它的架构这种: 信号源进入到拼接器中.先进入缩小模块.然后存进DDR中.然后从DDR中读出视频.进入到放大模块,最后依据屏幕的位置,输出到屏幕 ...

  6. 【Qt+FFmpeg】鼠标滚轮放大、缩小、移动——解码播放本地视频(三)

    上一期我们实现了播放.暂停.重播.倍速功能,这期来谈谈如何实现鼠标滚轮放大缩小和移动:如果还没看过上期,请移步 [Qt+FFmpeg]解码播放本地视频(一)_logani的博客-CSDN博客[Qt+F ...

  7. Android——关于图片移动与放大与缩小详解

    最近参加了全国移动应用开发的技能大赛,有些遗憾,从原来必得的二等奖,因为操作问题:没得奖,回来整理下相关技术点: 这算是第一个了吧,详细的写了下图片的放大缩小,根据自己的理解写了个这样的小模块: An ...

  8. android 手势放缩_AIR Android:放大与缩小手势

    放大与缩小手势(1) 放大与缩小手势对应TransformGestureEvent. GESTURE_ZOOM事件类型,使用时要求两个手指触摸屏幕,同时向外或向内做放缩动作,如图3-2所示. 图3-2 ...

  9. Android自定义ImageView(二)——实现双击放大与缩小图片

    效果图: 首先设置图片依据控件的大小来显示在ImageVeiw中 也就是当图片的宽与高小于控件的宽与高的时候,默认不进行对图片进行放大的操作,但是会将图片居中显示,当然使用的时候可以使用自定义的属性i ...

最新文章

  1. 大地发生了变化写具体_小学语文三年级下册期末检测卷 (2)
  2. TensorFlow 笔记2--MNIST手写数字分类
  3. ios 开发框架原始雏形 01
  4. usb连接不上 艾德克斯电源_艾德克斯双范围可编程直流电源IT6800A/B系列
  5. 数学图形(2.18)Hyperbolical conical spiral双曲圆锥螺线
  6. 解决WP表前缀更换后出现的You do not have sufficient permission
  7. 解决 Command “python setup.py egg_info“ failed with error code 1 问题
  8. 第一次作业+105032014142
  9. ai里怎样取消扩展外观_扩展AI:困难的5个原因
  10. Excel函数应用之数据库函数
  11. java字符常量_java字符常量
  12. java的数组排序和去重
  13. 联想新计算机开机黑屏,联想笔记本电脑开机黑屏没反应的原因及解决办法攻略【维修总结】...
  14. php微信商家分账API
  15. C#爬虫 音娱吉他文本和弦谱
  16. 小型直播系统系列-乐聊TV的开发(四)
  17. Vera++ 默认Rules文件功能解读
  18. 关于短视频平台框架搭建与技术选型探讨
  19. N+1 架构支持更高的电源可靠性
  20. Shared Project

热门文章

  1. 教你用Sound Forge声道切换功能编辑音频
  2. MPEG-2标准简介
  3. java 中的ajax_JAVA中的AJAX技术
  4. 一文搞清楚单相ab0到dq0的变换
  5. 可转债两个关键指标解读以及转股注意事项
  6. duilib-CComboUI执行SelectItem无效果排查
  7. 【源码】Wankel旋转式内燃机壳体轮廓的MATLAB程序设计
  8. b站bilibili哔哩哔哩动画视频加速18倍速js代码JavaScript最新2023年
  9. 项目中的防止同用户异地登录问题
  10. LG G3电池续航秒杀三星Galaxy S5