请帮忙!
我在静态上下文中创建 BitmapDrawable 时遇到问题。我正在为学生竞赛创建一个 android 应用程序,并且由于 MainActivity 类开始看起来“脏”(其中有很多代码),我决定制作扩展 MainActivity 的 MainActivityLayout 类。基本上我将一些方法(用于设置布局的方法)从 MainActivity 移动到 MainActivityLayout 类。问题在于我使用 BitmapDrawable 构造函数的 resize() 方法,它“让我慢慢烦恼”,因为需要引用资源才能正确调整位图的大小。
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 并仍然获得相同的效果?任何评论家都受到高度赞赏:)。