内存使用率突然飙升

时间:2017-08-10 11:44:25

标签: java android xml

每当我进入我的一个活动(我将其命名为main 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,歌曲艺术家姓名,drawable图像名称,songURL,songDuration和一个布尔值的类,用于说明该歌曲是否要在线流式传输。)

我的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在那里。

1 个答案:

答案 0 :(得分:1)

不要在UI线程上设置图像。使用像GlidePicasso这样的图像加载库在后台线程上加载图像,以便不阻止主线程