当我的应用从文件中读取数据时如何使用进度条?就在申请开始之前

时间:2013-05-21 12:22:25

标签: android performance usability

我正在尝试编写一个应用程序,其中从文本文件中提取了许多行数据。这是在我启动应用程序时完成的。当我在应用程序屏幕中单击应用程序时,我想向用户显示进度条。现在有短暂的黑屏,然后显示应用程序数据并显示数据。我想添加一个从0到100的进度条,或只是告诉用户我的应用程序正在加载。

任何人都可以帮我添加进度条代码,因为我是新手。我将非常感谢所有给予我的帮助。 提前谢谢。

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));
}
static class ViewHolder {
      TextView votesText;
      TextView movieText;
      ImageView icon;
    }

/** 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)
    {

        ViewHolder holder; 
        Movie currMovie = movies.get(pos);


        if (convertView == null)
        {

            convertView = mInflater.inflate(R.layout.listrow, parent, false);
            holder = new ViewHolder(); 
            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 convertView;
    }
}

/** 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);
    }
}
}

2 个答案:

答案 0 :(得分:0)

http://android-er.blogspot.be/2010/11/progressbar-running-in-asynctask.html

onPostExecute ==&gt; 阅读完成,下一步做什么

onPreExecute ==&gt; 要开始阅读先做的事情

doInBackground ==&gt; 阅读数据

onProgressUpdate ==&gt; 更新进度条

有了这个你应该能够创建一个进度条

答案 1 :(得分:0)

向您的用户界面添加进度条。然后移动它:

  movies = Movie.loadFromFile(inputStream);       
  setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));

使用更新进度条的doInBackground()方法进入AsyncTask的onProgressUpdate()。在onPostExecute中,将进度条替换为实际内容。

要实际报告您可能需要向Movie.loadFromFile()方法添加侦听器的进度,以便在需要更新进度条时获得回调。