仿快手编辑视频页面添加贴纸和文字,有移动、旋转、放大、文字大小自动适应的功能

来源:互联网 发布:高中信息技术会考编程 编辑:程序博客网 时间:2024/05/10 17:56


最近在开发视频编辑类的app,涉及到需求中要有快手的类似功能,就去研究了下。本文就是我其中一个功能的研究成果,本文涉及到了StickView、view的绘制、文字自动排版,参考了几个项目后写下了本文。

以下是一些核心代码说明,结尾附上源码下载地址

//添加文字贴纸

  private void addStikerTextView() {        stickerView = new StickerView(this,true);//hasTxt表示是否需要文字        stickerView.setOnStickerTouchListener(this);        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        rl.addRule(RelativeLayout.CENTER_IN_PARENT);        Bitmap bitmap  = BitmapFactory.decodeResource(getResources(),R.mipmap.txt_button_0);        stickerView.setTextDraw(bitmap,28,29.5f,118,85);//加入背景和文字范围        contentLayout.addView(stickerView,rl);    }
public void setTextDraw(Bitmap bitmap, float left, float top,float right,float bottom) {        float x = dpToPx(left);        float y =  dpToPx(top);        float r = dpToPx(right);        float b = dpToPx(bottom);        mOriginTextRect.set(x, y, r, b);//文字的范围        sizeTextView.setBounds(mOriginTextRect);//设置文字范围        if(canvasText != null){            canvasText.translate(x,y);//移动到文字绘制的起始位置        }        setWaterMark(bitmap);//设置贴纸 其中还有文字贴纸相关处理    }
//onDraw方法
mBitmap = originBitmap.copy(Bitmap.Config.ARGB_8888, true);canvasText.setBitmap(mBitmap);canvasText.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));sizeTextView.draw(canvasText);canvas.drawBitmap(mBitmap, mMatrix, mPaint);
//文字的范围控制主要在自定义的SizeAdjustingTextView控件中

    主要看这个方法
private float findNewTextSize(int width, int height, CharSequence text) {        TextPaint textPaint = new TextPaint(getPaint());        float targetTextSize = textPaint.getTextSize();        int textHeight = getTextHeight(text, textPaint, width, targetTextSize);        while(targetTextSize >= mMinTextSize) {            if(textHeight > height){                targetTextSize = Math.max(targetTextSize - 1, mMinTextSize);                textHeight = getTextHeight(text, textPaint, width, targetTextSize);            }else if(textHeight < height){                float tempSize = targetTextSize + 1;                int tempHeight = getTextHeight(text, textPaint, width, tempSize);                if(tempHeight > height){                    break;                }else{                    targetTextSize = tempSize;                    textHeight = tempHeight;                }            }else{                break;            }        }        return targetTextSize;    }
以下为效果预览图



代码下载地址:

源码地址

0 0
原创粉丝点击