Android 圖片截取人物頭像(仿逗拍)

來源:互聯網
上載者:User

Android 圖片截取人物頭像(仿逗拍)
要求

根據給定的頭部模板,截取資源圖。背景圖可支援拖動,縮放,拖動旋轉

分析

定義剪下浮層,實現背景資源圖的操作view

實現繪製剪下浮層
/**     * 初始化繪製筆     */    private void initRectPaint()    {        mFloatPaint = new Paint();        mFloatPaint.setAlpha(mAlpha);        mFloatPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));        mEdgePaint = new Paint();        mEdgePaint.setColor(Color.parseColor(#ff000000));        mEdgePaint.setAlpha(mAlpha);    }@Override    public void draw(Canvas canvas)    {        /** 繪製周圍的背景色 **/        drawEdge(canvas);        /** 繪製中間浮層 **/        canvas.drawBitmap(mCropDrawable, mFloatRect.left, mFloatRect.top,                mFloatPaint);    }
操作圖層手勢
@Override    protected void onDraw(Canvas canvas)    {        canvas.save();        canvas.drawColor(Color.parseColor(#ff1c1c22));        canvas.drawBitmap(mBGHeadBitmap, mFloatRect.left, mFloatRect.top, null);        canvas.drawBitmap(mBGBitmap, mBGgmatrix, null);        mFloatView.draw(canvas);        canvas.restore();    }//手勢操作public boolean onTouchEvent(MotionEvent event)    {        switch (event.getAction() & MotionEvent.ACTION_MASK)        {            case MotionEvent.ACTION_DOWN :                eventMode = EventMode.DRAG;                x_down = event.getX();                y_down = event.getY();                savedMatrix.set(mBGgmatrix);                break;            case MotionEvent.ACTION_POINTER_DOWN :                eventMode = EventMode.ZOOM;                oldDist = spacing(event);                mRotation = rotation(event);                savedMatrix.set(mBGgmatrix);                midPoint(midPoint, event);                break;            case MotionEvent.ACTION_MOVE :                if (eventMode == EventMode.DRAG)                {                    matrix1.set(savedMatrix);                    matrix1.postTranslate(event.getX() - x_down, event.getY()                            - y_down);                    mBGgmatrix.set(matrix1);                    invalidate();                } else if (eventMode == EventMode.ZOOM)                {                    float rotation = rotation(event) - mRotation;                    float newDist = spacing(event);                    float scale = newDist / oldDist;                    /** 縮放 **/                    float values[] = new float[9];                    {                        matrix1.set(savedMatrix);                        matrix1.postScale(scale, scale, midPoint.x, midPoint.y);                    }                    /** 旋轉 **/                    matrix1.postRotate(rotation, midPoint.x, midPoint.y);                    mBGgmatrix.set(matrix1);                    invalidate();                }                break;            case MotionEvent.ACTION_UP :            case MotionEvent.ACTION_POINTER_UP :                eventMode = EventMode.NONE;                break;        }        return true;    }
擷取剪下圖片
        Bitmap tmpBitmap = Bitmap.createBitmap(mScrrenWidth, mScrrenHeight,                Config.ARGB_8888); // 背景圖片        Canvas canvas = new Canvas(tmpBitmap); // 建立畫布        canvas.drawBitmap(mBGHeadBitmap, mFloatRect.left, mFloatRect.top, null);        canvas.drawBitmap(mBGBitmap, mBGgmatrix, null); // 畫圖片        canvas.save(Canvas.ALL_SAVE_FLAG); // 儲存畫布        canvas.restore();        Bitmap ret = Bitmap.createBitmap(tmpBitmap, mFloatRect.left,                mFloatRect.top, mFloatRect.width(), mFloatRect.height(), null,                true);        tmpBitmap.recycle();        tmpBitmap = null;        Bitmap newRet = Bitmap.createBitmap(mFloatRect.width(),                mFloatRect.height(), Config.ARGB_8888);        Canvas canvasHead = new Canvas(newRet);        canvasHead.drawBitmap(ret, 0, 0, null);        Paint paintHead = new Paint();        paintHead.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));        Bitmap crop = BitmapFactory.decodeResource(mContext.getResources(),                R.drawable.headmask);        canvasHead.drawBitmap(crop, 0, 0, paintHead);        return newRet;
效果

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.