繁体   English   中英

我正在尝试从可绘制对象中获取位图

[英]i am trying to get Bitmap from a drawable

我正在尝试从 drawable 中获取位图我使用此代码,但它不起作用,因为我无法在静态内部使用上下文

这是我这个类的完整代码:

 package com.onazifi.shelf; import android.content.Context; import android.content.Intent; import android.content.res.AssetManager; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import org.antlr.runtime.BitSet; import java.io.IOException; import java.io.InputStream; public class BookItem { private String author; private String title; private Bitmap image; Context context; public BookItem() { // TODO Auto-generated constructor stub } public BookItem(String _author, String _title , Bitmap _image) { this.author = _author; this.title = _title; this.image = _image; } public String getAuthor() { return this.author; } public void setAuthor(String _author) { this.author = _author; } public String getTitle() { return this.title; } public void setTitle(String _title) { this.title = _title; } public Bitmap getImage() { return this.image; } public void setImage(Bitmap _image) { this.image = _image; } Bitmap _image = BitmapFactory.decodeResource(context.getResources(), R.drawable.gbrankhalil2); public static final BookItem[] ALL_BOOKS={ new BookItem("1","test" ,BitmapFactory.decodeResource(context.getResources(), R.drawable.gbrankhalil2) ) }; }

问题就在这里

 BitmapFactory.decodeResource(context.getResources(), R.drawable.gbrankhalil2)

我无法在静态内部调用上下文,如何使用位图从静态内部调用 drawable

更新 :

这是数组列表存在的地方

 public class Library { private ArrayList<BookItem> arrayBookItem; public static final int AUTHOR = 1; public static final int TITLE = 2; public static final int IMAGE = 3; public static final int RATE = 4; public static final int DOWNLOAD_DATE =5; public Library() { arrayBookItem = new ArrayList<BookItem>(); } public void setColectionBookItem(ArrayList<BookItem> _array) { this.arrayBookItem = _array; } public void addBookItem(BookItem _bi) { this.arrayBookItem.add(_bi); } public ArrayList<ArrayList<BookItem>> groupbyArrayBookItem(int type) { BookItem[] books = BookItem.ALL_BOOKS; ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>(); String getType = ""; switch (type) { case AUTHOR: getType = "bookitem.getAuthor()"; break; case TITLE: getType = "bookitem.getTitle()"; break; case IMAGE: getType = "bookitem.getImage"; break; case DOWNLOAD_DATE: getType = "bookitem.getDownloadDate()"; break; case RATE: getType = "bookitem.getRate()"; break; default: return groupList; } /* * books is a object of BookItem * bookitem is item for point to list * getType is a string value for set type of grouping * groupbyArrayBookItem return back array of array of items */ Iterable<Group> groups = from("bookitem").in(books).group("bookitem") .by(getType).into("g").select("g"); for (Group group : groups) { ArrayList<BookItem> obj = new ArrayList<BookItem>(); for (Object Item : group.getGroup()) { obj.add((BookItem) Item); } groupList.add(obj); } return groupList; } }

通过constructor传递context然后使用它。

public BookItem(Context context, String _author, String _title , Bitmap _image) {
    this.context = context;
    this.author = _author;
    this.title = _title;
    this.image = _image;
}

或者

public BookItem(Context context) {
    this.context = context;
}

然后使用它。

BitmapFactory.decodeResource(context.getResources(),
            R.drawable.gbrankhalil2)

在任何活动中调用它。

BookItem item = new BookItem(MainActivity.this, _author, _title, _image);

或者如果它是片段

BookItem item = new BookItem(getContext(), _author, _title, _image);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM