我正在尝试播放自定义形状的视频(用户使用运动事件接触点绘制形状)正确绘制了路径,并且视频正在用户绘制的形状内播放...但是问题是宽度和videoview的高度已满布局。我想根据路径的最大顶部,底部,左侧和右侧设置布局参数。
public class VideoSurfaceView extends SurfaceView {
private final static String TAG = "VideoSurfaceView";
private boolean inOtherShape ;
private Path shapePath;
public VideoSurfaceView(Context context) {
super(context);
}
public VideoSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.clipPath(CanvasView.mPath);
super.dispatchDraw(canvas);
}
}
public class CanvasView extends View {
public int width;
public int height;
private Bitmap mBitmap,workingBitmap;
private Canvas mCanvas;
static Path mPath;
static Paint mPaint;
private Paint mBitmapPaint;
private float mX, mY;
static float startingpointx,startingpointy,endingpointx,endingpointy ;
private static final float TOLERANCE = 5;
Context context;
Region r;
static ArrayList<Path> paths = new ArrayList<Path>();
public CanvasView(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;
mPaint = new Paint(Paint.DITHER_FLAG);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.parseColor("#37A1D1"));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
mPath = new Path();
mPaint.setStrokeWidth(4f);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.home);
mCanvas = new Canvas(mBitmap.copy(Bitmap.Config.ARGB_8888, true));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private void startTouch(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
startingpointx = x;
startingpointy = y;
mY = y;
mX = x;
}
private void moveTouch(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOLERANCE || dy >= TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void upTouch() {
mPath.lineTo(mX, mY);
mPath.lineTo(startingpointx,startingpointy);
// mCanvas.drawPath(mPath, mPaint);
// paths.add(mPath);
}
//override the onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
moveTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
upTouch();
invalidate();
break;
}
return true;
}
}
private void setVideoLayout(Shape shape, int width, int height){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
layoutParams.width = width;
layoutParams.height = height;
RectF bounds = new RectF();
CanvasView.mPath.computeBounds(bounds, true);
layoutParams.setMargins((int) bounds.left,(int)bounds.top,(int)bounds.right,(int)bounds.bottom);