Android - 在视频中添加框架

时间:2017-04-08 17:52:07

标签: java android video svg

我尝试将相框添加到视频中。

首先,我在我的布局中添加了框架波纹视频。:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.hdadmin.demolove.MainActivity">
    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="400dp">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/frame2"/>
        <com.example.hdadmin.demolove.CircleSurface
            android:id="@+id/surface"
            android:gravity="center"
            android:layout_width="300dp"
            android:layout_height="400dp" />
    </FrameLayout>
</RelativeLayout>

我用文件svg:

绘制掩码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.hdadmin.demolove.path.SvgUtil;
import com.example.hdadmin.demolove.path.parser.PathInfo;



public class CircleSurface extends SurfaceView implements SurfaceHolder.Callback{

    private final Path path = new Path();
    private final Matrix pathMatrix = new Matrix();
    private final float[] pathDimensions = new float[2];
    protected final Matrix matrix = new Matrix();
    private PathInfo shapePath;
    private int resId;
    protected int viewWidth;
    protected int viewHeight;
    MediaPlayer player;

    public CircleSurface(Context context) {
        super(context);
        init(context);
    }

    public CircleSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CircleSurface(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        onSizeChanged(w , h);
    }

    public void onSizeChanged(int width, int height) {
        if (viewWidth == width && viewHeight == height) return;
        viewWidth = width;
        viewHeight = height;
        calculateDrawableSizes();
    }

    public void calculateDrawableSizes() {
        int bitmapWidth = getWidth();
        int bitmapHeight = getHeight();

        if (bitmapWidth > 0 && bitmapHeight > 0) {
            float width = Math.round(viewWidth);
            float height = Math.round(viewHeight);

            float scale;
            float translateX = 0;
            float translateY = 0;

            if (bitmapWidth * height > width * bitmapHeight) {
                scale = height / bitmapHeight;
                translateX = Math.round((width / scale - bitmapWidth) / 2f);
            } else {
                scale = width / (float) bitmapWidth;
                translateY = Math.round((height / scale - bitmapHeight) / 2f);
            }

            matrix.setScale(scale, scale);

            calculate(bitmapWidth, bitmapHeight, width, height, scale, translateX, translateY);
            reset();
        }
    }

    public void calculate(int bitmapWidth, int bitmapHeight, float width, float height, float scale, float translateX, float translateY) {
        path.reset();

        pathDimensions[0] = shapePath.getWidth();
        pathDimensions[1] = shapePath.getHeight();

        pathMatrix.reset();

        scale = Math.min(width / pathDimensions[0], height / pathDimensions[1]);
        translateX = Math.round((width - pathDimensions[0] * scale) * 0.5f);
        translateY = Math.round((height - pathDimensions[1] * scale) * 0.5f);
        pathMatrix.setScale(scale, scale);
        pathMatrix.postTranslate(translateX, translateY);
        shapePath.transform(pathMatrix, path);

        pathMatrix.reset();
        matrix.invert(pathMatrix);
        path.transform(pathMatrix);
    }

    public void reset() {
        path.reset();
    }

    private void init(Context context) {
        //TODO: define the circle you actually want
        player = MediaPlayer.create(context, R.raw.video);
        player.setVolume(0 , 0);
        shapePath = SvgUtil.readSvg(context, R.raw.heat2);
        getHolder().addCallback(this);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.clipPath(shapePath.getPath());
        super.dispatchDraw(canvas);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = null;
        player.setDisplay(holder);
        player.start();
        player.setLooping(true);
}

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }


}

我希望我的视频适合框架。但结果不是因为我希望视频与框架偏离:result

0 个答案:

没有答案