Wednesday, January 22, 2014

Memory overflow outofmemery, with ViewPager memory overflow occurs, seek answers


            
When the sliding five or six Pager and they will burst this error, ViewPager not automatically destroy Pager for you, how will the memory overflow



 package com.siyehuazhilian.nextday; 

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.siyehuazhilian.control.ViewPagerAdapter;
import com.siyehuazhilian.fragment.ViewPagerAdatperFragment;

public class MainActivity extends FragmentActivity implements OnClickListener {
/ / From the beginning of the Lunar November 14, is
12 月 16 日private String startDate = "2013-11-20";
/ / Get today's date
private String today = new SimpleDateFormat ("yyyy-MM-dd")
. Format (new Date ());
/ / The currently selected date
private String currentDay = today;

/ / Declare ViewPager object
private ViewPager viewPager = null;
/ / Declare ViewPager adapter
private ViewPagerAdapter viewPagerAdapter = null;
/ / ViewPagerAdapter data
private ArrayList list;

/ / Sharing dialog
private Dialog dialog;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
viewPager = (ViewPager) findViewById (R.id.viewpager);
list = new ArrayList ();
viewPagerAdapter = new ViewPagerAdapter (getSupportFragmentManager (),
list);

/ / Set the adapter
to ViewPagerviewPager.setAdapter (viewPagerAdapter);
}

@ Override
protected void onResume () {
super.onResume ();
/ / Load data
addViewPagerAdpaterData ();
viewPager.setCurrentItem (3);

}

/ **
* Add specific data for the adapter
*
* @ Throws ParseException
* /
private void addViewPagerAdpaterData () {
try {
ArrayList listDate = new ArrayList ();
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
int count = differenceDay (startDate, today);
/ / This is a Fragment of each fragment adapter, so to create a new class to manage
debrisfor (int i = 0; i listDate.add (new Date (dateFormat.parse (today). getTime ()
- 1000 * 60 * 60 * 24 * i));
list.add (ViewPagerAdatperFragment.newInstance (listDate.get (i)
.. ToString () split ("")));
}

viewPagerAdapter.notifyDataSetChanged ();
} Catch (ParseException e) {
}
}

/ **

Number of days difference between two dates **
* /
private int differenceDay (String day1, String day2) {
try {
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
Date date1 = dateFormat.parse (day1);
Date date2 = dateFormat.parse (day2);
return (int) ((date2.getTime () - date1.getTime ()) / 1000/60/60/24);
} Catch (ParseException e) {
return -10086;
}

}

@ Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (dialog == null) {
dialog = new Dialog (this);
dialog.setTitle ("Share");
dialog.show ();
View layout = getLayoutInflater () inflate (R.layout.share, null);.
dialog.setContentView (layout);
layout.findViewById (R.id.imageview_qqshare)
. SetOnClickListener (this);
layout.findViewById (R.id.imageview_weixinshare). setOnClickListener (
this);
layout.findViewById (R.id.imageview_weiboshare). setOnClickListener (
this);
} Else {
dialog.show ();
}
return super.onPrepareOptionsMenu (menu);
}

@ Override
public void onClick (View v) {
dialog.dismiss ();
shootScreen ();
Toast.makeText (
this,
"Screenshot success has been saved to:" + Environment.getExternalStorageDirectory () + "/"
+ CurrentDay + ". Jpg", Toast.LENGTH_SHORT) show ();.
switch (v.getId ()) {
case R.id.imageview_qqshare:

break;
case R.id.imageview_weixinshare:

break;
case R.id.imageview_weiboshare:
Intent intent = new Intent (Intent.ACTION_SEND);

/ / Intent.setType ("text / plain");
intent.setType ("image / jpeg");
/ / Intent.putExtra (Intent.EXTRA_SUBJECT, "sharing");
/ / Intent.putExtra (Intent.EXTRA_TEXT,
/ / "I would like to share this with you ...");
Uri uri = Uri.parse (Environment.getExternalStorageDirectory () + "/"
+ CurrentDay + ". Jpg");
intent.putExtra (Intent.EXTRA_STREAM, uri);
startActivity (intent);
break;

default:
break;
}
}
Bitmap bitmap1 = null;

/ **
* Screenshot
* /
private void shootScreen () {
View view = getWindow () getDecorView ();.
view.setDrawingCacheEnabled (true);
view.buildDrawingCache ();
Bitmap b1 = view.getDrawingCache ();
Bitmap bitmap = Bitmap.createBitmap (b1, 0, 0, b1.getWidth (),
b1.getHeight ());
bitmap1 = bitmap;
view.destroyDrawingCache ();

try {
OutputStream outputStream = new FileOutputStream (
Environment.getExternalStorageDirectory () + "/"
+ CurrentDay + ". Jpg");
bitmap.compress (CompressFormat.PNG, 100, outputStream);
outputStream.flush ();
outputStream.close ();
} Catch (Exception e) {
e.printStackTrace ();
}
}
}






Reply:

 package com.siyehuazhilian.fragment; 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.siyehuazhilian.nextday.R;
import com.siyehuazhilian.utils.DownRescoureces;

public class ViewPagerAdatperFragment extends Fragment {
private View layoutinflater;
/ / Background image
private ImageView imageView;
/ / Characters Description
private TextView largeSize;
/ / Small print instructions
private TextView smallSize;
/ / Date
private TextView monthTextView, yearTextView, weekTextView, dateTextView;
/ /
private LinearLayout dateLayout, informationLayout;
/ / Hide picture factor of 0 means into the front, hiding other controls, 1 put the final surface, does not hide other controls
private int hideImage = 0;

/ / Current time
private String [] dateStr = new String [6];
/ / Current display verse
private String [] textStr = new String [2];

/ / Load the resource object
private DownRescoureces downRescoureces = new DownRescoureces ();

public static ViewPagerAdatperFragment newInstance (String [] dateStr) {
ViewPagerAdatperFragment adatperFragment = new ViewPagerAdatperFragment ();

Bundle bundle = new Bundle ();
bundle.putString ("week", dateStr [0]);
bundle.putString ("month", dateStr [1]);
bundle.putString ("day", dateStr [2]);
bundle.putString ("year", dateStr [5]);

adatperFragment.setArguments (bundle);
return adatperFragment;
}

@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
layoutinflater = inflater.inflate (R.layout.pager_adapter_view, null);
return layoutinflater;
}

@ Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated (savedInstanceState);
dateLayout = (LinearLayout) layoutinflater
. FindViewById (R.id.linerlayout_date);
informationLayout = (LinearLayout) layoutinflater
. FindViewById (R.id.linearlayout_information);
imageView = (ImageView) layoutinflater
. FindViewById (R.id.imageview_image);
largeSize = (TextView) layoutinflater
. FindViewById (R.id.textview_large_size);
smallSize = (TextView) layoutinflater
. FindViewById (R.id.textview_small_size);
monthTextView = (TextView) layoutinflater
. FindViewById (R.id.textiview_month);
yearTextView = (TextView) layoutinflater
. FindViewById (R.id.textiview_year);
weekTextView = (TextView) layoutinflater
. FindViewById (R.id.textiview_week);
dateTextView = (TextView) layoutinflater
. FindViewById (R.id.textview_day);

/ / Get the data
dateStr [0] = getArguments () getString ("week");.
dateStr [1] = getArguments () getString ("month");.
dateStr [2] = getArguments () getString ("day");.
dateStr [5] = getArguments () getString ("year");.

imageView.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
if (hideImage == 0) {
Animation animation = new AlphaAnimation (1.0f, 0.0f);
animation.setDuration (3000);
dateLayout.startAnimation (animation);
informationLayout.startAnimation (animation);
dateLayout.setVisibility (View.GONE);
informationLayout.setVisibility (View.GONE);
hideImage + +;
} Else {
Animation animation = new AlphaAnimation (0.0f, 1.0f);
animation.setDuration (3000);
dateLayout.startAnimation (animation);
informationLayout.startAnimation (animation);
dateLayout.setVisibility (View.VISIBLE);
informationLayout.setVisibility (View.VISIBLE);
hideImage -;
}
}
});

}

@ Override
public void onResume () {
super.onResume ();
setText ();
setImage ();
setDate ();
}

/ **
* Set Date
* /
private void setDate () {
monthTextView.setText (dateStr [1]);
yearTextView.setText (dateStr [5]);
weekTextView.setText (dateStr [0]);
dateTextView.setText (dateStr [2]);
}

/ **
* Set picture
* /
private void setImage () {
int currentDay = Integer.parseInt (dateStr [2]);
Field [] fields = R.drawable.class.getDeclaredFields ();

try {
DisplayMetrics display = new DisplayMetrics ();
getActivity (). getWindowManager (). getDefaultDisplay ()
. GetMetrics (display);
int width = display.widthPixels;
int height = display.heightPixels;
imageView.setImageBitmap (optimizeBitmap (getResources (),
fields [currentDay - 1]. getInt (R.drawable.class), width,
height));
} Catch (IllegalArgumentException e) {
e.printStackTrace ();
} Catch (IllegalAccessException e) {
e.printStackTrace ();
}
}

/ **
* Reading the image
* /
public Bitmap optimizeBitmap (Resources resources, int drawableID,
int maxWidth, int maxHeight) {
Bitmap result = null;
/ / Image configuration object that can be configured to obtain the number of pixel image loaded
BitmapFactory.Options options = new BitmapFactory.Options ();
/ / Represents the original image width and height
loadoptions.inJustDecodeBounds = true;
result = BitmapFactory.decodeResource (resources, drawableID, options);
/ / Math.ceil retrieves its nearest integer (up values ​​such as: 4.1-> 5 4.9-> 5)
int widthRatio = (int) Math.ceil (options.outWidth / maxWidth);
int heightRatio = (int) Math.ceil (options.outHeight / maxHeight);

options.inSampleSize = 1;
Pixel ratio / / set the final loading, which means that the final number of pixels displayed is the total number of
if (widthRatio> 1 | | heightRatio> 1) {
if (widthRatio> heightRatio) {
options.inSampleSize = widthRatio;
} Else {
options.inSampleSize = heightRatio;
}
}
/ / Decoding pixel mode, this mode can be taken directly in accordance with the pixel
configuration optionsoptions.inJustDecodeBounds = false;
result = BitmapFactory.decodeResource (resources, drawableID, options);
return result;
}

/ **
* Set the text
*
* /
private void setText () {
try {
InputStream inputStream = getResources (). GetAssets ()
. Open ("100.txt");
BufferedReader bufferedReader = new BufferedReader (
new InputStreamReader (inputStream, "gb2312"));
int currentDay = Integer.parseInt (dateStr [2]);
String tmp = "";
for (int i = 0; i tmp = bufferedReader.readLine ();
}
textStr = tmp.split ("-");
largeSize.setText (textStr [1]);
smallSize.setText (textStr [0]);
smallSize.setSelected (true);
} Catch (IOException e) {
e.printStackTrace ();
}
}

}

No comments:

Post a Comment