自定义进度对话框,Squre图像旋转随着AsynTask [英] Custom Progress Dialog With Squre Image Rotation With AsynTask
问题描述
我创建了一个自定义加载进度对话框。而其运作良好。
I have created a custom Loading Progress Dialog. And its working well.
我旋转12平方米影像这里是其中的一个
I am rotating 12 square Images here is one of them
但是,当我想用它AsynTask,动画不能正常工作。
But when I want to use it with AsynTask, The animation not working.
我的样品code是如下。的
活动从何讲起载入中...动画和停止。
Activity Where I Start Loading... Animation and Stop.
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布局。
XML layout with Simple Text and Image for Progress Dialog.
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幅,我用旋转角度和放大器;持续时间
Animation List having 12 Images I am Rotating with Angle & Time Duration
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>
下面是结果载入画面。
我的问题是,有什么方法可以达到相同的加载动画 与AsynTask。
您小的帮助将AP preciated。
Your Small Help will appreciated.
推荐答案
好感谢@Brontok和@URAndroid的帮助。我得到了解决我的问题。 因此,让我回答我的问题,锄我实现了自定义加载动画
Well thanks to @Brontok and @URAndroid for their help. I got solved my problem. So let me answer my own question, Hoe i achieved that Custom Loading animation
我添加一些图片进行图片旋转动画
I have added few Image for Image Rotation Animation
第1步 - 在RES /绘制文件夹
Step 1 - in res/drawable folder
- progress_sm_w00.png(默认为空透明图像)
- progress_sm_w01.png(第一animtion位置)
- progress_sm_w02.png
- progress_sm_w03.png
- progress_sm_w04.png
- progress_sm_w05.png
- progress_sm_w06.png
- progress_sm_w07.png
- progress_sm_w08.png
- progress_sm_w09.png
- progress_sm_w10.png
- progress_sm_w11.png
- progress_sm_w12.png(最后一个动画位置)。
- progress_sm_w00.png (Default blank transparent image)
- progress_sm_w01.png (first animtion position)
- progress_sm_w02.png
- progress_sm_w03.png
- progress_sm_w04.png
- progress_sm_w05.png
- progress_sm_w06.png
- progress_sm_w07.png
- progress_sm_w08.png
- progress_sm_w09.png
- progress_sm_w10.png
- progress_sm_w11.png
- progress_sm_w12.png (last animation position).
例:其中之一就是下面我补充
Example : one of these is below i have added
第2步 - 在RES /阿尼姆创建的文件夹动画文件名loading_animation.xml
Step 2 - in res/anim folder created animation file name "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步 - 现在创建的自定义加载视图在我的屏幕(活动)只要我需要显示加载布局
Step 3 - now created Custom Loading View layout in my Screen (Activity) wherever I need to show loading
例子。 我的Facebook的登录屏幕XML布局
example. XML layout for my Facebook login screen
<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中code(活动)我创建加载视图和完成的OnCreate方法后,我开始ASYN任务,所以我的动画获得正常工作
Step 4 - In java code (Activity) I created Loading view and after completing OnCreate method I started Asyn Task so my Animation get work properly.
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步 - 下面是结果屏幕与进度加载动画
Step 5 - Here is the Result Screen with Progress Loading animation.
希望这会帮助你。干杯!!
Hope this will help you. Cheers!!
这篇关于自定义进度对话框,Squre图像旋转随着AsynTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!