EDITED
我有这堂课:
public class Item {
private Bitmap image;
public Item(Bitmap image) {
this.image = image;
}
我还有主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//the question is about this line
Item item = new Item(???);
}
}
如何从MainActivity(onCreate里面)调用Item的构造函数?我不知道如何从参考资料中引用位图。
我想传递给构造函数的图像位于: app>> res>> mipmap>> cat.png
答案 0 :(得分:0)
在这里,您可以将位图转换为可绘制的
Drawable myDrawable = getResources().getDrawable(R.drawable.cat);
Bitmap bitmap= ((BitmapDrawable) myDrawable).getBitmap();
答案 1 :(得分:0)
在java中,您可以通过构建类的实例来调用构造函数。
你问题中的:
new Item(bitmap);
您可以通过执行以下操作来引用“活动”中的资源位图:
Bitmap bitmap = BitmapFactory.decodeResource(getResources() , R.drawable.cat);
并在片段中:
Bitmap bitmap = BitmapFactory.decodeResource(getAvtivity().getResources() , R.drawable.cat);
答案 2 :(得分:0)
Bitmap image = BitmapFactory.decodeResource(getResources() , R.drawable.cat);