我正在开发Android应用,
一旦此应用程序的一部分创建包含一些图像的GridView(图像从URL加载),
然后,当GridView中的一个图像处于onClick状态时,将出现另一个以全屏显示所选图像的活动。
问题:
当我进入GridView活动时,通常需要花费一些时间来加载gridview中的所有图像。
然后我单击其中一个图像进入全屏活动,然后单击后退按钮返回GridView,
但是,返回gridview时加载需要几秒钟,就像再次加载所有图像一样。
我想知道为什么gridView活动会在onResume上加载几秒钟?
例如,在Google Play中,任何应用中的示例图片的全屏视图都可以立即返回到上一个视图。
封闭代码:
GridView的:
public class ManagePhoto extends Activity {
ImageAdapter ia;
GridView gridview;
InputStream inputStream;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
TextView tvRunningMark;
boolean bRunning;
String[] purl;
Bitmap[] bm;
String the_string_response;
TouchImageView touch;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_manage);
//gv is Gobal Value
final Gvalue gv = ((Gvalue) getApplicationContext());
gridview = (GridView) findViewById(R.id.gv_photo);
gridview.setOnItemClickListener(new GridView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view,int position, long id) {
gv.setbm(bm[position]);
Intent myIntent = new Intent(adapterView.getContext(), FullScreenImage.class);
startActivityForResult(myIntent, 0);
}
});
new GridTask().execute();
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return purl.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200,200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
try {
bm[position] = loadBitmap(purl[position]);
imageView.setImageBitmap(bm[position]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageView;
}
}
class GridTask extends AsyncTask<Void, String, Void> {
@Override
protected void onPostExecute(Void result) {
gridview.setAdapter(ia);
final LinearLayout llo_probar = (LinearLayout)findViewById(R.id.llo_probar);
llo_probar.setVisibility(LinearLayout.GONE);
gridview.setVisibility(GridView.VISIBLE);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(String... values) {
}
@Override
protected Void doInBackground(Void... params) {
getphoto();
bm = new Bitmap[purl.length];
ia = new ImageAdapter(ManagePhoto.this);
return null;
}
}
private Bitmap loadBitmap(String url) throws MalformedURLException,IOException {
return BitmapFactory.decodeStream(new FlushedInputStream(
(InputStream) new URL(url).getContent()));
}
class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(final InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(final long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int bytesRead = read();
if (bytesRead < 0) { // we reached EOF
break;
}
bytesSkipped = 1;
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
public void getphoto(){
final Gvalue gv = ((Gvalue) getApplicationContext());
final TextView tv_fn = (TextView) findViewById(R.id.tv_fn);
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sql",
("select * from personal_photo where member_id = " + gv.getuid())));
InputStream is = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://" + gv.getserverIP()
+ "/android_getdata.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
List url = new ArrayList();
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
url.add(json_data.getString("save_location"));
}
int size = url.size();
purl = new String[size];
for (int j = 0; j < size; j++) {
purl[j] = (String) url.get(j);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
public void toast(String text) {
Toast.makeText(ManagePhoto.this, text, 5).show();
}
}
全屏:
public class FullScreenImage extends Activity {
TouchImageView touch;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
new ShowPhotoTask().execute();
}
class ShowPhotoTask extends AsyncTask<Void, String, Void> {
@Override
protected void onPostExecute(Void result) {
setContentView(touch);
}
@Override
protected void onPreExecute() {
setContentView(R.layout.full_image);
}
@Override
protected void onProgressUpdate(String... values) {
}
@Override
protected Void doInBackground(Void... params) {
final Gvalue gv = ((Gvalue) getApplicationContext());
touch = new TouchImageView(FullScreenImage.this);
touch.setMaxZoom(4f); // change the max level of zoom, default is 3f
touch.setImageBitmap(gv.getbm());
return null;
}
}
}
答案 0 :(得分:0)
在你的gridview onclick中,你开始使用startActivityForResults(intent,0);用startActivity(intent)替换它;
当您完成FullScreenActivity时,只需使用finish();
可能会解决您的问题
答案 1 :(得分:0)
它太慢了,因为你没有用后台线程执行操作,缓存.. 简单和更好解决方案可以是一个集合,其中包含AsyncTasks插入的所有位图,您将执行这些位图以下载图片。
有更好的解决方案,但它们更难实施。 例如,你可以考虑保留一个线程池来解析你的runnables代表“下载http://jhon.doe.jpg”然后“立即在UI线程上显示”。
答案 2 :(得分:0)
每次从互联网上重新加载图片时,只需getView
代码ImageAdapter
。
您应该将图像下载到本地,下次设置图像时,首先尝试从本地获取图像。
同样,您应该将get位图放在线程中,因为您已将解析JSON放入线程中。
这是一个demo,我认为它会对你有帮助。
答案 3 :(得分:0)
正如您在getView()方法中编写的以下函数:
bm[position] = loadBitmap(purl[position]);
我想说你应该实现代码异步加载图像。在此逻辑中,一旦下载完成,图像就会在存储卡中同步。因此,下次它将直接从存储卡加载,而不是再次从网络加载。
以下是您可以尝试的代码示例:Android - Universal Image Loader