Skip to content

Commit

Permalink
1. 优化kotlin代码
Browse files Browse the repository at this point in the history
  • Loading branch information
aaatttcccc committed Mar 4, 2022
1 parent ff283d4 commit a56f122
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.zhongjh.albumcamerarecorder.album.utils.PhotoMetadataUtils;
import com.zhongjh.cameraapp.R;

import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

Expand All @@ -50,7 +52,7 @@ public Set<MimeType> constraintTypes() {
}

@Override
public IncapableCause filter(Context context, MultiMedia item) {
public IncapableCause filter(@NotNull Context context, @NotNull MultiMedia item) {
if (!needFiltering(context, item)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.zhongjh.albumcamerarecorder.album.engine.ImageEngine;
import com.zhongjh.cameraapp.R;

import org.jetbrains.annotations.NotNull;


/**
* {@link ImageEngine} implementation using Glide.
Expand All @@ -34,7 +36,7 @@
public class Glide4Engine implements ImageEngine {

@Override
public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
public void loadThumbnail(@NotNull Context context, int resize, @NotNull Drawable placeholder, @NotNull ImageView imageView, @NotNull Uri uri) {
Glide.with(context)
.asBitmap() // some .jpeg files are actually gif
.load(uri)
Expand All @@ -46,8 +48,8 @@ public void loadThumbnail(Context context, int resize, Drawable placeholder, Ima
}

@Override
public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView,
Uri uri) {
public void loadGifThumbnail(@NotNull Context context, int resize, @NotNull Drawable placeholder, @NotNull ImageView imageView,
@NotNull Uri uri) {
Glide.with(context)
.asBitmap() // some .jpeg files are actually gif
.load(uri)
Expand All @@ -59,7 +61,7 @@ public void loadGifThumbnail(Context context, int resize, Drawable placeholder,
}

@Override
public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
public void loadImage(@NotNull Context context, int resizeX, int resizeY, @NotNull ImageView imageView, @NotNull Uri uri) {
Glide.with(context)
.load(uri)
.apply(new RequestOptions()
Expand All @@ -71,7 +73,7 @@ public void loadImage(Context context, int resizeX, int resizeY, ImageView image
}

@Override
public void loadUrlImage(Context context, ImageView imageView, String url) {
public void loadUrlImage(@NotNull Context context, @NotNull ImageView imageView, @NotNull String url) {
Glide.with(context)
.load(url)
.apply(new RequestOptions()
Expand All @@ -82,7 +84,7 @@ public void loadUrlImage(Context context, ImageView imageView, String url) {
}

@Override
public void loadUriImage(Context context, ImageView imageView, Uri uri) {
public void loadUriImage(@NotNull Context context, @NotNull ImageView imageView, @NotNull Uri uri) {
Glide.with(context)
.load(uri)
.apply(new RequestOptions()
Expand All @@ -93,7 +95,7 @@ public void loadUriImage(Context context, ImageView imageView, Uri uri) {
}

@Override
public void loadDrawableImage(Context context, ImageView imageView, Integer resourceId) {
public void loadDrawableImage(@NotNull Context context, @NotNull ImageView imageView, int resourceId) {
Glide.with(context)
.load(resourceId)
.apply(new RequestOptions()
Expand All @@ -104,7 +106,7 @@ public void loadDrawableImage(Context context, ImageView imageView, Integer reso
}

@Override
public void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
public void loadGifImage(@NotNull Context context, int resizeX, int resizeY, @NotNull ImageView imageView, @NotNull Uri uri) {
Glide.with(context)
.asGif()
.load(uri)
Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/com/zhongjh/cameraapp/phone/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.zhongjh.albumcamerarecorder.settings.RecorderSetting;
import com.zhongjh.cameraapp.BaseActivity;
import com.zhongjh.cameraapp.R;
import com.zhongjh.cameraapp.configuration.ImageCompressionLuBan;
import com.zhongjh.cameraapp.configuration.GifSizeFilter;
import com.zhongjh.cameraapp.configuration.Glide4Engine;
import com.zhongjh.cameraapp.configuration.ImageCompressionLuBan;
import com.zhongjh.cameraapp.databinding.ActivityMainBinding;
import com.zhongjh.common.entity.LocalFile;
import com.zhongjh.common.entity.MediaExtraInfo;
Expand Down Expand Up @@ -220,15 +220,11 @@ protected void openMain(int alreadyImageCount, int alreadyVideoCount, int alread
// 是否压缩图片
if (mBinding.cbIsCompressImage.isChecked()) {
mGlobalSetting.setOnImageCompressionInterface(new ImageCompressionLuBan());
} else {
mGlobalSetting.setOnImageCompressionInterface(null);
}

// 是否压缩视频
if (mBinding.cbIsCompressVideo.isChecked()) {
mGlobalSetting.videoCompress(new VideoCompressManager());
} else {
mGlobalSetting.videoCompress(null);
}

// 自定义路径,如果其他子权限设置了路径,那么以子权限为准
Expand Down Expand Up @@ -274,7 +270,7 @@ private void initForResult() {
mGlobalSetting.forResult(new OnResultCallbackListener() {

@Override
public void onResult(List<LocalFile> result) {
public void onResult(@NotNull List<? extends LocalFile> result) {
for (LocalFile localFile : result) {
Log.i(TAG, "onResult id:" + localFile.getId());
Log.d(TAG, "onResult 绝对路径:" + localFile.getPath());
Expand All @@ -295,10 +291,12 @@ public void onResult(List<LocalFile> result) {
Log.d(TAG, "onResult 具体类型:" + localFile.getMimeType());
// 某些手机拍摄没有自带宽高,那么我们可以自己获取
if (localFile.getWidth() == 0 && localFile.isVideo()) {
MediaExtraInfo mediaExtraInfo = MediaUtils.getVideoSize(getApplication(), localFile.getPath());
localFile.setWidth(mediaExtraInfo.getWidth());
localFile.setHeight(mediaExtraInfo.getHeight());
localFile.setDuration(mediaExtraInfo.getDuration());
if (localFile.getPath() != null) {
MediaExtraInfo mediaExtraInfo = MediaUtils.getVideoSize(getApplication(), localFile.getPath());
localFile.setWidth(mediaExtraInfo.getWidth());
localFile.setHeight(mediaExtraInfo.getHeight());
localFile.setDuration(mediaExtraInfo.getDuration());
}
}
Log.d(TAG, "onResult 宽高: " + localFile.getWidth() + "x" + localFile.getHeight());
Log.d(TAG, UriUtils.uriToFile(getApplicationContext(), localFile.getUri()).getPath());
Expand All @@ -307,7 +305,7 @@ public void onResult(List<LocalFile> result) {
}

@Override
public void onResultFromPreview(List<MultiMedia> result, boolean apply) {
public void onResultFromPreview(@NotNull List<? extends MultiMedia> result, boolean apply) {
if (apply) {
for (MultiMedia multiMedia : result) {
// 绝对路径,AndroidQ如果存在不属于自己App下面的文件夹则无效
Expand Down
21 changes: 16 additions & 5 deletions common/src/main/java/com/zhongjh/common/entity/LocalFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ open class LocalFile : Parcelable {
constructor()

/**
* 从localFile赋值到另外一个新的localFile
* 之所以这样做是因为Parcelable如果使用的是看似父类其实是子类就会出问题
* 从 localFile 赋值到另外一个新的 localFile
* 之所以这样做是因为 Parcelable 如果使用的是看似父类其实是子类就会出问题
*/
constructor(localFile: LocalFile) : super() {
id = localFile.id
Expand All @@ -85,14 +85,24 @@ open class LocalFile : Parcelable {
/**
* 赋值一个新的path,借由这个新的path,修改相关参数
*/
constructor(context: Context, mediaStoreCompat: MediaStoreCompat, localFile: LocalFile, compressionFile: File) : super() {
constructor(
context: Context,
mediaStoreCompat: MediaStoreCompat,
localFile: LocalFile,
compressionFile: File
) : super() {
updateFile(context, mediaStoreCompat, localFile, compressionFile)
}

/**
* 修改新的file
*/
fun updateFile(context: Context, mediaStoreCompat: MediaStoreCompat, localFile: LocalFile, compressionFile: File) {
fun updateFile(
context: Context,
mediaStoreCompat: MediaStoreCompat,
localFile: LocalFile,
compressionFile: File
) {
id = localFile.id
this.path = compressionFile.absolutePath
this.uri = mediaStoreCompat.getUri(compressionFile.absolutePath)
Expand All @@ -102,7 +112,8 @@ open class LocalFile : Parcelable {
oldPath = localFile.oldPath
oldUri = localFile.oldUri
if (isImageOrGif()) {
val imageWidthAndHeight: IntArray = MediaUtils.getImageWidthAndHeight(compressionFile.absolutePath)
val imageWidthAndHeight: IntArray =
MediaUtils.getImageWidthAndHeight(compressionFile.absolutePath)
height = imageWidthAndHeight[1]
width = imageWidthAndHeight[0]
} else if (isVideo()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
/*
* Copyright 2017 Zhihu Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zhongjh.albumcamerarecorder.album.engine;
package com.zhongjh.albumcamerarecorder.album.engine

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView

/**
* 图片不同加载方式
* Image loader interface. There are predefined {@link com.zhongjh.albumcamerarecorder.album.engine.impl.GlideEngine}
* and {@link com.zhongjh.albumcamerarecorder.album.engine.impl.PicassoEngine}.
* Image loader interface. There are predefined [com.zhongjh.albumcamerarecorder.album.engine.impl.GlideEngine]
* and [com.zhongjh.albumcamerarecorder.album.engine.impl.PicassoEngine].
* @author zhongjh
*/
public interface ImageEngine {

interface ImageEngine {
/**
* 加载静态图像资源的缩略图
* 大部分场景用于相册
Expand All @@ -38,7 +22,13 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param uri 加载图像的URI
*/
void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri);
fun loadThumbnail(
context: Context,
resize: Int,
placeholder: Drawable,
imageView: ImageView,
uri: Uri
)

/**
* 加载GIF图像资源的缩略图。如果只是一个缩略图,你不必加载动画gif
Expand All @@ -50,7 +40,13 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param uri 加载图像的URI
*/
void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri);
fun loadGifThumbnail(
context: Context,
resize: Int,
placeholder: Drawable,
imageView: ImageView,
uri: Uri
)

/**
* 加载静态图像资源
Expand All @@ -62,7 +58,7 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param uri 加载图像的URI
*/
void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri);
fun loadImage(context: Context, resizeX: Int, resizeY: Int, imageView: ImageView, uri: Uri)

/**
* 加载静态图像资源
Expand All @@ -72,7 +68,7 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param url 加载图像的url
*/
void loadUrlImage(Context context, ImageView imageView, String url);
fun loadUrlImage(context: Context, imageView: ImageView, url: String)

/**
* 加载静态图像资源
Expand All @@ -82,7 +78,7 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param uri 加载图像的uri
*/
void loadUriImage(Context context, ImageView imageView, Uri uri);
fun loadUriImage(context: Context, imageView: ImageView, uri: Uri)

/**
* 加载静态图像资源
Expand All @@ -92,7 +88,7 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param resourceId 资源id图片
*/
void loadDrawableImage(Context context, ImageView imageView ,Integer resourceId);
fun loadDrawableImage(context: Context, imageView: ImageView, resourceId: Int)

/**
* 加载GIF图像资源。
Expand All @@ -103,13 +99,19 @@ public interface ImageEngine {
* @param imageView ImageView控件
* @param uri 加载图像的uri
*/
void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri);
fun loadGifImage(
context: Context,
resizeX: Int,
resizeY: Int,
imageView: ImageView,
uri: Uri
)

/**
* 此实现是否支持动态GIF
* 只需了解它,方便用户使用
*
* @return true支持动画gif,false不支持动画gif。
*/
boolean supportAnimatedGif();
}
fun supportAnimatedGif(): Boolean
}
Loading

0 comments on commit a56f122

Please sign in to comment.