0

キャッシュの概念を理解しようとしています。上下listviewにスクロールすると遅れます。これに関するAndroidチュートリアルを理解しようとしました。しかし、それはあまり役に立ちませんでした。

私を助けてくれた人々から言われたように、ビューをviewHolderに変更しました。

しかし、今、私は新しいエラーに遭遇しています。

戻り型は互換性がありませんArrayAdapter<Movie>.getView(int, View, ViewGroup)

public class MovieRatingsActivity extends ListActivity
{
    private ArrayList<Movie> movies = new ArrayList<Movie>();
    private LayoutInflater mInflater;
    private LruCache<String, Bitmap> cache;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeUI();
    }

    private void initializeUI()
    {
        mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
        InputStream inputStream = getResources().openRawResource(   R.raw.ratings);
        movies = Movie.loadFromFile(inputStream);       
        setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
    }

    /** Custom row adatper -- that displays an icon next to the movie name */
    class RowIconAdapter extends ArrayAdapter<Movie> 
    {
        private ArrayList<Movie> movies;        
        public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, 
                ArrayList<Movie> items)
        {
            super(c, rowResourceId, textViewResourceId, items);
            movies  = items;
        }
        /*
        public View getView(int pos, View convertView, ViewGroup parent)
        {
            View row = mInflater.inflate(R.layout.listrow, parent, false);
            Movie currMovie = movies.get(pos);


            if (currMovie != null)
            {
                ImageView icon = (ImageView) row.findViewById(R.id.row_icon);
                TextView movieText = (TextView) row.findViewById(R.id.row_label);
                TextView votesText = (TextView) row.findViewById(R.id.row_subtext);
                movieText.setText(currMovie.getName());
                String votesStr = currMovie.getVotes()+" votes";
                votesText.setText(votesStr);
                Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
                icon.setImageBitmap(movieIcon);
                Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName());
            }


            return row;         
        }
    }
    */
    /*NEW CODE WHERE ERROR IS THROWN*/

    public ViewHolder getView(int pos, View convertView, ViewGroup parent)
    {
        ViewHolder holder = new ViewHolder();

        Movie currMovie = movies.get(pos);

        if (currMovie != null)
        {
            holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);
            holder.movieText = (TextView) convertView.findViewById(R.id.row_label);
            holder.votesText = (TextView) convertView.findViewById(R.id.row_subtext);
            holder.movieText.setText(currMovie.getName());
            String votesStr = currMovie.getVotes()+" votes";
            holder.votesText.setText(votesStr);
            Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
            holder.icon.setImageBitmap(movieIcon);
            Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName());
        }
        return holder;
    }
}

/** Creates a unique movie icon based on name and rating */
private Bitmap getMovieIcon(String movieName, String movieRating)
{
    int bgColor = getColor(movieName);
    Bitmap b = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
    b.eraseColor(bgColor); // fill bitmap with the color
    Canvas c = new Canvas(b);
    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setColor(getTextColor(bgColor));
    p.setTextSize(24.0f);
    c.drawText(movieRating, 8, 32, p);
    return b;
}

/** Construct a color from a movie name */
private int getColor(String name)
{
    String hex = toHexString(name);
    String red = "#"+hex.substring(0,2);
    String green = "#"+hex.substring(2,4);
    String blue = "#"+hex.substring(4,6);
    String alpha = "#"+hex.substring(6,8);
    int color = Color.argb(Integer.decode(alpha), Integer.decode(red), 
                            Integer.decode(green), Integer.decode(blue));
    return color;
}

/** Given a movie name -- generate a hex value from its hashcode */
private String toHexString(String name)
{
    int hc = name.hashCode();
    String hex = Integer.toHexString(hc);
    if (hex.length() < 8)
    {
        hex = hex+hex+hex;
        hex = hex.substring(0,8); // use default color value
    }
    return hex;
}

/** Crude optimization to obtain a contrasting color -- does not work well yet */
private int getTextColor(int bg)
{

    int r = Color.red(bg);
    int g = Color.green(bg);
    int b = Color.blue(bg);
    String hex = Integer.toHexString(r)+Integer.toHexString(g);
    hex += Integer.toHexString(b);

    int cDec = Integer.decode("#"+hex);
    if (cDec > 0xFFFFFF/2)  // go dark for lighter shades
        return Color.rgb(0, 0, 0);
    else
    {
        r = (r+128)%256;
        g = (g+128)%256;
        b = (b+128)%256;
        return Color.rgb(r,g,b);
    }
}
4

2 に答える 2

2

パフォーマンスを向上させることができるいくつかのことがあります。

  1. 呼び出しを防ぐためにViewHolder パターンの使用を検討してくださいfindViewById()
  2. ViewHolder パターンを実装することで、レイアウトの不必要な膨張も削除します
  3. ペイント オブジェクトの作成にはコストがかかります。1 つの onCreate をインスタンス化し、変更されるプロパティを更新して再利用します。
于 2013-05-20T05:03:43.633 に答える
1

コードに従って、getview が呼び出されるたびに行を inftaling しており、ビューの null もチェックしています

View row=null;
if(convertView==null)
{
    row = mInflater.inflate(R.layout.listrow, parent, false);
}

スムーズなスクロールのためにホルダーパターンも使用する必要があります

リンク

于 2013-05-20T05:04:15.863 に答える