我正在为此实现无限视图寻呼机我在视图寻呼机中获取3个视图,并且包含一个imageview和一个视频视图的一个xml文件我将这个xml添加到中间和右边的3个视图中当视频来到imageview时当imageview将来到videoview在3个主要视图中消失。但是当我运行应用程序时,我的应用程序将给出堆大小。我不明白为什么应用程序给堆。
这里我用logcat完成了我的完整代码。
logcat的
10-15 12:51:56.840: I/dalvikvm-heap(32135): Grow heap (frag case) to 6.850MB for 2073616-byte allocation
10-15 12:51:56.850: D/dalvikvm(32135): GC_FOR_ALLOC freed 1K, 10% free 6495K/7148K, paused 12ms, total 12ms
10-15 12:51:57.080: D/dalvikvm(32135): GC_FOR_ALLOC freed 33K, 10% free 6496K/7168K, paused 11ms, total 11ms
10-15 12:51:57.080: I/dalvikvm-heap(32135): Grow heap (frag case) to 8.827MB for 2073616-byte allocation
10-15 12:51:57.100: D/dalvikvm(32135): GC_FOR_ALLOC freed <1K, 8% free 8521K/9196K, paused 13ms, total 13ms
10-15 12:52:05.400: E/position scrolled(32135): position--->2
10-15 12:52:05.400: E/position scrolled(32135): iiii--->1
10-15 12:52:05.400: V/position next(32135): ---->
10-15 12:52:06.030: D/next called(32135): --->0
10-15 12:52:06.050: D/dalvikvm(32135): GC_FOR_ALLOC freed 41K, 8% free 8527K/9196K, paused 14ms, total 14ms
10-15 12:52:06.060: I/dalvikvm-heap(32135): Grow heap (frag case) to 10.811MB for 2073616-byte allocation
10-15 12:52:06.070: D/dalvikvm(32135): GC_FOR_ALLOC freed <1K, 6% free 10552K/11224K, paused 12ms, total 12ms
10-15 12:52:06.290: D/dalvikvm(32135): GC_FOR_ALLOC freed 33K, 6% free 10553K/11224K, paused 12ms, total 12ms
10-15 12:52:06.300: I/dalvikvm-heap(32135): Grow heap (frag case) to 12.790MB for 2073616-byte allocation
10-15 12:52:06.310: D/dalvikvm(32135): GC_FOR_ALLOC freed <1K, 6% free 12578K/13252K, paused 12ms, total 12ms
Mainactivity.java
package com.example.copyimagetosdcard;
public class MainActivity extends Activity {
ArrayList<HashMap<String,String>> myplaylist = new ArrayList<HashMap<String,String>>();
String[] sslideimagearray={"/mnt/sdcard/peakmedia/201410141503550814.jpg",
"/mnt/sdcard/peakmedia/201410141504120735.jpg",
"/mnt/sdcard/peakmedia/201410141504190108.mp4",
"/mnt/sdcard/peakmedia/201410141504490486.jpg",
"/mnt/sdcard/peakmedia/201410141504530577.m4v" };
int i=0;
boolean next=false;
boolean previous=false;
Runnable runnable;
final Handler imagehandler = new Handler();
private long runnableMill = 4000;
boolean automode=true;
String videoplay="false";
MediaController controller;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
controller = new MediaController(this);
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomeAdapter = new AwesomePagerAdapter(awesomePager);
awesomePager.setAdapter(awesomeAdapter);
awesomePager.setCurrentItem(1);
awesomePager.setOnPageChangeListener(awesomeAdapter);
awesomePager.setPageMargin(20);
for(int k=0;k<5;k++){
HashMap<String,String> map = new HashMap<String,String>();
if(k==0 || k==1 || k==3){
map.put("type", "image");
}
else{
map.put("type", "video");
}
map.put("path", sslideimagearray[k]);
myplaylist.add(map);
}
Log.d("myplaylist","--->"+myplaylist.size());
Log.d("myplaylist","--->"+myplaylist);
runnable = new Runnable()
{
public void run()
{ // slider image run
Log.v("runnable iiiii","---->"+i);
Log.v("videoplay","---->"+videoplay);
if(videoplay.equals("true"))
{
}
else
{
if(i==myplaylist.size())
{
i=0;
}
if(i==0)
{
runnableMill = 10000;
imagesautomode();
i++;
}
else
{
automode=true;
imagesautomode();
runnableMill = 10000;
i++;
}
}
imagehandler.postDelayed(this, runnableMill); // for interval
}
};
imagehandler.postDelayed(runnable,runnableMill);
}
private class AwesomePagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
public AwesomePagerAdapter(ViewPager p) {
super();
LayoutInflater inflater= LayoutInflater.from(MainActivity.this);
mLeft=inflater.inflate(R.layout.imagevideo,null);
p.addView(mLeft);
mMiddle=inflater.inflate(R.layout.imagevideo,null);
p.addView(mMiddle);
mRight=inflater.inflate(R.layout.imagevideo,null);
p.addView(mRight);
lImageview=(ImageView)mLeft.findViewById(R.id.imageView);
mImageview=(ImageView)mMiddle.findViewById(R.id.imageView);
rImageview=(ImageView)mRight.findViewById(R.id.imageView);
lVideoview=(VideoView)mLeft.findViewById(R.id.videoView);
mVideoview=(VideoView)mMiddle.findViewById(R.id.videoView);
rVideoview=(VideoView)mRight.findViewById(R.id.videoView);
Log.d("constructor called","----->");
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.e("position scrolled","position--->"+position);
Log.e("position scrolled","iiii--->"+i);
if(automode){
i--;
automode=false;
}
if(position==0){
previous=true;
next=false;
Log.v("position previous","---->");
}
else{
next=true;
previous=false;
Log.v("position next","---->");
}
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
// if automode true than we need to adujust i value for display because it was auto incrementd in handler
if(automode){
if(next){
Log.d("next called"," auto mode"+i);
if(i==myplaylist.size()){
i=0;
}
}
else{
Log.d("previous called"," auto mode"+i);
if(i==0){
i=myplaylist.size()-1;
}else{
i--;
i--;
if(i<0){
i=0;
}
}
}
automode=false;
}
else{
automode=false;
if(next){
Log.d("next called","--->"+i);
if(i==myplaylist.size()-1){
i=0;
}
else{
i++;
}
}else{
Log.d("previous called","--->"+i);
if(i==0){
i=myplaylist.size()-1;
}
else{
i--;
}
}
}
images();
awesomePager.setCurrentItem(1, false);
}
}
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
@Override
public Object instantiateItem(View collection, int position) {
if (position == 0) {
return mLeft;
} else if (position == 1) {
return mMiddle;
} else {
return mRight;
}
//return tv;
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
//((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(object);
}
}
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private AwesomePagerAdapter awesomeAdapter;
ImageView mImageview;
ImageView lImageview;
ImageView rImageview;
VideoView lVideoview;
VideoView mVideoview;
VideoView rVideoview;
private View mLeft;
private View mRight;
private View mMiddle;
public void images(){
automode=false;
videoplay="false";
if(i==0 && myplaylist.size()>=2){
String type=myplaylist.get(myplaylist.size()-1).get("type").toString();
String path=myplaylist.get(myplaylist.size()-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(i+1).get("type").toString();
path=myplaylist.get(i+1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[sliderImageArray.length-1]);
mImageview.setImageResource(sliderImageArray[0]);
rImageview.setImageResource(sliderImageArray[i+1]);
*/
}
else if(i==0 && myplaylist.size()==1){
String type=myplaylist.get(0).get("type").toString();
String path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
}
else if(i==myplaylist.size()-1){
String type=myplaylist.get(i-1).get("type").toString();
String path=myplaylist.get(i-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(i).get("type").toString();
path=myplaylist.get(i).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[i-1]);
mImageview.setImageResource(sliderImageArray[i]);
rImageview.setImageResource(sliderImageArray[0]);
*/
}
else{
String type=myplaylist.get(i-1).get("type").toString();
String path=myplaylist.get(i-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(i).get("type").toString();
path=myplaylist.get(i).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(i+1).get("type").toString();
path=myplaylist.get(i+1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[i-1]);
mImageview.setImageResource(sliderImageArray[i]);
rImageview.setImageResource(sliderImageArray[i+1]);
*/
}
}
public void imagesautomode(){
videoplay="false";
automode=true;
if(i==0 && myplaylist.size()>=2){
String type=myplaylist.get(myplaylist.size()-1).get("type").toString();
String path=myplaylist.get(myplaylist.size()-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(i+1).get("type").toString();
path=myplaylist.get(i+1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[sliderImageArray.length-1]);
mImageview.setImageResource(sliderImageArray[0]);
rImageview.setImageResource(sliderImageArray[i+1]);
*/
}
else if(i==0 && myplaylist.size()==1){
String type=myplaylist.get(0).get("type").toString();
String path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
}
else if(i==myplaylist.size()-1){
String type=myplaylist.get(i-1).get("type").toString();
String path=myplaylist.get(i-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(i).get("type").toString();
path=myplaylist.get(i).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(0).get("type").toString();
path=myplaylist.get(0).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[i-1]);
mImageview.setImageResource(sliderImageArray[i]);
rImageview.setImageResource(sliderImageArray[0]);
*/
}
else{
String type=myplaylist.get(i-1).get("type").toString();
String path=myplaylist.get(i-1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(lImageview.getVisibility()==View.GONE){
lImageview.setVisibility(View.VISIBLE);
}
lVideoview.setVisibility(View.GONE);
nextImage(path, 0);
}else{
lImageview.setVisibility(View.GONE);
lVideoview.setVisibility(View.VISIBLE);
}
type=myplaylist.get(i).get("type").toString();
path=myplaylist.get(i).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(mImageview.getVisibility()==View.GONE){
mImageview.setVisibility(View.VISIBLE);
}
mVideoview.setVisibility(View.GONE);
nextImage(path,1);
}else{
videoplay="true";
nextVideo(path);
}
type=myplaylist.get(i+1).get("type").toString();
path=myplaylist.get(i+1).get("path").toString();
if(type.equalsIgnoreCase("image")){
if(rImageview.getVisibility()==View.GONE){
rImageview.setVisibility(View.VISIBLE);
}
rVideoview.setVisibility(View.GONE);
nextImage(path,2);
}else{
rImageview.setVisibility(View.GONE);
rVideoview.setVisibility(View.VISIBLE);
}
/*
lImageview.setImageResource(sliderImageArray[i-1]);
mImageview.setImageResource(sliderImageArray[i]);
rImageview.setImageResource(sliderImageArray[i+1]);
*/
}
}
private void nextVideo(String path){
videoplay="true";
mImageview.setVisibility(View.GONE);
if(mVideoview.getVisibility()==View.GONE){
mVideoview.setVisibility(View.VISIBLE);
}
mVideoview.setVideoURI(Uri.parse(path));
mVideoview.setMediaController(null);
controller.setMediaPlayer(mVideoview);
mVideoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
long duration = mVideoview.getDuration();
Log.e("Video Prepared","---->"+duration);
mVideoview.start();
imagehandler.removeCallbacks(runnable);
imagehandler.postDelayed(runnable, duration);
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
mp.reset();
videoplay="false";
//images();
}
});
mVideoview.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
images();
return true;
}
});
}
private void nextImage(String path,int position)
{
try
{
imagehandler.removeCallbacks(runnable);
imagehandler.postDelayed(runnable, runnableMill);
videoplay="false";
DisplayMetrics metrics = getResources().getDisplayMetrics();
int imageWidth = (int) ( (float) metrics.widthPixels / 1.5 );
int imageHeight = (int) ( (float) metrics.heightPixels / 1.5 );
path=path.replaceAll(" ", "\\ ");
Bitmap bmp = null;
bmp =loadResizedBitmap(path, imageWidth, imageHeight, false);
if(bmp!=null){
if(position==0){
lVideoview.setVisibility(View.GONE);
lImageview.setImageDrawable(null);
lImageview.setImageBitmap(bmp);
lImageview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
else if(position==1){
mVideoview.setVisibility(View.GONE);
mImageview.setImageDrawable(null);
mImageview.setImageBitmap(bmp);
mImageview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
else if(position==2){
rVideoview.setVisibility(View.GONE);
rImageview.setImageDrawable(null);
rImageview.setImageBitmap(bmp);
rImageview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
}
}
catch (Exception e)
{
}
}
public static Bitmap loadResizedBitmap( String filename, int width, int height, boolean exact )
{
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile( filename, options );
if ( options.outHeight > 0 && options.outWidth > 0 ) {
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
while ( options.outWidth / options.inSampleSize > width
&& options.outHeight / options.inSampleSize > height ) {
options.inSampleSize++;
}
options.inSampleSize--;
bitmap = BitmapFactory.decodeFile( filename, options );
if(bitmap != null && exact ) {
bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
System.gc();
}
}
return bitmap;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.copyimagetosdcard.MainActivity" >
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/awesomepager"/>
</RelativeLayout>
imagevideo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
</LinearLayout>
答案 0 :(得分:0)
在您的AndroidManifest.xml
内application
标记内将largeHeap
指定为true
。
android:largeHeap="true"
它可能对你有帮助。