使用AsyncTask进行方形图像旋转的自定义进度对话框

时间:2013-11-11 07:11:53

标签: android animation android-asynctask progressdialog

我创建了一个自定义加载进度对话框。而且运作良好。

我正在旋转12平方图像这里是其中之一

enter image description here

但是当我想在AsynTask中使用它时,动画无效。

我的示例代码如下。

我开始加载的活动...动画和停止。

MainActivity.java

public class MainActivity extends Activity {
    AnimationDrawable loadingViewAnim;
    TextView loadigText;
    ImageView loadigIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loadigText = (TextView) findViewById(R.id.textView1);
        loadigText.setText("Loading...");
        loadigText.setVisibility(View.GONE);

        loadigIcon = (ImageView) findViewById(R.id.imageView1);
        loadigIcon.setVisibility(View.GONE);

        loadigIcon.setBackgroundResource(R.anim.progress_animation_white);
        loadingViewAnim = (AnimationDrawable) loadigIcon.getBackground();


    }

    //When User Touch on Screen The Loading... Animation Starts With Image Rotation

    //If I start below code in AsynTask's onPreExecute method it doesn't work
    public boolean onTouchEvent(MotionEvent event)
      {
        loadigText.setVisibility(View.VISIBLE);
        loadigIcon.setVisibility(View.VISIBLE);

          if (event.getAction() == MotionEvent.ACTION_DOWN)
          {
             loadingViewAnim.start();
             return true;
          }
         return super.onTouchEvent(event);
      }
}

使用简单文本和图像进行对话的XML布局。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/progress_sm_w01" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Loading..."
        android:textColor="#ffffff"
        android:textSize="12sp" />

</LinearLayout>

具有12个图像的动画列表我正在使用角度旋转&amp;时间持续时间

progress_animation_white.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/progress_sm_w01" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w02" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w03" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w04" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w05" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w06" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w07" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w08" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w09" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w10" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w11" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w12" android:duration="50" />

    </animation-list>

结果载入画面..

enter image description here

  

我的问题是无论如何都要实现相同的加载动画   使用AsynTask。

您的小帮助将受到赞赏。

2 个答案:

答案 0 :(得分:32)

非常感谢@Brontok和@URAndroid的帮助。我解决了我的问题。 所以让我回答我自己的问题,锄头我实现了自定义加载动画

我添加了一些用于图像旋转动画的图像

res / drawable文件夹中的

第1步 -

  1. progress_sm_w00.png(默认空白透明图片)
  2. progress_sm_w01.png(第一个动画位置)
  3. progress_sm_w02.png
  4. progress_sm_w03.png
  5. progress_sm_w04.png
  6. progress_sm_w05.png
  7. progress_sm_w06.png
  8. progress_sm_w07.png
  9. progress_sm_w08.png
  10. progress_sm_w09.png
  11. progress_sm_w10.png
  12. progress_sm_w11.png
  13. progress_sm_w12.png(上一个动画位置)。
  14. 示例:其中一个在下面我添加了

    enter image description here

    res / anim文件夹中的

    步骤2 - 创建了动画文件名“loading_animation.xml”

    <item android:drawable="@drawable/progress_sm_w01" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w02" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w03" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w04" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w05" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w06" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w07" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w08" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w09" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w10" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w11" android:duration="50" />
    <item android:drawable="@drawable/progress_sm_w12" android:duration="50" />
    
    </animation-list>
    

    第3步 - 现在我的屏幕(活动)中创建了自定义加载视图布局,无论我需要显示加载

    例子。 我的Facebook登录屏幕的XML布局

         
    <LinearLayout
             android:id="@+id/LinearLayout1"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#0D000000"
             android:gravity="center"
             android:orientation="vertical"
             android:visibility="gone" >
    
        <ImageView
        android:id="@+id/imageView111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/progress_sm_w00"
        android:visibility="gone" />
    
        <TextView
        android:id="@+id/textView111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Searching..."
        android:textColor="#ffffff"
        android:textSize="12sp"
        android:visibility="gone" />
    
    </LinearLayout>
    

    第4步 - 在java代码(Activity)中我创建了Loading视图,在完成OnCreate方法后,我启动了Asyn Task,这样我的动画就可以正常工作了。

    public class ResultActivity extends Activity {
    
          private static final String TAG = "ResultActivity";
          private AnimationDrawable loadingViewAnim=null;
          private TextView loadigText = null;
          private ImageView loadigIcon = null;
          private LinearLayout loadingLayout = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
    
            loadingLayout = (LinearLayout)findViewById(R.id.LinearLayout1);
            loadingLayout.setVisibility(View.GONE);
    
            loadigText = (TextView) findViewById(R.id.textView111);
            loadigText.setVisibility(View.GONE);
    
            loadigIcon = (ImageView) findViewById(R.id.imageView111);
            loadigIcon.setVisibility(View.GONE);
    
            loadigIcon.setBackgroundResource(R.anim.loading_animation);
            loadingViewAnim = (AnimationDrawable) loadigIcon.getBackground();
    
            // This line is to start Asyn Task only when OnCreate Method get completed, So Loading Icon Rotation Animation work properly
            loadigIcon.post(new Starter());
    
        }
    
        class Starter implements Runnable {
              public void run() {
                //start Asyn Task here   
                new LongOperation().execute("");
              }
          }
    
        private class LongOperation extends AsyncTask<String, Void, String> {
    
            @Override
            protected String doInBackground(String... params) {
                //ToDo your Network Job/Request etc. here 
                return "Executed";
            }
    
            @Override
            protected void onPostExecute(String result) {
            //ToDo with result you got from Task
    
            //Stop Loading Animation
                loadingLayout.setVisibility(View.GONE);
            loadigText.setVisibility(View.GONE);
            loadigIcon.setVisibility(View.GONE);
            loadingViewAnim.stop();
            }
    
            @Override
            protected void onPreExecute() {
            //Start  Loading Animation
            loadingLayout.setVisibility(View.VISIBLE);
            loadigText.setVisibility(View.VISIBLE);
            loadigIcon.setVisibility(View.VISIBLE);
            loadingViewAnim.start();
            }
    
            @Override
            protected void onProgressUpdate(Void... values) {}
        }
    }
    

    第5步 - 以下是带有进度加载动画的结果屏幕。

    enter image description here

    希望这会对你有所帮助。干杯!!

答案 1 :(得分:1)

也许您可以使用GLIDE,例如

Glide.with(context)
    .load(imageUrl)
    .asGif()
    .placeholder(R.drawable.loading2)
    .crossFade()
    .into(imageView);

Show GIF file with Glide (image loading and caching library)