Android自定義相機界面的實現(xiàn)代碼
我們先實現(xiàn)拍照按鈕的圓形效果哈,Android開發(fā)中,當(dāng)然可以找美工人員設(shè)計圖片,然后直接拿進來,不過我們可以自己寫代碼實現(xiàn)這個效果哈,最常用的的是用layout-list實現(xiàn)圖片的疊加,我們這個layout命名為btn_take_photo.xml,這是一個自定義的drawable文件,所以按照規(guī)范,我們要將它放在drawable文件夾里。
注意:drawable文件夾一般是來放自定義的drawable文件的,可以將它看成自己寫的背景樣式等等哦
解釋代碼:
layer-list里面放3個item,先實現(xiàn)一個白色背景的橢圓,屬性android:shape="oval"是實現(xiàn)橢圓的
android:shape=["rectangle" | "oval" | "line" | "ring"]
shape的形狀,默認(rèn)為矩形,可以設(shè)置為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環(huán)形(ring)
然后再放入一個item,這個item是一個左右上下都等長的橢圓
ok,這樣一個等邊的橢圓就做好了
接著再次放入一個一個藍色背景的橢圓
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <solid android:color="@color/white" /> </shape> </item> <item android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> <shape android:shape="oval"> <solid android:color="@color/blue" /> </shape> </item> <item> <shape android:shape="oval"> <stroke android:width="1dp" android:color="@color/blue" android:dashWidth="0dp" /> </shape> </item> </layer-list>
這是一個界面:activity_take_photo.xml
界面的很簡單,這里只是提供參考學(xué)習(xí)的,解釋代碼:
SurfaceView是用來拍照用的,注意這個類只要和視頻或者拍照的都需要用到,不過項目里一般都是自己寫的。
這些代碼只是參考互相學(xué)習(xí),功能的話,自己還在做,所以先提供這些學(xué)習(xí)的...,希望可以幫助學(xué)習(xí)的人,然后自己寫博客的目的也是對自己學(xué)習(xí)的技術(shù)進行收錄和共享,只是本著互相學(xué)習(xí)的目的。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <!-- 顯示預(yù)覽圖形 --> <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/buttonLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/pic"> <RelativeLayout android:id="@+id/panel_take_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/white" android:gravity="center_vertical" android:padding="2dp"> <Button android:id="@+id/btn_take_photo" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/btn_take_photo" android:layout_centerHorizontal="true" android:layout_alignTop="@+id/iv_album" /> <ImageView android:id="@+id/iv_album" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:padding="5dp" android:src="@drawable/camera_library" /> <ImageView android:id="@+id/title_btn_black" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp" android:padding="5dp" android:src="@drawable/camera_back" /> </RelativeLayout> <LinearLayout android:id="@+id/photo_area" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/panel_take_photo" android:layout_centerVertical="true" android:background="@color/white" android:orientation="horizontal"></LinearLayout> <!-- 自定義的標(biāo)題欄--> <RelativeLayout android:id="@+id/camera_top" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_alignParentTop="true" android:background="@color/black"> <ImageView android:id="@+id/btn_black" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingTop="10dp" android:src="@drawable/back" /> <ImageView android:id="@+id/btn_change" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingBottom="10dp" android:paddingRight="10dp" android:paddingTop="10dp" android:src="@drawable/camera_flip" /> </RelativeLayout> <!-- 自定義的CameraGrid--> <org.personality.camera.ui.view.CameraGrid android:id="@+id/masking" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/photo_area" android:layout_alignParentTop="true" /> <View android:id="@+id/focus_index" android:layout_width="40dp" android:layout_height="40dp" android:layout_above="@id/photo_area" android:background="@drawable/cam_focus" android:visibility="invisible" /> </RelativeLayout> </FrameLayout>
提供自定義CameraGrid類:
/** * 自定義的View * 照相機井字線 * */ public class CameraGrid extends View { private int topBannerWidth = 0; private Paint mPaint; public CameraGrid(Context context) { this(context,null); } public CameraGrid(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init(){ mPaint = new Paint(); mPaint.setColor(Color.WHITE); mPaint.setAlpha(120); mPaint.setStrokeWidth(1f); } private boolean showGrid = true; public boolean isShowGrid() { return showGrid; } public void setShowGrid(boolean showGrid) { this.showGrid = showGrid; } public int getTopWidth() { return topBannerWidth; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)筆記之:Handler Runnable與Thread的區(qū)別詳解
本篇文章是對在Android中Handler Runnable與Thread的區(qū)別進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Windows)
這篇文章主要介紹了利用adt-bundle在Windows下輕松搭建Android開發(fā)環(huán)境與Hello world,感興趣的小伙伴們可以參考一下2016-07-07Flutter?Widget之NavigationBar使用詳解
這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識,在Android應(yīng)用開發(fā)過程中會遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法2016-08-08Android 網(wǎng)絡(luò)請求框架解析之okhttp與okio
HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp如何用于安卓網(wǎng)絡(luò)請求,需要的朋友可以參考下2021-10-10Android進階Hook攔截系統(tǒng)實例化View過程實現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進階Hook攔截系統(tǒng)實例化View過程實現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01詳解Android通過修改配置文件設(shè)置wifi密碼
這篇文章主要介紹了詳解Android通過修改配置文件設(shè)置wifi密碼的相關(guān)資料,需要的朋友可以參考下2017-07-07