我正在尝试为Android编写一个简单的应用程序。我正在尝试加载7个jpg图像。如何在每个图像视图中加载图像时显示动画循环进度条。我使用两个进度条来执行此操作。我已尝试下面给出的代码。
if (mainImageURl.startsWith("http://")) {
//resultp = arraylist.get(0);
//Picasso.with(getActivity()).load(mainImageURl).into(mainImageView);
//imageLoader.DisplayImage((mainImageURl), mainImageView);
Picasso.with(getActivity())
.load(mainImageURl)
.into(mainImageViewnew Callback() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
progress1.setVisibility(View.GONE);
}
@Override
public void onError() {
// TODO Auto-generated method stub
}
});
}
else if (stage1ImageURL.startsWith("http://")) {
//resultp = arraylist.get(0);
//Picasso.with(getActivity()).load(mainImageURl).into(mainImageView);
//imageLoader.DisplayImage((mainImageURl), mainImageView);
Picasso.with(getActivity())
.load(mainImageURl)
.into(mainImageViewCallback() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
progress2.setVisibility(View.GONE);
}
@Override
public void onError() {
// TODO Auto-generated method stub
}
});
}
答案 0 :(得分:1)
我创建了一个asynctask" ImageLoading"。这将加载" loadingimg" gif on pre方法..并尝试从web-service(在php中构建)获取背景方法上的图像。 当图像背景返回时,它将使加载gif不可见,并在返回true时加载图像。
确保启动xampp服务器(apache以及mysql)
xampp-> [内部htdocs-> imgloading文件夹]
" showLoading.php"
<?php
$id=$_GET["id"];
$con=mysqli_connect("localhost","root","","imgloading");
$query_result=mysqli_query($con,"select img from img where id=1");
//since id=1 contain the loading gif image...which shows until original image loaded
while($row=mysqli_fetch_row($query_result))
{
$img=$row[0];
}
echo '<img src="data:image/*;base64,'.base64_encode($img).'" width="32" heigth="32"/>';?>
&#34; getImage.php&#34;
<?php
$id=$_GET["id"];
$con=mysqli_connect("localhost","root","","imgloading");
$query_result=mysqli_query($con,"select img from img where id=".$id);
while($row=mysqli_fetch_row($query_result))
{
$img=$row[0];
}
echo $img;
?>
公共类MainActivity扩展了ActionBarActivity {
私有WebView loadingImg; private ImageView img; 私有位图位图; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadingImg=(WebView)findViewById(R.id.imageView1);
img=(ImageView)findViewById(R.id.imageView2);
loadingImg.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
ImageLoading imgLoading=new ImageLoading();
imgLoading.execute();
}
private class ImageLoading extends AsyncTask<Void, Void, Boolean>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
loadingImg.getSettings().setJavaScriptEnabled(true);
loadingImg.loadUrl("http://10.0.2.2/imgloading/showLoading.php");
Log.i("img","pre");
}
@Override
protected Boolean doInBackground(Void... params)
{
Log.i("img","background");
try
{
HttpClient client=new DefaultHttpClient();
HttpGet request=new HttpGet("http://10.0.2.2/imgloading/getImage.php?id=2");
HttpResponse response=client.execute(request);
HttpEntity responseEntity=response.getEntity();
final byte[] imgByteArray=EntityUtils.toByteArray(responseEntity);
bitmap=BitmapFactory.decodeByteArray(imgByteArray, 0, imgByteArray.length);
responseEntity.consumeContent();
Log.i("img","inside try");
return true;
}
catch(Exception e)
{
e.printStackTrace();
Log.i("img","inside catch");
Log.i("img", e.toString());
return false;
}
}
@Override
protected void onPostExecute(Boolean result)
{
super.onPostExecute(result);
Log.i("img","post");
if(result==true)
{
img.setVisibility(View.GONE);img.setVisibility(View.VISIBLE);
Log.i("img","result true");
img.setImageBitmap(bitmap);
}
else
{
img.setVisibility(View.GONE);img.setVisibility(View.GONE);
Log.i("img","result false");
}
}
}
}
<WebView
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:background="#f4f4f4" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:visibility="invisible" />