I have an Activity which loads a custom ImageView ( https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java). Inside onCreate, I use setImageBitmap to display a huge image (4Mb…).
The problem is that this activity takes 4 seconds to show, but there is no blocking method which I could place in a AsyncTask. What I mean is that all my Activity code is executed, but the activity comes only when the image is loaded, and I don’t know when its finish.
What I want to do is showing a ProgressDialog while the Activity loads it, and displays it when it’s done.
How can I resolve this ? Here is some code.
Hope you’ll understand my problem, it’s pretty hard to explain. Thanks !
carte.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<loki.test.lokitest.affichage.TouchImageView
android:id="@+id/image"
android:src="@drawable/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/bouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/textBouton"/>
</RelativeLayout>
CarteActivity.java (The one who takes 4 seconds)
public class CarteActivity extends Activity implements OnClickListener{
private Group groupe;
private String android_id;
private ConnexionServeur serveur;
private TouchImageView vue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Loki","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.carte);
findViewById(R.id.bouton).setOnClickListener(this);
vue = ((TouchImageView)findViewById(R.id.image));
vue.setImageBitmap(Singleton.getInstance().getImage());
groupe = Singleton.getInstance().getGroupe();
android_id = Singleton.getInstance().getAndroid_id();
serveur = Singleton.getInstance().getServeur();
if(groupe != null)
groupe.afficherGroupe(vue);
}
}
EDIT :
The image is loaded with :
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.myImage);
But I don’t think that this is this code which takes time, it is already done when i use setImageBitmap. I can’t figure out what takes so much time, but I wonder if it’s a metod I can access.
Make Layout xml top container (Top RelativeLayout) carry two elements
1 Progress Bar – With Visibility Set to Visible. Make it large and in the center
2 Relative Layout – Entire Content Body . Make it invisible first. It will be under progressbar and will be filling entire screen both height and width wise.
In oncreate setContentView and then starts a AysncTask that loads the imageview in doInBackground.In its postExecute make content body visible and progress bar invisible.
Postexecute will get called only after when your loading is complete
Hope this help and you can translate all this in code.