安卓app_sl4_9显示在标题上的进度条-功能:创建异步任务

安卓app_sl4_9显示在标题上的进度条

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout1"
    android:orientation="vertical"
    
    tools:context="com.example.sl4_9.MainActivity" >

   

</LinearLayout>
package com.example.sl4_9;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
     private int imageId[] = new int[]{R.drawable.img01,
             R.drawable.img02,
             R.drawable.img03,
             R.drawable.img04};
     private LinearLayout layout1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout1=(LinearLayout) findViewById(R.id.linearlayout1);
        new MyTack().execute();
    }
    //功能:创建异步任务,添加4张图片
    class MyTack extends AsyncTask<Void,Integer,LinearLayout>
    {
       @Override
       protected void onPreExecute()
       {
           setProgressBarVisibility(true);
           super.onPreExecute();
       }
        @Override
        protected LinearLayout doInBackground(Void... params) 
        {
            // TODO Auto-generated method stub
            LinearLayout layout2=new LinearLayout(MainActivity.this);
            for(int i=1;i<5;i++)
            {
                ImageView iv=new ImageView(MainActivity.this);
                iv.setLayoutParams(new LayoutParams(245,108) );
                iv.setImageResource(imageId[i-1]);
                layout2.addView(iv);
                try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                publishProgress(i);
                
            }
            return layout2;
        }
        /*
         * 功能:更新进度
         */
        @Override
        protected void onProgressUpdate(Integer...values)
        {
            setProgress(values[0] * 2500);
            super.onProgressUpdate(values);
        }
        /*
         * 功能:任务执行后
         */
        @Override
        protected void onPostExecute(LinearLayout result)
        {
            setProgressBarVisibility(true);
            layout1.addView(result);
            super.onPostExecute(result);
            
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2022-03-09 08:06  txwtech  阅读(25)  评论(0编辑  收藏  举报