在静态上下文中创建BitmapDrawable

时间:2014-03-19 03:20:56

标签: java android bitmap static bitmapdrawable

请帮忙!

我在静态上下文中创建BitmapDrawable时遇到问题。 我正在为学生竞赛创建一个Android应用程序,因为MainActivity类启动了 看起来“脏”(里面有很多代码)我决定创建扩展MainActivity的MainActivityLayout类。基本上我将一些方法(用于设置布局的方法)从MainActivity移动到MainActivityLayout类。问题是resize()方法,我使用BitmapDrawable构造函数“慢慢地烦我”,因为需要引用资源才能正确调整位图大小。

MainActivity类:

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);

    }

}

MainActivityLayout类:

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);

}

我的问题有解决办法吗?在另一种模式下调整大小,还是可以避免创建BitmapDrawable并仍然获得相同的效果? 任何评论家都非常感激:)。

0 个答案:

没有答案