Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化图片选择页UI, 适配预览页的横竖屏切换 #195

Merged
merged 4 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ dependencies {

compile 'com.lzy.widget:view-core:0.2.1'

//compile 'com.lzy.widget:imagepicker:0.5.5'
//compile 'com.lzy.widget:imagepicker:0.6.0'
compile project(':imagepicker')
}
2 changes: 1 addition & 1 deletion imagepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 11
Expand Down
1 change: 1 addition & 0 deletions imagepicker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<activity
android:name=".ui.ImagePreviewDelActivity"
android:configChanges="orientation|screenSize"
android:theme="@style/ImagePickerThemeFullScreen"/>

<provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.format.Formatter;
Expand All @@ -16,6 +15,7 @@
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.R;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.util.NavigationBarChangeListener;
import com.lzy.imagepicker.util.Utils;
import com.lzy.imagepicker.view.SuperCheckBox;

Expand All @@ -37,28 +37,24 @@ public class ImagePreviewActivity extends ImagePreviewBaseActivity implements Im
private SuperCheckBox mCbOrigin; //原图
private Button mBtnOk; //确认图片的选择
private View bottomBar;
private View marginView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

isOrigin = getIntent().getBooleanExtra(ImagePreviewActivity.ISORIGIN, false);
imagePicker.addOnImageSelectedListener(this);

mBtnOk = (Button) topBar.findViewById(R.id.btn_ok);
mBtnOk = (Button) findViewById(R.id.btn_ok);
mBtnOk.setVisibility(View.VISIBLE);
mBtnOk.setOnClickListener(this);

bottomBar = findViewById(R.id.bottom_bar);
bottomBar.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Utils.hasVirtualNavigationBar(this)) {
View marginView = findViewById(R.id.margin_bottom);
ViewGroup.LayoutParams layoutParams = marginView.getLayoutParams();
layoutParams.height = Utils.getNavigationBarHeight(this);
marginView.requestLayout();
}

mCbCheck = (SuperCheckBox) findViewById(R.id.cb_check);
mCbOrigin = (SuperCheckBox) findViewById(R.id.cb_origin);
marginView = findViewById(R.id.margin_bottom);
mCbOrigin.setText(getString(R.string.ip_origin));
mCbOrigin.setOnCheckedChangeListener(this);
mCbOrigin.setChecked(isOrigin);
Expand Down Expand Up @@ -94,8 +90,40 @@ public void onClick(View v) {
}
}
});
NavigationBarChangeListener.with(this).setListener(new NavigationBarChangeListener.OnSoftInputStateChangeListener() {
@Override
public void onNavigationBarShow(int orientation, int height) {
marginView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams layoutParams = marginView.getLayoutParams();
if (layoutParams.height == 0) {
layoutParams.height = Utils.getNavigationBarHeight(ImagePreviewActivity.this);
marginView.requestLayout();
}
}

@Override
public void onNavigationBarHide(int orientation) {
marginView.setVisibility(View.GONE);
}
});
NavigationBarChangeListener.with(this, NavigationBarChangeListener.ORIENTATION_HORIZONTAL)
.setListener(new NavigationBarChangeListener.OnSoftInputStateChangeListener() {
@Override
public void onNavigationBarShow(int orientation, int height) {
topBar.setPadding(0, 0, height, 0);
bottomBar.setPadding(0, 0, height, 0);
}

@Override
public void onNavigationBarHide(int orientation) {
topBar.setPadding(0, 0, 0, 0);
bottomBar.setPadding(0, 0, 0, 0);
}
});
}



/**
* 图片添加成功后,修改当前图片的选中数量
* 当调用 addSelectedImageItem 或 deleteSelectedImageItem 都会触发当前回调
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.R;
import com.lzy.imagepicker.util.NavigationBarChangeListener;

/**
* ================================================
Expand Down Expand Up @@ -42,6 +43,18 @@ public void onPageSelected(int position) {
mTitleCount.setText(getString(R.string.ip_preview_image_count, mCurrentPosition + 1, mImageItems.size()));
}
});
NavigationBarChangeListener.with(this, NavigationBarChangeListener.ORIENTATION_HORIZONTAL)
.setListener(new NavigationBarChangeListener.OnSoftInputStateChangeListener() {
@Override
public void onNavigationBarShow(int orientation, int height) {
topBar.setPadding(0, 0, height, 0);
}

@Override
public void onNavigationBarHide(int orientation) {
topBar.setPadding(0, 0, 0, 0);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.lzy.imagepicker.util;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;


/**
* Created by z-chu on 2017/9/4
* 用于监听导航栏的显示和隐藏,主要用于适配华为EMUI系统上虚拟导航栏可随时收起和展开的情况
*/

public class NavigationBarChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {


/**
* 监听竖屏模式导航栏的显示和隐藏
*/
public static final int ORIENTATION_VERTICAL = 1;

/**
* 监听横屏模式导航栏的显示和隐藏
*/
public static final int ORIENTATION_HORIZONTAL = 2;

private Rect rect;

private View rootView;

private boolean isShowNavigationBar = false;

private int orientation;

private OnSoftInputStateChangeListener listener;


public NavigationBarChangeListener(View rootView, int orientation) {
this.rootView = rootView;
this.orientation = orientation;
rect = new Rect();
}

@Override
public void onGlobalLayout() {
rect.setEmpty();
rootView.getWindowVisibleDisplayFrame(rect);
int heightDiff = 0;
if (orientation == ORIENTATION_VERTICAL) {
heightDiff = rootView.getHeight() - (rect.bottom - rect.top);
} else if (orientation == ORIENTATION_HORIZONTAL) {
heightDiff = rootView.getWidth() - (rect.right - rect.left);
}
int navigationBarHeight = Utils.hasVirtualNavigationBar(rootView.getContext()) ?
Utils.getNavigationBarHeight(rootView.getContext()) : 0;
if (heightDiff >= navigationBarHeight && heightDiff < navigationBarHeight * 2) {
if (!isShowNavigationBar && listener != null) {
listener.onNavigationBarShow(orientation, heightDiff);
}
isShowNavigationBar = true;
} else {

if (isShowNavigationBar && listener != null) {
listener.onNavigationBarHide(orientation);
}
isShowNavigationBar = false;
}
}

public void setListener(OnSoftInputStateChangeListener listener) {
this.listener = listener;
}

public interface OnSoftInputStateChangeListener {
void onNavigationBarShow(int orientation, int height);

void onNavigationBarHide(int orientation);
}

public static NavigationBarChangeListener with(View rootView) {
return with(rootView, ORIENTATION_VERTICAL);
}

public static NavigationBarChangeListener with(Activity activity) {
return with(activity.findViewById(android.R.id.content), ORIENTATION_VERTICAL);
}

public static NavigationBarChangeListener with(View rootView, int orientation) {
NavigationBarChangeListener softInputHeightListener = new NavigationBarChangeListener(rootView, orientation);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(softInputHeightListener);
return softInputHeightListener;
}

public static NavigationBarChangeListener with(Activity activity, int orientation) {
return with(activity.findViewById(android.R.id.content), orientation);
}
}
8 changes: 8 additions & 0 deletions imagepicker/src/main/res/drawable/ic_cover_shade.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:startColor="#19000000"
android:endColor="#00000000"
/>
</shape>
5 changes: 5 additions & 0 deletions imagepicker/src/main/res/layout/adapter_image_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
android:scaleType="centerCrop"
android:src="@drawable/ic_default_image" />

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_cover_shade" />

<View
android:id="@+id/mask"
android:layout_width="match_parent"
Expand Down