Android 自定義View實(shí)現(xiàn)簡單的相機(jī)預(yù)覽取景框

前言

最近遇到拍照時(shí)帶取景框的需求鳄抒,于是自己寫了個(gè)簡單的取景框配合相機(jī)預(yù)覽界面實(shí)現(xiàn)拍照功能浴捆。本文涉及自定義view的內(nèi)容冬殃,還不太了解同學(xué)的可以移步https://hencoder.com/大牛系統(tǒng)介紹了Android自定義View相關(guān)知識(shí)結(jié)構(gòu),可以讓你對自定義view有個(gè)清晰的認(rèn)識(shí)尚猿,感謝大牛的分享智绸。首先看下完成后的效果:

效果圖

正文

首先創(chuàng)建BackgroundView繼承自View野揪,通常實(shí)現(xiàn)三個(gè)構(gòu)造函數(shù)。
    public BackgroundView(Context context) {
        this(context, null);
    }

    public BackgroundView(Context context,  @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BackgroundView(Context context,  @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mPaint = new Paint();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BackgroundView);
        if (ta != null) {
            mRectLeft = ta.getDimension(R.styleable.BackgroundView_rectLeft, defaultRectLeft);
            mRectTop = ta.getDimension(R.styleable.BackgroundView_rectTop, defaultRectTop);
            mRectRight = ta.getDimension(R.styleable.BackgroundView_rectRight, defaultRectRight);
            mRectBottom = ta.getDimension(R.styleable.BackgroundView_rectBottom, defaultRectBottom);
            mRectCornerRadius = ta.getDimension(R.styleable.BackgroundView_rectCornerRadius, defaultRectCornerRadius);
            mRectOutColor = ta.getColor(R.styleable.BackgroundView_rectOutColor, defaultRectOutColor);
        }
    }

單個(gè)參數(shù)的構(gòu)造方法是在代碼中new對象用的瞧栗,帶AttributeSet參數(shù)的構(gòu)造是在xml中用的:

    <com.example.customcamera.BackgroundView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rectLeft="30dp"
        app:rectTop="100dp"
        app:rectRight="30dp"
        app:rectBottom="300dp"
        app:rectCornerRadius="10dp"
        app:rectOutColor="#99ff0000" />
如果要自定義屬性需要在/res/values/attrs.xml中創(chuàng)建
<resources>
    <declare-styleable name="BackgroundView">
        <attr name="rectLeft" format="dimension" /><!--取景框左邊緣距離屏幕左邊的距離-->
        <attr name="rectTop" format="dimension" /><!--取景框上邊緣距離屏幕上邊的距離-->
        <attr name="rectRight" format="dimension" /><!--取景框右邊緣距離屏幕右邊的距離-->
        <attr name="rectBottom" format="dimension" /><!--取景框下邊緣距離屏幕下邊的距離-->
        <attr name="rectCornerRadius" format="dimension" /><!--取景框圓角半徑-->
        <attr name="rectOutColor" format="color" /><!--取景框外部顏色斯稳,通常為半透明-->
    </declare-styleable>
</resources>

name即屬性名,format即你要聲明的屬性是什么類型如尺寸為dimension迹恐、字符串為string平挑,Android Studio中有提示。attrs.xml中聲明的屬性在構(gòu)造方法中初始化:

    public BackgroundView(Context context,  @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mPaint = new Paint();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BackgroundView);
        if (ta != null) {
            mRectLeft = ta.getDimension(R.styleable.BackgroundView_rectLeft, defaultRectLeft);
            mRectTop = ta.getDimension(R.styleable.BackgroundView_rectTop, defaultRectTop);
            mRectRight = ta.getDimension(R.styleable.BackgroundView_rectRight, defaultRectRight);
            mRectBottom = ta.getDimension(R.styleable.BackgroundView_rectBottom, defaultRectBottom);
            mRectCornerRadius = ta.getDimension(R.styleable.BackgroundView_rectCornerRadius, defaultRectCornerRadius);
            mRectOutColor = ta.getColor(R.styleable.BackgroundView_rectOutColor, defaultRectOutColor);
        }
    }
接下來開始畫取景框,重寫View的onDraw方法super.onDraw是空實(shí)現(xiàn)可以直接刪掉通熄,如果是繼承自其它已經(jīng)實(shí)現(xiàn)了onDraw方法的View如TextView則不能刪除super.onDraw唆涝,至于你的代碼寫在之前還是之后要根據(jù)實(shí)際需求,寫在父方法之前則自定義的畫面在父控件繪制之前繪制唇辨,父控件的繪制會(huì)覆蓋你自己的繪制廊酣,否則相反。Android的繪制是通過Canvas和Paint實(shí)現(xiàn)的Canvas映射到實(shí)際生活中好比是塊畫布赏枚,Paint好比是作畫的畫筆亡驰,畫筆可以控制顏色、粗細(xì)饿幅、形狀等凡辱,google已經(jīng)為我們提供好了一系列api方便我們快速畫出想要的畫面。
    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true);//抗鋸齒
        mPaint.setColor(mRectOutColor);
        // 取景框和全屏
        RectF rect = new RectF(mRectLeft, mRectTop, screenWidth - mRectRight, screenHeight - mRectBottom);
        Path path = new Path();
        path.addRoundRect(rect, mRectCornerRadius, mRectCornerRadius, Path.Direction.CW);
        Region region = new Region();
        region.setPath(path, new Region(0, 0, screenWidth, screenHeight));

        Region region1 = new Region();
        Path path1 = new Path();
        path1.addRect(0, 0, screenWidth, screenHeight, Path.Direction.CW);
        region1.setPath(path1, new Region(0, 0, screenWidth, screenHeight));

        region.op(region1, Region.Op.XOR);// 范圍取異并集

        RegionIterator iterator = new RegionIterator(region);
        Rect rec = new Rect();
        while (iterator.next(rec)) {
            canvas.drawRect(rec, mPaint);
        }
    }

Paint在構(gòu)造方法中已經(jīng)初始化栗恩,mPaint.setAntiAlias(true);設(shè)置抗鋸齒可以使不規(guī)則線條看起來更加平滑透乾,原理是給線條邊緣像素點(diǎn)一個(gè)顏色過渡由深到淺。由于需要中間取景框全透明磕秤,取景框以外的區(qū)域畫上一層半透明的陰影乳乌,所以我用一個(gè)中間圓角矩形和一個(gè)屏幕大小的區(qū)域取異并集得到的就是取景框以外的區(qū)域范圍然后用Canvas直接根據(jù)Paint設(shè)置的顏色、透明度等參數(shù)直接畫出取景框以外的背景即可市咆。

總結(jié)

本文雖然實(shí)現(xiàn)比較簡單但是要先有自定義View的基礎(chǔ)知識(shí)汉操,在這里再次感謝HenCoder朱凱老師的分享。文章中如有錯(cuò)誤歡迎指出蒙兰。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末磷瘤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子搜变,更是在濱河造成了極大的恐慌膀斋,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痹雅,死亡現(xiàn)場離奇詭異,居然都是意外死亡糊识,警方通過查閱死者的電腦和手機(jī)绩社,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赂苗,“玉大人愉耙,你說我怎么就攤上這事“枳蹋” “怎么了朴沿?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我赌渣,道長魏铅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任坚芜,我火速辦了婚禮览芳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鸿竖。我一直安慰自己沧竟,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布缚忧。 她就那樣靜靜地躺著悟泵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪闪水。 梳的紋絲不亂的頭發(fā)上糕非,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音敦第,去河邊找鬼峰弹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛芜果,可吹牛的內(nèi)容都是我干的鞠呈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼右钾,長吁一口氣:“原來是場噩夢啊……” “哼蚁吝!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起舀射,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤窘茁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后脆烟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體山林,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年邢羔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了驼抹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拜鹤,死狀恐怖框冀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情敏簿,我是刑警寧澤明也,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布宣虾,位于F島的核電站,受9級(jí)特大地震影響温数,放射性物質(zhì)發(fā)生泄漏绣硝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一帆吻、第九天 我趴在偏房一處隱蔽的房頂上張望域那。 院中可真熱鬧,春花似錦猜煮、人聲如沸次员。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽淑蔚。三九已至,卻和暖如春愕撰,著一層夾襖步出監(jiān)牢的瞬間刹衫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工搞挣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留带迟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓囱桨,卻偏偏與公主長得像仓犬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子舍肠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容