2

If I place the pictures with different resolution into different drawable folders, it will not stretch and it will select the pictures from one of the drawable folders. However, the pictures still come out off.

If I place the pictures into only 1 drawable folder, for some reasons, the pictures will be stretched way bigger than the actual phone size. It's like I can only see the middle part of the pictures.

I also noticed frames being skipped.

Could someone please give me some advices?

Thank you and I very appreciate for your help.

main class:

  public class main_activity extends WallpaperService {



   @Override
    public void onCreate() {
    super.onCreate();
    }

   @Override
    public void onDestroy() {
    super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
    eturn new wallpaperEngine();    
     }

    class wallpaperEngine extends Engine implements 
     SharedPreferences.OnSharedPreferenceChangeListener{

    private final Handler mhandler = new Handler();     

    private final Runnable drawrunnable = new Runnable() 
    {
        public void run() {
            drawFrame();
        }
    };      

    private boolean mVisible;
    private SharedPreferences mPrefs;
    private int i = 0;


    int[] pirates = {

    };


    wallpaperEngine() 
    {

    mPrefs = main_activity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
    mPrefs.registerOnSharedPreferenceChangeListener(this);
    onSharedPreferenceChanged(mPrefs, null);
    }

    public void onSharedPreferenceChanged(SharedPreferences mPrefs2, String key) {  
    }


    @Override
    public void onCreate(SurfaceHolder holder){
        super.onCreate(holder);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mhandler.removeCallbacks(drawrunnable);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        mVisible = visible;
        if (visible) {
            drawFrame();
        } else {
            mhandler.removeCallbacks(drawrunnable);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        drawFrame();    
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceDestroyed(holder);
        mVisible = false;
        mhandler.removeCallbacks(drawrunnable);
    }


    @Override
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,float yStep, int xPixels, int yPixels) {
        drawFrame();
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
    }

    private void drawFrame() {
        // TODO Auto-generated method stub
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {


                drawPirate(c);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
        mhandler.removeCallbacks(drawrunnable);
            if (mVisible) {
                mhandler.postDelayed(drawrunnable);
            }
        }

    private void drawPirate(Canvas c) {
        // TODO Auto-generated method stub

        Bitmap icon;              

        c.drawBitmap(icon, matrix, null);
        icon.recycle();
     }

}

}

Edit: fixed title

Edit: I removed Paint in Black. It's not supposed to be there

IYM-14
  • 260
  • 2
  • 11

1 Answers1

1

You could give a rect with the desired dimmensions using:

public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)

The bitmap will be automatically transformed to the rectangles dimensions. Or actually fill the matrix you just created with the size values so it scales. Size values I would assume you would get from either the screen or the view it's inserted in.

It's hard to help any further if you don't specify what you are trying to do.

More here: http://developer.android.com/reference/android/graphics/Canvas.html

  • Thanks a lot for the reply. All I want to do is to display the pictures as slideshow and I like all the pictures to be stretched to the correct dimension/size on all the Android phones. What that means is it is supposed to get the size of the phone and then stretches the pictures accordingly. – IYM-14 Apr 11 '14 at 11:43
  • Hum, is there a reason you are specifically drawing on a canvas and not just loading ImageViews? Those allow you to set certain layout formats like fitXY and so on, that would scale your images to the layout size you give them. – user2881881 Apr 11 '14 at 14:13
  • Honestly, I got some of my codes online, so I am still trying to understand them since I am still a newbie. Would you please show me how to fix it? Thank you – IYM-14 Apr 11 '14 at 15:11
  • How do you plan on changing images on your slideshow? – user2881881 Apr 11 '14 at 16:49