2

我在通过动画幻灯片上的方法缩放图像的集成方法时遇到问题。我的目标是我有 10 张大图像用于幻灯片放映,当我尝试测试 2 张图像时,它工作得很好,但是当我尝试使用所有图像时,它给了我错误 java.lang.OutOfMemoryError: 位图大小超过 VM预算,我在互联网上搜索,我想出了缩放图像或使用位图,我得到了几个代码,但我的问题是如何将我从网上获得的位图/解码代码集成到我的幻灯片脚本中。

下面是脚本:

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class SliderActivity extends Activity {

    public int currentimageindex=0;
    Timer timer;
    TimerTask task;
    ImageView slidingImage;

    private int[] IMAGE_IDS = {
            R.drawable.pic1edited, R.drawable.pic2edited, R.drawable.pic3edited,
            R.drawable.pic4edited, R.drawable.pic5edited        
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set fullscreen and no title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.main_slider); 

        Bitmap image = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier( "imagename" , "drawable", getPackageName()));

        final Handler mhandler = new Handler();

        //Create runnable for posting      
        final Runnable mUpdateResults =  new Runnable() {

            @Override
            public void run() {
                AnimateandSlideShow();
            }
        };

        int delay = 1000; //delay for 1 sec
        int period = 8000; //repeat every 4 sec

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {

                mhandler.post(mUpdateResults);

            }
        }, delay, period);
    }

    //Helper method to start the animation on the splash screen
    private void AnimateandSlideShow() {



        slidingImage = (ImageView)findViewById(R.id.imageView1);
        slidingImage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

        currentimageindex++;

        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);

        slidingImage.setAnimation(rotateimage);
        Log.d(getClass().getName(), "value = " + currentimageindex);

    }  

    private Drawable resize(Drawable image) {
        Bitmap b = ((BitmapDrawable)image).getBitmap();
        Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false);
        return new BitmapDrawable(bitmapResized);
    }

    private Bitmap decodeFile(File f){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_SIZE=70;

            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

        } catch (FileNotFoundException e) {}

            return null;
    }

    //override some buttons
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //Log.d("button", new Integer(keyCode).toString());

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            return false;
        }
        else if ((keyCode == KeyEvent.KEYCODE_CALL)) {
            return false;
        } 
        else if ((keyCode == KeyEvent.KEYCODE_MENU)) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();

    }


}
4

0 回答 0