Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
asnhkl1 committed Mar 25, 2021
1 parent 94b8117 commit cc9bf41
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions app/src/main/java/demo/lee/com/imagecompress/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -28,16 +30,20 @@
import demo.lee.com.compressimagelibrary.Photo;

public class MainActivity extends AppCompatActivity {
ImageView image1,image2;
ImageView image1, image2;
TextView text1, text2;
ArrayList<String> list = new ArrayList<>();

private CompressConfig compressConfig; // 压缩配置

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1=findViewById(R.id.image1);
image2=findViewById(R.id.image2);
image1 = findViewById(R.id.image1);
image2 = findViewById(R.id.image2);
text1 = findViewById(R.id.text1);
text2 = findViewById(R.id.text2);
XXPermissions.with(MainActivity.this)
// 可设置被拒绝后继续申请,直到用户授权或者永久拒绝
//.constantRequest()
Expand Down Expand Up @@ -78,9 +84,13 @@ public void openAlarm(View view) {
public void compress(View view) {
ArrayList<Photo> photos = new ArrayList<>();
// String path =Environment.getExternalStorageDirectory()+"/bc4.jpg";
String path =list.get(0);
if (list.size() == 0) {
Toast.makeText(this, "请选择需要压缩的照片", Toast.LENGTH_SHORT).show();
return;
}
String path = list.get(0);
File file = new File(path);
if(file.exists()){
if (file.exists()) {
Photo photo = new Photo(file.getPath());
photo.setOriginalPath(path);
photos.add(photo);
Expand All @@ -95,16 +105,25 @@ public void compress(View view) {
.setCacheDir(path) // 压缩后缓存图片路径,默认值:Constants.COMPRESS_CACHE
.setShowCompressDialog(true) // 是否显示压缩进度条,默认值:false
.create();
CompressImageManager.build(this,compressConfig, photos, new CompressImage.CompressListener() {
CompressImageManager.build(this, compressConfig, photos, new CompressImage.CompressListener() {
@Override
public void onCompressSuccess(ArrayList<Photo> var1) {
Log.i("imageCompress","success");
String path = var1.get(0).getCompressPath();
if (path == null) {
path = var1.get(0).getOriginalPath();
}
Log.i("imageCompress", "success" + path);

Glide.with(MainActivity.this).load(path).into(image2);

File file = new File(path);

text2.setText(path + ",大小为:" + file.length() / 1000 + "K");
}

@Override
public void onCompressFailed(ArrayList<Photo> var1, String var2) {
Log.e("imageCompress","false",null);
Log.e("imageCompress", "false", null);
}
}).compress();
}
Expand All @@ -118,6 +137,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
for (int i = 0; i < selectList.size(); i++) {
list.add(selectList.get(i).getPath());
}
File file = new File(list.get(0));

text1.setText(list.get(0) + ",大小为:" + file.length() / 1000 + "K");

Glide.with(MainActivity.this).load(list.get(0)).into(image1);
}
}
Expand Down
64 changes: 39 additions & 25 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="300dp"/>
<ImageView
android:id="@+id/image2"
android:layout_below="@+id/image1"
android:layout_width="match_parent"
android:layout_height="300dp"/>

<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="open!"
android:onClick="openAlarm"/>
<Button
android:layout_toRightOf="@+id/open"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="compress"
android:onClick="compress"/>
android:orientation="vertical">

<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="open!"
android:onClick="openAlarm"/>
<Button
android:layout_toRightOf="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="compress"
android:onClick="compress"/>
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="300dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="300dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text2"/>

</RelativeLayout>
</LinearLayout>
</ScrollView>

0 comments on commit cc9bf41

Please sign in to comment.