fix:切换图片时会白屏的问题

This commit is contained in:
Yutousama 2022-06-12 00:06:38 +08:00
parent b04bd7ca5a
commit 3aef3c1504

View File

@ -69,48 +69,16 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
handler = new Handler(Looper.myLooper()); handler = new Handler(Looper.myLooper());
initView(); initView();
play.setOnClickListener(new View.OnClickListener() { play.setOnClickListener(v -> handler.post(() -> {
@Override
public void onClick(View v) {
handler.post(new Runnable() {
@Override
public void run() {
if (browserHelper.isPlayer()) {
browserHelper.getTransportControls().pause();
} else {
browserHelper.getTransportControls().play();
}
}
});
if (browserHelper.isPlayer()) {
browserHelper.getTransportControls().pause();
} else {
browserHelper.getTransportControls().play();
} }
}); }));
next.setOnClickListener(new View.OnClickListener() { next.setOnClickListener(v -> handler.post(() -> browserHelper.getTransportControls().skipToNext()));
@Override previous.setOnClickListener(v -> handler.post(() -> browserHelper.getTransportControls().skipToPrevious()));
public void onClick(View v) {
handler.post(new Runnable() {
@Override
public void run() {
browserHelper.getTransportControls().skipToNext();
}
});
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.post(new Runnable() {
@Override
public void run() {
browserHelper.getTransportControls().skipToPrevious();
}
});
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@ -124,27 +92,21 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
handler.post(new Runnable() { handler.post(() -> {
@Override if (browserHelper.getTransportControls() != null) {
public void run() { browserHelper.getTransportControls().seekTo(MainActivity.this.seekBar.getProgress());
if (browserHelper.getTransportControls() != null) {
browserHelper.getTransportControls().seekTo(MainActivity.this.seekBar.getProgress());
}
} }
}); });
} }
}); });
playModel.setOnClickListener(new View.OnClickListener() { playModel.setOnClickListener(v -> {
@Override int model = (int) playModel.getTag();
public void onClick(View v) { if (model == MusicContainer.PLAY_MODEL_LIST_RANDOM) {
int model = (int) playModel.getTag(); setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, true);
if (model == MusicContainer.PLAY_MODEL_LIST_RANDOM) { return;
setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, true);
return;
}
setPlayModelButton(model + 1, true);
} }
setPlayModelButton(model + 1, true);
}); });
browserHelper = new MediaBrowserHelper(this); browserHelper = new MediaBrowserHelper(this);
@ -190,26 +152,9 @@ public class MainActivity extends AppCompatActivity {
int seconds = (int) (data.getDurationAsDouble() % 60); int seconds = (int) (data.getDurationAsDouble() % 60);
durationTime.setText(String.format(Locale.CHINA, "%02d", minutes) + ":" durationTime.setText(String.format(Locale.CHINA, "%02d", minutes) + ":"
+ String.format(Locale.CHINA, "%02d", seconds)); + String.format(Locale.CHINA, "%02d", seconds));
int model = ConfigTools.getPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM); int model = ConfigTools.getConfigPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM);
setPlayModelButton(model); setPlayModelButton(model);
if (data.getImg(AppData.imageWidth, AppData.imageHigh) != null) { setImage(data);
setImage(data);
} else {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (data.getImg(AppData.imageWidth, AppData.imageHigh) != null) {
AppData.handler.post(new Runnable() {
@Override
public void run() {
setImage(data);
}
});
cancel();
}
}
}, 0, 100);
}
} }
@ -243,17 +188,17 @@ public class MainActivity extends AppCompatActivity {
break; break;
default: default:
setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, isButton); setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM, isButton);
return;
} }
ConfigTools.getPreferences().edit().putInt("playModel", model).apply();
} }
private void setImage(MusicData data) { private void setImage(MusicData data) {
// setBarColor(data.getImageColor()); // setBarColor(data.getImageColor());
Glide.with(this).load(data.getImg(AppData.imageWidth, AppData.imageHigh)) Glide.with(this).load(data.getImg(AppData.imageWidth, AppData.imageHigh))
.placeholder(album_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(album_image); .into(album_image);
Glide.with(this).load(data.getImg(AppData.minImageWidth, AppData.minImageHigh)) Glide.with(this).load(data.getImg(AppData.minImageWidth, AppData.minImageHigh))
.placeholder(background_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
.into(background_image); .into(background_image);
} }
@ -274,7 +219,7 @@ public class MainActivity extends AppCompatActivity {
playModel = findViewById(R.id.playModel); playModel = findViewById(R.id.playModel);
playIndex = findViewById(R.id.playIndex); playIndex = findViewById(R.id.playIndex);
touch = new GestureDetectorCompat(this, new MyTouch()); touch = new GestureDetectorCompat(this, new MyTouch());
int model = ConfigTools.getPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM); int model = ConfigTools.getConfigPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM);
setPlayModelButton(model); setPlayModelButton(model);
} }
@ -358,11 +303,13 @@ public class MainActivity extends AppCompatActivity {
} else { } else {
dialog.setTitle("播放列表:" + playList.size()); dialog.setTitle("播放列表:" + playList.size());
MusicLibsAdapter adapter = new MusicLibsAdapter(this); MusicLibsAdapter adapter = new MusicLibsAdapter(this);
// ItemTouchHelper touchHelper=new ItemTouchHelper(new MusicItemTouchAdapterCallback());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
recyclerView = new RecyclerView(this); recyclerView = new RecyclerView(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setLayoutParams(params); recyclerView.setLayoutParams(params);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
// touchHelper.attachToRecyclerView(recyclerView);
adapter.setData(playList); adapter.setData(playList);
adapter.setBrowserHelper(browserHelper); adapter.setBrowserHelper(browserHelper);
dialog.setContentView(recyclerView); dialog.setContentView(recyclerView);