Android中相機和相冊使用分析
歡迎轉載,但請尊重原創(文章來自不易,轉載請標明轉載出處,謝謝)
在手機應用程序中,使用自帶的相機拍照以及相冊選擇喜歡的圖片是最常見不過的用戶需求,那麼怎麼合理使用相機和相冊來選擇照片是重要的,下面就以項目中實際需求為例進行說明,這裡實現的功能如下:
1 使用相機和相冊選擇圖片,並裁剪較小圖片(常用於剪裁小圖)
2 使用相機和相冊選擇圖片,並裁剪較大圖片(常用於裁剪大圖)
具體的實現功能清楚了,那麼就一一進行說明,具體如下(這裡不會羅列怎麼上傳圖片到服務端,只介紹怎麼使用裁剪和使用相冊和相機)。另外,有圖有真相,我的實現效果圖如下所示:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20141004/2014100408572233.png" alt="\">
上面為我的實現效果圖,接下來按照實現次序列出源代碼文件,具體如下:
Popup.java( 彈窗屬性設置對象類):
public class Popup {
private int xPos;// 彈出窗口的x方向位置
private int yPos;// 彈出窗口的y方向位置
private int vWidth;// 窗口顯示內容的視圖寬度
private int vHeight;// 窗口顯示內容的視圖高度
private int animFadeInOut;// 窗口顯示動畫
private int contentView;// 潛入在窗口的視圖
private View customView;// 潛入的窗口視圖view
private boolean isClickable;// 視圖外部是否可以點擊
private OnDismissListener listener;// 監聽彈窗是否dismiss
private OnTouchListener touchListener;// 監聽觸摸位置
private float bgAlpha;// 背景遮罩的透明度
public int getxPos() {
return xPos;
}
public void setxPos(int xPos) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos(int ypos) {
this.yPos = ypos;
}
public int getvWidth() {
return vWidth;
}
public void setvWidth(int vWidth) {
this.vWidth = vWidth;
}
public int getvHeight() {
return vHeight;
}
public void setvHeight(int vHeight) {
this.vHeight = vHeight;
}
public int getAnimFadeInOut() {
return animFadeInOut;
}
public void setAnimFadeInOut(int animFadeInOut) {
this.animFadeInOut = animFadeInOut;
}
public int getContentView() {
return contentView;
}
public void setContentView(int contentView) {
this.contentView = contentView;
}
public boolean isClickable() {
return isClickable;
}
public void setClickable(boolean isClickable) {
this.isClickable = isClickable;
}
public View getCustomView() {
return customView;
}
public void setCustomView(View customView) {
this.customView = customView;
}
public OnDismissListener getListener() {
return listener;
}
public void setListener(OnDismissListener listener) {
this.listener = listener;
}
public float getBgAlpha() {
return bgAlpha;
}
public void setBgAlpha(float bgAlpha) {
this.bgAlpha = bgAlpha;
}
public OnTouchListener getTouchListener() {
return touchListener;
}
public void setTouchListener(OnTouchListener touchListener) {
this.touchListener = touchListener;
}
PopupDialog.java(彈窗實體類):
public class PopupDialog extends PopupWindow {
public PopupDialog(View view,int width,int height) {
super(view,width,height);
}
}
ViewUtils.java(自定義彈窗工具類):
public class ViewUtils {
private static PopupDialog popupDialog = null;
@SuppressLint("NewApi")
public static PopupDialog createPopupDialog(Context context,Popup dialog) {
dismissPopupDialog();
View view = null;
if(null == dialog.getCustomView()) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(dialog.getContentView(), null);
} else {
view = dialog.getCustomView();
}
view.setOnTouchListener(dialog.getTouchListener());
if(0 != dialog.getBgAlpha()) {
view.setAlpha(dialog.getBgAlpha());
}
popupDialog = new PopupDialog(view,dialog.getvWidth(),dialog.getvHeight());
ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);// follow two
lines is used for back key -00000
popupDialog.setBackgroundDrawable(dw);
popupDialog.setAnimationStyle(dialog.getAnimFadeInOut());
popupDialog.setOutsideTouchable(dialog.isClickable());
popupDialog.setFocusable(true);// not allow user click popupwindow background
event or not permit
popupDialog.setOnDismissListener(dialog.getListener());
popupDialog.update();
return popupDialog;
}
public static void dismissPopupDialog() {
if(null != popupDialog &&
popupDialog.isShowing()) {
popupDialog.dismiss();
popupDialog = null;
}
}
public static boolean isPopupShowing() {
if(null != popupDialog &&
popupDialog.isShowing()) {
return true;
} else {
return false;
}
}
view_cameraalbum_popup_menus.xml(導航大小圖片裁剪彈窗布局):
android:layout_width="match_parent"
android:layout_height="match_parent">
<frameLayout
android:id="@+id/flMaskLayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
android:id="@+id/llHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:layout_alignParentBottom="true">
android:layout_width="match_parent"
android:layout_height="101.0dp"
android:background="@drawable/corners_bk_white"
>
android:id="@+id/tvSmallImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="裁剪小圖"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc"
android:layout_centerVertical="true"
/>
android:id="@+id/tvBigImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="裁剪大圖"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
android:layout_below="@id/tvSmallImage"
/>
android:id="@+id/tvCancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="取消"
android:textColor="#5084FE"
android:background="@drawable/corners_bk_white"
android:gravity="center"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:layout_marginBottom="15dp"
/>
view_cameraalbum_popup_smallimage.xml(裁剪小圖頁面彈窗布局):
android:layout_width="match_parent"
android:layout_height="match_parent">
<frameLayout
android:id="@+id/flMaskLayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
android:id="@+id/llHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:layout_alignParentBottom="true">
android:layout_width="match_parent"
android:layout_height="101.0dp"
android:background="@drawable/corners_bk_white"
>
android:id="@+id/tvAlbumSmallImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="相冊"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc"
android:layout_centerVertical="true"
/>
android:id="@+id/tvCameraSmallImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="拍照"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
android:layout_below="@id/tvAlbumSmallImage"
/>
android:id="@+id/tvCancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="取消"
android:textColor="#5084FE"
android:background="@drawable/corners_bk_white"
android:gravity="center"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:layout_marginBottom="15dp"
/>
view_cameraalbum_popup_bigimage.xml(裁剪大圖頁面彈窗布局):
android:layout_width="match_parent"
android:layout_height="match_parent">
<frameLayout
android:id="@+id/flMaskLayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
android:id="@+id/llHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:layout_alignParentBottom="true">
android:layout_width="match_parent"
android:layout_height="101.0dp"
android:background="@drawable/corners_bk_white"
>
android:id="@+id/tvAlbumBigImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="相冊"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc"
android:layout_centerVertical="true"
/>
android:id="@+id/tvCameraBigImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="拍照"
android:textSize="14sp"
android:textColor="#5084FE"
android:gravity="center"
android:layout_below="@id/tvAlbumBigImage"
/>
android:id="@+id/tvCancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="取消"
android:textColor="#5084FE"
android:background="@drawable/corners_bk_white"
android:gravity="center"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:layout_marginBottom="15dp"
/>
activity_main.xml(首頁面布局):
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:background="#F2F3F4">
android:id="@+id/tvCameraAlbumFuntip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#676767"
android:text="@string/text_camera_album_tip"
/>