0

I've found the following code and I want to see a background image instead of a static color. I don't know how to fix the getResources error.

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.text.format.Time;
import hu.rycus.watchface.commons.NonAmbientBackground;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Background extends NonAmbientBackground {

private Bitmap bitmap;
private Bitmap sBbitmap;

@Override
protected void onSizeSet(final int width, final int height, final boolean round) {
    super.onSizeSet(width, height, round);

    paint.setColor(0xFF0000AA);

    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Canvas canvas = new Canvas(bitmap);
    canvas.drawRect(0, 0, width, height, paint);

}

@Override
protected void onDrawBackground(final Canvas canvas, final Time time) {
    canvas.drawBitmap(bitmap, 0, 0, paint);
}

I've tried to change the code in protected void onSizeSet to:

    Resources resources = Background.this.getResources();
    int imgResId;
    imgResId = R.drawable.wood;

    Drawable backgroundDrawable = resources.getDrawable(imgResId);
    bitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
    canvas.drawBitmap(bitmap, 0, 0, null);

But then I get an error because it can't find the getResources.

edit: I've found something here: getResources does not work / undefined Java And if I use this in my code just under the imports then it give's no error.

What do I need to change after Resources resources = ?

Because now it says: Expected class or package.

Community
  • 1
  • 1
  • Why are you trying to access it as Background.this.getResources() ? And does Background have a getResources() method defined? – Erik Dec 23 '14 at 14:47
  • Because that's how it worked in another code. But in the other code i can't find anything about defining a getResources() method. How can I define such method? – Rik Clephas Dec 23 '14 at 15:16
  • You need an object of type `Context` (http://developer.android.com/reference/android/content/Context.html) to call it's `getResources` method. – Henry Dec 23 '14 at 16:08

0 Answers0