Skip to content

Commit

Permalink
新增动态配制主题
Browse files Browse the repository at this point in the history
  • Loading branch information
罗孟伟 committed Nov 24, 2019
1 parent 924f345 commit d703ff2
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 65 deletions.
58 changes: 56 additions & 2 deletions app/src/main/java/com/luck/pictureselector/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.luck.pictureselector;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
Expand All @@ -21,6 +23,8 @@
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.broadcast.BroadcastAction;
import com.luck.picture.lib.broadcast.BroadcastManager;
import com.luck.picture.lib.style.PictureCropParameterStyle;
import com.luck.picture.lib.style.PictureParameterStyle;
import com.luck.picture.lib.PictureSelector;
Expand All @@ -29,6 +33,7 @@
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.PermissionChecker;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.luck.picture.lib.tools.ToastUtils;
import com.luck.pictureselector.adapter.GridImageAdapter;

import java.util.ArrayList;
Expand Down Expand Up @@ -116,8 +121,8 @@ protected void onCreate(Bundle savedInstanceState) {
default:
// 预览图片 可自定长按保存路径
PictureSelector.create(MainActivity.this)
.themeStyle(themeId)
.setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题, 这个是配合 .theme();结合使用
//.themeStyle(themeId) // xml设置主题
.setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题
.isNotPreviewDownload(true)// 预览图片长按是否可以下载
.loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎,必传项
.openExternalPreview(position, selectList);
Expand All @@ -133,6 +138,10 @@ protected void onCreate(Bundle savedInstanceState) {
PermissionChecker.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE);
}

// 注册外部预览图片删除按钮回调
BroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,
BroadcastAction.ACTION_DELETE_PREVIEW_POSITION);
}

private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
Expand Down Expand Up @@ -417,6 +426,10 @@ private void getDefaultStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(this, R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(this, R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -467,6 +480,10 @@ private void getWhiteStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(this, R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(this, R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_black_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -517,6 +534,10 @@ private void getNumStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(this, R.color.app_color_blue);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(this, R.color.picture_color_fa);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -567,6 +588,10 @@ private void getSinaStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(this, R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(this, R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_black_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -628,4 +653,33 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
break;
}
}


private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BroadcastAction.ACTION_DELETE_PREVIEW_POSITION:
// 外部预览删除按钮回调
Bundle extras = intent.getExtras();
int position = extras.getInt(PictureConfig.EXTRA_PREVIEW_DELETE_POSITION);
ToastUtils.s(context, "delete image index:" + position);
if (position < adapter.getItemCount()) {
selectList.remove(position);
adapter.notifyItemRemoved(position);
}
break;
}
}
};

@Override
protected void onDestroy() {
super.onDestroy();
if (broadcastReceiver != null) {
BroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver,
BroadcastAction.ACTION_DELETE_PREVIEW_POSITION);
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/luck/pictureselector/PhotoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ private void getDefaultStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -423,6 +427,10 @@ private void getWhiteStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_black_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -473,6 +481,10 @@ private void getNumStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(getContext(), R.color.app_color_blue);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_fa);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down Expand Up @@ -523,6 +535,10 @@ private void getSinaStyle() {
mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(getContext(), R.color.picture_color_9b);
// 预览界面底部背景色
mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(getContext(), R.color.picture_color_grey_3e);
// 外部预览界面删除按钮样式
mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_black_delete;
// 外部预览界面是否显示删除按钮
mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true;

// 裁剪主题
mCropParameterStyle = new PictureCropParameterStyle(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.luck.picture.lib;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.PointF;
import android.net.Uri;
Expand All @@ -19,6 +20,8 @@
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.luck.picture.lib.broadcast.BroadcastAction;
import com.luck.picture.lib.broadcast.BroadcastManager;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.dialog.PictureCustomDialog;
Expand Down Expand Up @@ -50,8 +53,8 @@
* @描述: 预览图片
*/
public class PictureExternalPreviewActivity extends PictureBaseActivity implements View.OnClickListener {
private ImageButton left_back;
private TextView tv_title;
private ImageButton ibLeftBack;
private TextView tvTitle;
private PreviewViewPager viewPager;
private List<LocalMedia> images = new ArrayList<>();
private int position = 0;
Expand All @@ -60,23 +63,48 @@ public class PictureExternalPreviewActivity extends PictureBaseActivity implemen
private loadDataThread loadDataThread;
private String downloadPath;
private String mimeType;
private ImageButton ibDelete;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_activity_external_preview);
inflater = LayoutInflater.from(this);
tv_title = findViewById(R.id.picture_title);
left_back = findViewById(R.id.left_back);
tvTitle = findViewById(R.id.picture_title);
ibLeftBack = findViewById(R.id.left_back);
ibDelete = findViewById(R.id.ib_delete);
viewPager = findViewById(R.id.preview_pager);
position = getIntent().getIntExtra(PictureConfig.EXTRA_POSITION, 0);
images = (List<LocalMedia>) getIntent().getSerializableExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST);
left_back.setOnClickListener(this);
ibLeftBack.setOnClickListener(this);
ibDelete.setOnClickListener(this);
ibDelete.setVisibility(config.style != null ? config.style.pictureExternalPreviewGonePreviewDelete
? View.VISIBLE : View.GONE : View.GONE);
initViewPageAdapterData();
initPictureSelectorStyle();
}

/**
* 设置样式
*/
private void initPictureSelectorStyle() {
if (config.style != null) {
if (config.style.pictureTitleTextColor != 0) {
tvTitle.setTextColor(config.style.pictureTitleTextColor);
}
if (config.style.pictureLeftBackIcon != 0) {
ibLeftBack.setImageResource(config.style.pictureLeftBackIcon);
}
if (config.style.pictureExternalPreviewDeleteStyle != 0) {
ibDelete.setImageResource(config.style.pictureExternalPreviewDeleteStyle);
}
}
tvTitle.setBackgroundColor(colorPrimary);
}

private void initViewPageAdapterData() {
tv_title.setText(position + 1 + "/" + images.size());
tvTitle.setText(getString(R.string.picture_preview_image_num,
position + 1, images.size()));
adapter = new SimpleFragmentAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
Expand All @@ -87,8 +115,10 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
}

@Override
public void onPageSelected(int position) {
tv_title.setText(position + 1 + "/" + images.size());
public void onPageSelected(int index) {
tvTitle.setText(getString(R.string.picture_preview_image_num,
index + 1, images.size()));
position = index;
}

@Override
Expand All @@ -99,22 +129,49 @@ public void onPageScrollStateChanged(int state) {

@Override
public void onClick(View v) {
finish();
overridePendingTransition(0, R.anim.picture_anim_a3);
int id = v.getId();
if (id == R.id.left_back) {
finish();
overridePendingTransition(0, R.anim.picture_anim_a3);
} else if (id == R.id.ib_delete) {
if (images != null && images.size() > 0) {
int currentItem = viewPager.getCurrentItem();
images.remove(currentItem);
// 删除通知用户更新
Bundle bundle = new Bundle();
bundle.putInt(PictureConfig.EXTRA_PREVIEW_DELETE_POSITION, currentItem);
BroadcastManager.getInstance(this)
.action(BroadcastAction.ACTION_DELETE_PREVIEW_POSITION)
.extras(bundle).broadcast();
if (images.size() == 0) {
onBackPressed();
return;
}
tvTitle.setText(getString(R.string.picture_preview_image_num,
position + 1, images.size()));
position = currentItem;
adapter.notifyDataSetChanged();
}
}
}

public class SimpleFragmentAdapter extends PagerAdapter {

@Override
public int getCount() {
return images.size();
return images != null ? images.size() : 0;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
(container).removeView((View) object);
}

@Override
public int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
Expand Down Expand Up @@ -237,6 +294,11 @@ private void showDownLoadDialog() {
PictureFileUtils.copyFile(downloadPath, dirPath);
ToastUtils.s(mContext, getString(R.string.picture_save_success) + "\n" + dirPath);

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(dirPath));
intent.setData(uri);
sendBroadcast(intent);

dismissDialog();
} catch (IOException e) {
ToastUtils.s(mContext, getString(R.string.picture_save_error) + "\n" + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,6 @@ public class PicturePreviewActivity extends PictureBaseActivity implements
private Handler mHandler;
private RelativeLayout selectBarLayout;

private void initPictureSelectorStyle() {
if (config.style != null) {
if (config.style.pictureTitleTextColor != 0) {
tv_title.setTextColor(config.style.pictureTitleTextColor);
}
if (config.style.pictureLeftBackIcon != 0) {
picture_left_back.setImageResource(config.style.pictureLeftBackIcon);
}
if (config.style.picturePreviewBottomBgColor != 0) {
selectBarLayout.setBackgroundColor(config.style.picturePreviewBottomBgColor);
}
if (config.style.pictureCheckNumBgStyle != 0) {
tv_img_num.setBackgroundResource(config.style.pictureCheckNumBgStyle);
}
if (config.style.pictureCheckedStyle != 0) {
check.setBackgroundResource(config.style.pictureCheckedStyle);
}
if (config.style.pictureUnCompleteTextColor != 0) {
mTvPictureOk.setTextColor(config.style.pictureUnCompleteTextColor);
}
}
tv_title.setBackgroundColor(colorPrimary);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -198,6 +174,33 @@ public void onPageScrollStateChanged(int state) {
});
}

/**
* 动态设置相册主题
*/
private void initPictureSelectorStyle() {
if (config.style != null) {
if (config.style.pictureTitleTextColor != 0) {
tv_title.setTextColor(config.style.pictureTitleTextColor);
}
if (config.style.pictureLeftBackIcon != 0) {
picture_left_back.setImageResource(config.style.pictureLeftBackIcon);
}
if (config.style.picturePreviewBottomBgColor != 0) {
selectBarLayout.setBackgroundColor(config.style.picturePreviewBottomBgColor);
}
if (config.style.pictureCheckNumBgStyle != 0) {
tv_img_num.setBackgroundResource(config.style.pictureCheckNumBgStyle);
}
if (config.style.pictureCheckedStyle != 0) {
check.setBackgroundResource(config.style.pictureCheckedStyle);
}
if (config.style.pictureUnCompleteTextColor != 0) {
mTvPictureOk.setTextColor(config.style.pictureUnCompleteTextColor);
}
}
tv_title.setBackgroundColor(colorPrimary);
}

/**
* 这里没实际意义,好处是预览图片时 滑动到屏幕一半以上可看到下一张图片是否选中了
*
Expand Down
Loading

0 comments on commit d703ff2

Please sign in to comment.