Android移动应用开发之使用异步调用进度条及实现幻灯片切换效果
Posted Icy Hunter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android移动应用开发之使用异步调用进度条及实现幻灯片切换效果相关的知识,希望对你有一定的参考价值。
文章目录
异步调用进度条
核心代码如下:
package scq.scq.asyntaskdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDialog = findViewById(R.id.btn);
btnDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showDialog(1);
new StringTask().execute();
);
// 创建AsynkTask子类
private class StringTask extends AsyncTask<Void, Integer, Void>
public void printInfo(String info)
Log.i("asyncTask", info+":Thread is" + Thread.currentThread().getName());
@Override
protected Void doInBackground(Void... voids)
pd.incrementProgressBy(-pd.getProgress());
for(int i=0; i<=100; i++)
publishProgress(i);
printInfo("doInBackground" + i);
SystemClock.sleep(50);
return null;
@Override
protected void onPostExecute(Void unused)
printInfo("onPostExecute");
Toast.makeText(getApplicationContext(), "it is over!", Toast.LENGTH_LONG).show();
super.onPostExecute(unused);
@Override
protected void onPreExecute()
printInfo("onPreExecute");
Toast.makeText(getApplicationContext(), "it is begin!", Toast.LENGTH_LONG).show();
super.onPreExecute();
@Override
protected void onProgressUpdate(Integer... values)
printInfo("onProgressUpdate");
pd.incrementProgressBy(1);
if(values[0] >= 100)
pd.dismiss();
super.onProgressUpdate(values);
@Override
protected Dialog onCreateDialog(int id)
pd = new ProgressDialog(this);
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("完成进度");
pd.setMessage("请稍等...");
pd.setCancelable(false);
return pd;
实现幻灯片切换效果
实现思路还是比较简单,就是创建一个线程,然后永真循环,然后根据计数余数确定改呈现什么样的内容。
核心代码如下:
首先线程类:
package scq.scq.handlertest;
public class MyThread extends Thread
MainActivity activity;
int what = 0;
public MyThread(MainActivity activity)
this.activity = activity;
@Override
public void run()
while (true)
this.activity.handler.sendEmptyMessage((what++)%5);
try
Thread.sleep(300);
catch (Exception e)
e.printStackTrace();
因为每次停留300ms然后进行切换。
然后MainActivity中使用Handler根据余数切换内容:
package scq.scq.handlertest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
TextView tvContent;
ImageView ivPic;
Handler handler = new Handler()
@Override
public void handleMessage(@NonNull Message msg)
switch (msg.what)
case 0:
tvContent.setText(R.string.andy);
ivPic.setImageResource(R.drawable.andy);
break;
case 1:
tvContent.setText(R.string.bill);
ivPic.setImageResource(R.drawable.bill);
break;
case 2:
tvContent.setText(R.string.edgar);
ivPic.setImageResource(R.drawable.edgar);
break;
case 3:
tvContent.setText(R.string.torvalds);
ivPic.setImageResource(R.drawable.torvalds);
break;
case 4:
tvContent.setText(R.string.turing);
ivPic.setImageResource(R.drawable.turing);
break;
super.handleMessage(msg);
;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ivPic = findViewById(R.id.ivPic);
this.tvContent = findViewById(R.id.tvContent);
MyThread myThread = new MyThread(this);
myThread.start();
以上是关于Android移动应用开发之使用异步调用进度条及实现幻灯片切换效果的主要内容,如果未能解决你的问题,请参考以下文章