0

Please help!

I have a problem with creating a BitmapDrawable in a static context. I am creating an android app for a student contest and since the MainActivity class starts to look "dirty" (having lots of code in it) I decided to make MainActivityLayout class that extends the MainActivity. Basicly I moved some methods (the ones for setting layouts) from MainActivity to MainActivityLayout class. The problem is with resize() method where I use BitmapDrawable constructor wich is "bugging me slowly" because needs a reference to resources in order to resize properly the bitmap.

The MainActivity class :

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getLayoutOne();
    MainActivityLayouts.setLayoutOne();

    getLayoutTwo();
    MainActivityLayouts.setLayoutTwo();

}


private void getLayoutOne(){

//here I get the layouts id using findViewById() method

}

private void getLayoutTwo(){

//here I get the layout 2 id using findViewById() method

}

    protected static Drawable resize(Drawable image,int xSize,int ySize) {

    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, xSize, ySize, false);
    return new BitmapDrawable(getResources(),bitmapResized);

    }

}

The MainActivityLayout class:

public class MainActivityLayouts extends MainActivity{


public MainActivityLayouts(){
// ... 
}

protected final static void setLayoutOne(){

Drawable drawableOne = ImageViewOne.getDrawable();
//here is the problem...cannot make a static refference... 
//getResources() from resize method from MainActivity to be more specific
drawableOne = resize(drawableOne,100,100);
ImageViewOne.setImageDrawable(drawableOne);

}

protected final static void setLayoutTwo(){

Drawable drawableTwo = ImageViewTwo.getDrawable();
//here is the problem...cannot make a static refference... 
//getResources() from resize method from MainActivity to be more specific
drawableTwo = resize(drawableTwo,200,200);
ImageViewTwo.setImageDrawable(drawableTwo);

}

Is there any solution to my problem? Resizing in another mode or can I avoid creating a BitmapDrawable and still obtain the same effect? Any critic is highly appreciated :).

  • I don't really understand what you are trying to achieve, but I think you should delete your second activity and move everything into the first one. If you think that it is too much code for an activity then you can have a look at fragments. – k3v1n4ud3 Mar 19 '14 at 04:01
  • try [this](http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap) – user3355820 Mar 19 '14 at 04:02
  • Thanks for reply. I belive I'll rezise the drawable in mainactivity class and set in the mainactivitylayouts class. – user2592139 Mar 19 '14 at 19:29

0 Answers0