gpt4 book ai didi

java - 内存使用量突然激增

转载 作者:行者123 更新时间:2023-12-02 12:23:32 24 4
gpt4 key购买 nike

每当我进入其中一项 Activity (我将其命名为主要 Activity )时,内存和 CPU 使用率都会突然激增,从而导致延迟,如下所示。 enter image description here
我还在我的 MainActivity 中使用了 RecyclerView,它使用 row_stream_songs.xml
我设法发现它是由row_stream_songs.xml上的ImageView引起的(代码在下面),但是我确实需要那里的ImageView。

我的 MainActivity.java 的相关代码:

//Fetch stream song data
SongData songData = new SongData();
ArrayList<Song> streamSong = songData.getSongList(0);

//Setup the recycler view to place
//song data.
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.streamRecycler);
StreamSongAdapter songAdapter = new StreamSongAdapter(this, streamSong);
recyclerView.setAdapter(songAdapter);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);

//When the user selects a song.
songAdapter.setOnItemClickListener(new StreamSongAdapter.OnItemClickListener(){
@Override
public void onItemClick(LinearLayout b, View v, Song obj, int position){
Intent intent = new Intent(MainActivity.this, PlayMusic.class);
intent.putExtra("songData",obj.getSongData());
intent.putExtra("songType", 1);
startActivity(intent);
}
});

我的 StreamSongAdapter 的代码

public class StreamSongAdapter extends RecyclerView.Adapter<StreamSongAdapter.SongHolder> {
private ArrayList<Song> song;
private Context context;

private OnItemClickListener onItemClickListener;

public StreamSongAdapter(Context _context, ArrayList<Song> _song){
context = _context;
song = _song;
}

public interface OnItemClickListener{
void onItemClick(LinearLayout b, View v, Song obj, int position);
}

public void setOnItemClickListener(final OnItemClickListener _onItemClickListener){
onItemClickListener = _onItemClickListener;
}

@Override
public StreamSongAdapter.SongHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View myView = LayoutInflater.from(context).inflate(R.layout.row_stream_songs, parent, false);
return new SongHolder(myView);
}

//Adds the row layout to the Recycler view.
@Override
public void onBindViewHolder(final StreamSongAdapter.SongHolder holder, final int position) {

holder.songName.setText(song.get(position).getSongName());
int resId = AppUtil.getImageIdFromDrawable(context, song.get(position).getSongImage());
holder.songImage.setImageResource(resId);
holder.artistName.setText(song.get(position).getSongArtist());

//Handle song selection
holder.linearLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (onItemClickListener != null){
onItemClickListener.onItemClick(holder.linearLayout, v, song.get(position), position);
}
}
});
}
@Override
public int getItemCount() {
return song.size();
}

//Gets data for each view in the recyclerview.
class SongHolder extends RecyclerView.ViewHolder{
TextView songName , artistName;
ImageView songImage;
LinearLayout linearLayout;

SongHolder(View itemView) {
super(itemView);
artistName = (TextView) itemView.findViewById(R.id.streamSongArtist);
songName = (TextView) itemView.findViewById(R.id.streamSongTitle);
songImage = (ImageView) itemView.findViewById(R.id.streamSongImage);
linearLayout = (LinearLayout) itemView.findViewById(R.id.rowMusicSelect);
}
}
}

(Song 是一个类,它存储 SongId、songTitle、歌曲艺术家姓名、可绘制图像名称、songURL、songDuration 和一个 boolean 值,该 boolean 值表明歌曲是否要在线流式传输。 )

我的 content_main.xml 代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="sg.edu.tp.melodia.Activities.MainActivity"
tools:showIn="@layout/app_bar_main">

<android.support.v7.widget.RecyclerView
android:id="@+id/streamRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
android:layout_marginTop="60dp"
tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>

row_stream_songs.xml 的代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
android:layout_width="match_parent"
android:id="@+id/rowMusicSelect"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="@drawable/clickable1"
android:orientation="horizontal">

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/streamSongTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView" />

<TextView
android:id="@+id/streamSongArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>

<ImageView
android:id="@+id/streamSongImage"
android:layout_width="45dp"
android:layout_height="45dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>

</android.support.v7.widget.CardView>

如上所述,我发现每当我删除 row_stream_songs.xml 上的 ImageView 时,内存和 CPU 使用率就会正常下降。但我需要 ImageView 在那里。

最佳答案

不要在 UI 线程上设置图像。使用像 Glide 这样的图像加载库或Picasso在后台线程上加载图像,这样主线程就不会被阻塞

关于java - 内存使用量突然激增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45612831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com