实现主窗口背景颜色修改功能。
This commit is contained in:
@@ -26,11 +26,12 @@ import androidx.core.app.ActivityCompat;
|
|||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
||||||
import cc.winboll.studio.gallery.AlbumAdapter.OnAlbumClickListener;
|
import cc.winboll.studio.gallery.AlbumAdapter.OnAlbumClickListener;
|
||||||
import cc.winboll.studio.gallery.utils.BackgroundUtils;
|
import cc.winboll.studio.gallery.utils.BackgroundUtils;
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
|
||||||
import cc.winboll.studio.libappbase.LogActivity;
|
import cc.winboll.studio.libappbase.LogActivity;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import com.a4455jkjh.colorpicker.ColorPickerDialog;
|
||||||
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileFilter;
|
import java.io.FileFilter;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
@@ -51,6 +52,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
LogUtils.d(TAG, "onCreate");
|
LogUtils.d(TAG, "onCreate");
|
||||||
|
|
||||||
|
View content = findViewById(android.R.id.content);
|
||||||
|
if (content != null) {
|
||||||
|
content.setBackground(BackgroundUtils.getInstance().getDrawable());
|
||||||
|
}
|
||||||
|
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
@@ -64,50 +70,41 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
fabScrollTop = findViewById(R.id.fab_scroll_top);
|
fabScrollTop = findViewById(R.id.fab_scroll_top);
|
||||||
fabScrollTop.setOnClickListener(new View.OnClickListener() {
|
fabScrollTop.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
recyclerView.scrollToPosition(0);
|
recyclerView.scrollToPosition(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||||
super.onScrolled(recyclerView, dx, dy);
|
super.onScrolled(recyclerView, dx, dy);
|
||||||
GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
|
GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
|
||||||
if (layoutManager != null) {
|
if (layoutManager != null) {
|
||||||
int firstVisible = layoutManager.findFirstVisibleItemPosition();
|
int firstVisible = layoutManager.findFirstVisibleItemPosition();
|
||||||
if (firstVisible > 0) {
|
if (firstVisible > 0) {
|
||||||
fabScrollTop.setVisibility(View.VISIBLE);
|
fabScrollTop.setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
fabScrollTop.setVisibility(View.GONE);
|
fabScrollTop.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
adapter.setOnAlbumClickListener(new OnAlbumClickListener() {
|
adapter.setOnAlbumClickListener(new OnAlbumClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAlbumClick(Album album) {
|
public void onAlbumClick(Album album) {
|
||||||
Intent intent = new Intent(MainActivity.this, AlbumActivity.class);
|
Intent intent = new Intent(MainActivity.this, AlbumActivity.class);
|
||||||
intent.putExtra(AlbumActivity.EXTRA_ALBUM_PATH, album.getPath());
|
intent.putExtra(AlbumActivity.EXTRA_ALBUM_PATH, album.getPath());
|
||||||
intent.putExtra(AlbumActivity.EXTRA_ALBUM_NAME, album.getName());
|
intent.putExtra(AlbumActivity.EXTRA_ALBUM_NAME, album.getName());
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
checkAndRequestPermissions();
|
checkAndRequestPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostCreate(Bundle savedInstanceState) {
|
|
||||||
super.onPostCreate(savedInstanceState);
|
|
||||||
View content = findViewById(android.R.id.content);
|
|
||||||
if (content != null) {
|
|
||||||
content.setBackground(BackgroundUtils.getInstance().getDrawable());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkAndRequestPermissions() {
|
private void checkAndRequestPermissions() {
|
||||||
LogUtils.i(TAG, "checkAndRequestPermissions");
|
LogUtils.i(TAG, "checkAndRequestPermissions");
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
@@ -155,13 +152,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void requestPermission() {
|
private void requestPermission() {
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
||||||
PERMISSION_REQUEST_CODE);
|
PERMISSION_REQUEST_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
|
||||||
@NonNull int[] grantResults) {
|
@NonNull int[] grantResults) {
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
if (requestCode == PERMISSION_REQUEST_CODE) {
|
if (requestCode == PERMISSION_REQUEST_CODE) {
|
||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
@@ -172,7 +169,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadAlbums() {
|
private void loadAlbums() {
|
||||||
LogUtils.d(TAG, "loadAlbums");
|
LogUtils.d(TAG, "loadAlbums");
|
||||||
String folderPath = prefs.getFolderPath();
|
String folderPath = prefs.getFolderPath();
|
||||||
File baseFolder = new File(folderPath);
|
File baseFolder = new File(folderPath);
|
||||||
@@ -248,8 +245,8 @@ private void loadAlbums() {
|
|||||||
String selection = MediaStore.Images.Media.DATA + " = ?";
|
String selection = MediaStore.Images.Media.DATA + " = ?";
|
||||||
String[] selectionArgs = { path };
|
String[] selectionArgs = { path };
|
||||||
try (Cursor cursor = getContentResolver().query(
|
try (Cursor cursor = getContentResolver().query(
|
||||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||||
projection, selection, selectionArgs, null)) {
|
projection, selection, selectionArgs, null)) {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
|
||||||
@@ -306,7 +303,31 @@ private void loadAlbums() {
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return true;
|
return true;
|
||||||
} else if (id == R.id.action_change_bg_color) {
|
} else if (id == R.id.action_change_bg_color) {
|
||||||
Toast.makeText(this, "修改背景颜色", Toast.LENGTH_SHORT).show();
|
//Toast.makeText(this, "修改背景颜色", Toast.LENGTH_SHORT).show();
|
||||||
|
if (BackgroundUtils.DrawableType.COLOR == BackgroundUtils.getInstance().getDrawableType()) {
|
||||||
|
|
||||||
|
ColorPickerDialog dlg = new ColorPickerDialog(this, BackgroundUtils.getInstance().getColor());
|
||||||
|
dlg.setOnColorChangedListener(new com.a4455jkjh.colorpicker.view.OnColorChangedListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeColorChanged() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onColorChanged(int color) {
|
||||||
|
BackgroundUtils.getInstance().initFromColor(MainActivity.this, color);
|
||||||
|
View content = findViewById(android.R.id.content);
|
||||||
|
if (content != null) {
|
||||||
|
content.setBackground(BackgroundUtils.getInstance().getDrawable());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterColorChanged() {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dlg.show();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} else if (id == R.id.action_settings) {
|
} else if (id == R.id.action_settings) {
|
||||||
startActivity(new Intent(this, SettingsActivity.class));
|
startActivity(new Intent(this, SettingsActivity.class));
|
||||||
@@ -359,23 +380,23 @@ private void loadAlbums() {
|
|||||||
File baseFolder = new File(folderPath);
|
File baseFolder = new File(folderPath);
|
||||||
if (baseFolder.exists() && baseFolder.isDirectory()) {
|
if (baseFolder.exists() && baseFolder.isDirectory()) {
|
||||||
File[] subfolders = baseFolder.listFiles(new FileFilter() {
|
File[] subfolders = baseFolder.listFiles(new FileFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File file) {
|
public boolean accept(File file) {
|
||||||
return file.isDirectory();
|
return file.isDirectory();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (subfolders != null) {
|
if (subfolders != null) {
|
||||||
ArrayList<String> paths = new ArrayList<>();
|
ArrayList<String> paths = new ArrayList<>();
|
||||||
for (File subfolder : subfolders) {
|
for (File subfolder : subfolders) {
|
||||||
File[] images = subfolder.listFiles(new FilenameFilter() {
|
File[] images = subfolder.listFiles(new FilenameFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File dir, String name) {
|
public boolean accept(File dir, String name) {
|
||||||
String lower = name.toLowerCase();
|
String lower = name.toLowerCase();
|
||||||
return lower.endsWith(".jpg") || lower.endsWith(".jpeg")
|
return lower.endsWith(".jpg") || lower.endsWith(".jpeg")
|
||||||
|| lower.endsWith(".png") || lower.endsWith(".gif")
|
|| lower.endsWith(".png") || lower.endsWith(".gif")
|
||||||
|| lower.endsWith(".webp") || lower.endsWith(".bmp");
|
|| lower.endsWith(".webp") || lower.endsWith(".bmp");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (images != null) {
|
if (images != null) {
|
||||||
for (File img : images) {
|
for (File img : images) {
|
||||||
paths.add(img.getAbsolutePath());
|
paths.add(img.getAbsolutePath());
|
||||||
@@ -386,11 +407,11 @@ private void loadAlbums() {
|
|||||||
LogUtils.d(TAG, "scanning " + paths.size() + " files to MediaStore");
|
LogUtils.d(TAG, "scanning " + paths.size() + " files to MediaStore");
|
||||||
String[] pathArray = paths.toArray(new String[0]);
|
String[] pathArray = paths.toArray(new String[0]);
|
||||||
MediaScannerConnection.scanFile(this, pathArray, null, new MediaScannerConnection.OnScanCompletedListener() {
|
MediaScannerConnection.scanFile(this, pathArray, null, new MediaScannerConnection.OnScanCompletedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onScanCompleted(String path, Uri uri) {
|
public void onScanCompleted(String path, Uri uri) {
|
||||||
LogUtils.d(TAG, "scanCompleted: " + path + " -> " + uri);
|
LogUtils.d(TAG, "scanCompleted: " + path + " -> " + uri);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user