我正在尝试开发壁纸应用程序。有两个用于更改图像的按钮(下一个和后一个)。这一切都很顺利。
现在,我想在单击“下一步”更改图像时显示一段时间的进度条(图像在不到一秒的时间内更改)。请给我一个如何做的提示,我完全不知道。
这是我的主要活动。
public class Main extends Activity {
private ImageView hImageViewPic;
private Button iButton, gButton;
private int currentImage = 0;
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4,
R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bnext);
gButton = (Button) findViewById(R.id.bprev);
//Just set one Click listener for the image
IButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
}
答案 0 :(得分:1)
按如下方式更改您的代码:
public class Main extends Activity {
private ImageView hImageViewPic;
private Button iButton, gButton;
private int currentImage = 0;
private Handler handler = null;
private ProgressDialog progressDialog = null;
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4,
R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
hImageViewPic.setImageResource(images[currentImage]);
iButton = (Button) findViewById(R.id.bnext);
gButton = (Button) findViewById(R.id.bprev);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
private void changeImage(View v){
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please Wait...");
progressDialog.show();
new Thread(new Runnable() {
public void run() {
long starttime = SystemClock.currentThreadTimeMillis();
long stopwatch = 0;
//Use Handler to change your Image here
handler.post(new Runnable() {
@Override
public void run() {
if (currentImage < 0)
currentImage = images.length - 1;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
});
do{
stopwatch =(SystemClock.currentThreadTimeMillis()-starttime);
if(stopwatch > 500)
break;
}while(true);
//The loop will always execute for 5000 milliseconds
progressDialog.dismiss();
}
}).start();
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
changeImage(v);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Decrease Counter to move to next Image
currentImage--;
changeImage(v);
}
};
}
答案 1 :(得分:1)
嗯,您需要使用视图的可见性并添加ProgressBar
。
请参阅我的xml以了解您应该使用的布局(+添加按钮):
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"/>
</RelativeLayout>
加载图片后,您可以更改ProgressBar
&amp; ImageView
。您可以使用AsyncTask实现这一点,例如:
private class LoadImage extends AsyncTask<Void, Void, Drawable> {
@Override
protected Drawable doInBackground(Void... params) {
return getResources().getDrawable(images[currentImage]);
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
hImageViewPic.setImageDrawable(result);
}
// you can add animation transition (fadein) if you want to
hImageViewPic.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
super.onPostExecute(result);
}
}
答案 2 :(得分:0)
这样的事情怎么样?
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Show dialog which will show
ProgressDialog dialog = ProgressDialog.show(Main.this, "Loading", "Please wait...", true);
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
//everything is complete so we can hide the progress dialog
dialog.dismiss();
} };