Android8.1 Camera2修改ShutterButton,拍照及视频按钮修改
一、修改图库打开按钮样式
host/src/com/mediatek/camera/ui/ThumbnailViewManager.java
private RoundedBitmapDrawable createRoundDrawable(Bitmap bitmap, final int color){
......
/*<20180709 OR by xza 0000092 begin>*/
// borderPaint.setColor(BORDER_COLOR);
borderPaint.setColor(Color.parseColor("#ffffff"));
borderPaint.setStrokeCap(Paint.Cap.BUTT);
/*<20180709 OR by xza 0000092 end>*/
......
/*<20180709 OR by xza 0000092 begin>*/
// canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2,
// newBitmapSquareWidth / 2, borderPaint);
int x=canvas.getWidth()/2;
int y=canvas.getHeight()/2;
int r=newBitmapSquareWidth/2;
canvas.drawRoundRect(x-r,y-r,x+r,y+r,12,12,borderPaint);
/*<20180709 OR by xza 0000092 end>*/
......
/*<20180709 OR by xza 0000092 begin>*/
// roundedBitmapDrawable.setCornerRadius(bitmapRadius);
roundedBitmapDrawable.setCornerRadius(12);
/*<20180709 OR by xza 0000092 begin>*/
}
二、添加切换mode按钮
host/res/layout/camera_ui_root.xml
<?xml version="1.0" encoding="utf-8"?>
<com.mediatek.camera.common.widget.RotateLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/app_ui"
android:splitMotionEvents="false">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/animation_root"...>
<FrameLayout
android:id="@+id/camera_ui_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/top_bar" ...>
<LinearLayout ...>
<!--20180709 OR by xza 0000092 layout_width=match_parent layout_marginBottom="18.5dp"-->
<com.mediatek.camera.ui.shutter.ShutterRootLayout
android:id="@+id/shutter_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="38.5dp"></com.mediatek.camera.ui.shutter.ShutterRootLayout>
<View android:id="@+id/center" .../>
<LinearLayout android:id="@+id/indicator_view" ...>
<View android:id="@+id/center_bottom" .../>
<!--20180709 OR by xza 0000092 begin-->
<!--添加mode切换按钮-->
<ImageView
android:id="@+id/shutter_type_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toRightOf="@id/center_bottom"
android:layout_alignParentBottom="true"
android:enabled="true"
android:clickable="true"
android:layout_marginLeft="85dp"
android:src="@drawable/shutter_type_video_ruyi"
android:layout_marginBottom="38.5dp"
android:visibility="gone"
android:scaleType="centerInside"/>
<!--20180709 OR by xza 0000092 end-->
<!--20180709 OR by xza layout_marginBottom==40dp clickable=true android:layout_above="@+id/shutter_type_button" 0000092-->
<RelativeLayout
android:id="@+id/effect"
android:layout_above="@+id/shutter_type_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="140dp"
android:layout_marginLeft="117dp"
android:layout_toRightOf="@id/center_bottom"
android:clickable="false"
android:focusable="false"
android:scaleType="center">
</RelativeLayout>
<!--20180709 OR by xza layout_marginRight=117dp 0000092-->
<com.mediatek.camera.common.widget.RotateImageView android:id="@+id/thumbnail" />
<com.mediatek.camera.common.widget.RotateImageView android:id="@+id/thumbnail_animation" ...>
</RelativeLayout>
<FrameLayout android:id="@+id/feature_root">
<FrameLayout android:id="@+id/quick_switcher_option">
</FrameLayout>
<FrameLayout android:id="@+id/screen_hint_root" ...>
</FrameLayout>
</com.mediatek.camera.common.widget.RotateLayout>
三、修改ShutterButtonManager
host/src/com/mediatek/camera/ui/shutter/ShutterButtonManager.java
public class ShutterButtonManager extends AbstractViewManager implements
ShutterRootLayout.OnShutterChangeListener,
/*<20180709 OR by xza 0000092>*/
View.OnClickListener{
/*<20180709 OR by xza 0000092 begin>*/
//添加modeName
private String mShutterName="Picture";
private String MODE_TYPE_NORMAL;
private String MODE_TYPE_PANO;
private String MODE_TYPE_SLOW;
private ImageView mShutterTypeButton;
private OnModeChanedShutterLister mOnModeChanedShutterLister;
//添加mode改变借口,在CameraAppUI中实现
public interface OnModeChanedShutterLister{
void onModeChanged(String modeName,String modeType);
}
/*<20180709 OR by xza 0000092 end>*/
......
public ShutterButtonManager(IApp app, ViewGroup parentView) {
super(app, parentView);
/*<20180709 OR by xza 0000092 begin>*/
//初始化modeName
MODE_TYPE_NORMAL=(String)mApp.getActivity().getResources().getText(R.string.normal_mode_title);
MODE_TYPE_PANO=(String)mApp.getActivity().getResources().getText(R.string.pano_dialog_title);
MODE_TYPE_SLOW=(String)mApp.getActivity().getResources().getText(R.string.slow_motion_title);
/*<20180709 OR by xza 0000092 end>*/
mShutterButtonListener = new ShutterButtonListenerImpl();
mInflater = (LayoutInflater) app.getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
protected View getView() {
mShutterLayout = (ShutterRootLayout) mApp.getActivity().findViewById(R.id.shutter_root);
mShutterLayout.setOnShutterChangedListener(this);
/*<20180709 OR by xza 0000092 begin>*/
//初始化切换mode按钮,并且注册点击事件
mShutterTypeButton=(ImageView)mApp.getActivity().findViewById(R.id.shutter_type_button);
mShutterTypeButton.setVisibility(View.VISIBLE);
mShutterTypeButton.setOnClickListener(this);
mApp.getAppUi().registerGestureListener(mShutterLayout.getGestureListener(),
SHUTTER_GESTURE_PRIORITY);
/*<20180709 OR by xza 0000092 end>*/
return mShutterLayout;
}
......
/*<20180709 OR by xza 0000092 begin>*/
//返回切换mode按钮
public View getShutterTypeButton(){
return mShutterTypeButton;
}
/*<20180709 OR by xza 0000092 end>*/
/*<20180709 OR by xza 0000092 begin>*/
//set modeChange事件,CameraAppUi中调用
public void setOnModeChanedShutterLister(OnModeChanedShutterLister onModeChanedShutterLister){
mOnModeChanedShutterLister=onModeChanedShutterLister;
}
/*<20180709 OR by xza 0000092 end>*/
}
/*<20180709 OR by xza 0000092 begin>*/
//切换mode按钮点击事件
@Override
public void onClick(View v) {
if(mShutterName.equals("Picture")){
if (mListener != null) {
mListener.onShutterTypeChanged("Video");
mShutterName="Video";
mShutterTypeButton.setImageResource(R.drawable.shutter_type_photo_ruyi);
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_video));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_video));
}
}else if(mShutterName.equals("Video")){
if (mListener != null) {
mListener.onShutterTypeChanged("Picture");
mShutterName="Picture";
mShutterTypeButton.setImageResource(R.drawable.shutter_type_video_ruyi);
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_photo));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_photo));
}
}else if(mShutterName.equals(MODE_TYPE_SLOW)){
mOnModeChanedShutterLister.onModeChanged(MODE_TYPE_NORMAL,"Picture");
mShutterName="Picture";
mShutterTypeButton.setImageResource(R.drawable.shutter_type_video_ruyi);
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_photo));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_photo));
}else if(mShutterName.equals(MODE_TYPE_PANO)){
mOnModeChanedShutterLister.onModeChanged(MODE_TYPE_NORMAL,"Video");
mShutterName="Video";
mShutterTypeButton.setImageResource(R.drawable.shutter_type_photo_ruyi);
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_video));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_video));
}
}
/*<20180709 OR by xza 0000092 end>*/
/*<20180709 OR by xza 0000092 begin>*/
//backPressed事件处理
private IApp.BackPressedListener mBackPressedListener
= new IApp.BackPressedListener() {
@Override
public boolean onBackPressed() {
if (mApp.getActivity().getFragmentManager().getBackStackEntryCount() > 0) {
mApp.getActivity().getFragmentManager().popBackStack();
return true;
}else{
if(!mShutterName.equals("Picture")){
if (mListener != null) {
mListener.onShutterTypeChanged("Picture");
mShutterTypeButton.setImageResource(R.drawable.shutter_type_video_ruyi);
mShutterName="Picture";
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_photo));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_photo));
shutter.setType("Picture");
mOnModeChanedShutterLister.onModeChanged(MODE_TYPE_NORMAL,"Picture");
}
return true;
}else{
return false;
}
}
}
};
/*<20180709 OR by xza 0000092 end>*/
@Override
public void onShutterChangedStart(String newShutterName) {
if (mListener != null) {
/*<20180709 OR by xza 0000092 begin>*/
mShutterName=newShutterName;
/*<20180709 OR by xza 0000092 end>*/
mListener.onShutterTypeChanged(newShutterName);
}
}
@Override
public void setEnabled(boolean enabled) {
......
if(mShutterTypeButton!=null){
/*<20180709 OR by xza 0000092 begin>*/
mShutterTypeButton.setEnabled(enabled);
/*<20180709 OR by xza 0000092 end>*/
}
}
@Override
public void onResume() {
super.onResume();
if (mShutterLayout != null) {
mShutterLayout.onResume();
}
/*<20180709 OR by xza 0000092 begin>*/
//注册backParessed事件
mApp.registerBackPressedListener(mBackPressedListener, IApp.DEFAULT_PRIORITY);
/*<20180709 OR by xza 0000092 end>*/
}
@Override
public void onPause() {
super.onPause();
if (mShutterLayout != null) {
mShutterLayout.onPause();
}
/*<20180709 OR by xza 0000092 begin>*/
//注销backParessed事件
mApp.unRegisterBackPressedListener(mBackPressedListener);
/*<20180709 OR by xza 0000092 end>*/
}
public void registerShutterButton(Drawable drawable, String type, int priority) {
.....
else if ("Video".equals(type)) {
/*<20180709 OR by xza 0000092 begin>*/
//显示一个shutterbutton
// item.mShutterName =
// (String) mApp.getActivity().getResources().getText(R.string.shutter_type_video);
item.mShutterName ="";
/*<20180709 OR by xza 0000092 end>*/
}
mShutterButtons.put(priority, item);
}
public void registerDone() {
......
/*<20180709 OR by xza 0000092 begin>*/
// shutterView.setOnShutterTextClickedListener(mShutterLayout);
/*<20180709 OR by xza 0000092 end>*/
......
}
public void updateModeSupportType(String currentType, String[] types) {
for (int i = 0; i < mShutterLayout.getChildCount(); i++) {
boolean isSupported = false;
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(i);
/*<20180709 OR by xza 0000092 begin>*/
//显示一个shutterbutton
// for (int j = 0; j < types.length; j++) {
// if (types[j].equals(shutter.getType())) {
// isSupported = true;
// }
// }
// if (isSupported) {
// shutter.setVisibility(View.VISIBLE);
// } else {
// shutter.setVisibility(View.INVISIBLE);
// }
if(i==0){
shutter.setVisibility(View.VISIBLE);
}else {
shutter.setVisibility(View.INVISIBLE);
}
/*<20180709 OR by xza 0000092 end>*/
}
String targetType = null;
if (types.length == 1) {
targetType = types[0];
} else {
targetType = currentType;
}
LogHelper.d(TAG, "currentType = " + currentType + " targetType = " + targetType);
/*<20180709 OR by xza 0000092 begin>*/
//显示一个shutterbutton
// for (int i = 0; i < mShutterLayout.getChildCount(); i++) {
// ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(i);
// if (targetType.equals(shutter.getType())) {
// mShutterLayout.updateCurrentShutterIndex(i);
// }
// }
/*<20180709 OR by xza 0000092 >*/
}
public void updateCurrentModeShutter(String type, Drawable drawable,String modeName) {
/*<20180709 OR by xza 0000092 begin>*/
//显示一个shutterbutton
// if (drawable != null) {
// for (int i = 0; i < mShutterLayout.getChildCount(); i++) {
// ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(i);
// if (shutter.getType().equals(type)) {
// shutter.setDrawable(drawable);
// }
// }
// } else {
// for (int i = 0; i < mShutterLayout.getChildCount(); i++) {
// ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(i);
// ShutterItem item;
// for (Integer key : mShutterButtons.keySet()) {
// item = mShutterButtons.get(key);
// if (shutter.getType().equals(item.mShutterType)) {
// shutter.setDrawable(item.mShutterDrawable);
// }
// }
// }
// }
//shutterbutton跟随mode变化切换图片
ShutterView shutter = (ShutterView) mShutterLayout.getChildAt(0);
if(MODE_TYPE_NORMAL.equals(modeName)){
mShutterName="Picture";
mShutterTypeButton.setImageResource(R.drawable.shutter_type_video_ruyi);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_photo));
shutter.setName((String)mApp.getActivity().getResources().getText(R.string.shutter_type_photo));
}else if(MODE_TYPE_PANO.equals(modeName)){
mShutterName=MODE_TYPE_PANO;
mShutterTypeButton.setImageResource(R.drawable.shutter_type_video_ruyi);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_photo));
shutter.setName(MODE_TYPE_PANO);
}else if(MODE_TYPE_SLOW.equals(modeName)){
mShutterName=MODE_TYPE_SLOW;
mShutterTypeButton.setImageResource(R.drawable.shutter_type_photo_ruyi);
shutter.setDrawable(mApp.getActivity().getResources().getDrawable(R.drawable.ic_shutter_drawable_video));
shutter.setName(MODE_TYPE_SLOW);
}
/*<20180709 OR by xza 0000092 begin>*/
}
三、处理ShutterButtonManager
host/src/com/mediatek/camera/ui/CameraAppUI.java
/*<20180709 OR by xza 0000092>*/
private View mShutterTypeButton;
public void onCreate() {
......
/*<20180709 OR by xza 0000092 begin>*/
//获取切换mode按钮
mShutterTypeButton=mShutterManager.getShutterTypeButton();
//设置shuttermanager onmodechanger 监听事件
mShutterManager.setOnModeChanedShutterLister(new OnModeChanedShutterListerImpl());
/*<20180709 OR by xza 0000092 end>*/
......
}
@Override
public void registerMode(List<ModeItem> items) {
......
for (int i = 0; i < items.size(); i++) {
......
if (item.mType.equals("Picture")) {
mShutterManager.registerShutterButton(mApp.getActivity().getResources()
.getDrawable(
R.drawable.ic_shutter_drawable_photo), "Picture", 0);
}
......
}
......
}
@Override
public void applyAllUIVisibilityImmediately(int visibility) {
.......
/*<20180709 OR by xza 0000092 begin>*/
mShutterTypeButton.setVisibility(visibility);
/*<20180709 OR by xza 0000092 end>*/
.......
}
private class ConfigUIHandler extends Handler {
super.handleMessage(msg);
LogHelper.d(TAG, "handleMessage what = " + msg.what);
switch (msg.what) {
case APPLY_ALL_UI_VISIBILITY:
......
/*<20180709 OR by xza 0000092 begin>*/
mShutterTypeButton.setVisibility(visibility);
/*<20180709 OR by xza 0000092 end>*/
......
break;
.......
}
}
private void configUIVisibility(int module, int visibility) {
LogHelper.d(TAG, "configUIVisibility + module " + module + " visibility " + visibility);
switch (module) {
......
case SHUTTER_BUTTON:
mShutterManager.setVisibility(visibility);
/*<20180709 OR by xza 0000092 begin>*/
mShutterTypeButton.setVisibility(visibility);
/*<20180709 OR by xza 0000092 end>*/
break;
......
}
}
private class OnModeChangedListenerImpl implements ModePickerManager.OnModeChangedListener {
@Override
public void onModeChanged(String modeName) {
......
//添加modename参数
/*<20180709 OR by xza 0000092 begin>*/
// mShutterManager.updateCurrentModeShutter(item.mType, item.mShutterIcon);
mShutterManager.updateCurrentModeShutter(item.mType, item.mShutterIcon,item.mModeName);
/*<20180709 OR by xza 0000092 begin>*/
}
}
/*<20180709 OR by xza 0000092 begin>*/
//ShutterButtonManager onmdeoChanged借口实现类
private class OnModeChanedShutterListerImpl implements ShutterButtonManager.OnModeChanedShutterLister{
@Override
public void onModeChanged(String modeName,String modeType) {
if("Picture".equals(modeType)){
ModeItem item = mModeProvider.getModeEntryName(modeName, modeType);
mModeChangeListener.onModeSelected(item.mClassName);
mModePickerManager.updateCurrentModeItem(item);
}else if("Video".equals(modeType)){
ModeItem item = mModeProvider.getModeEntryName(modeName, modeType);
mModeChangeListener.onModeSelected(item.mClassName);
mModePickerManager.updateCurrentModeItem(item);
}
}
}
/*<20180709 OR by xza 0000092 begin>*/
大概的修改就是这样,需要的同学自己可以在优化一下代码。