Java 如何设置图像资源?
我想在Java 如何设置图像资源?,java,android,imageview,baseadapter,Java,Android,Imageview,Baseadapter,我想在picture.setImageResource()处将ImageView从字符串转换为Int我使用字符串来更改整数,它不会显示在GridView中。如何获取图像大小和设置图像视图 Menu.java public class Menu { private Integer id; private String menuImage; public Integer getId() { return id; } public void setId(I
picture.setImageResource()处将ImageView从字符串转换为Int代码>我使用字符串来更改整数,它不会显示在GridView中。如何获取图像大小和设置图像视图
Menu.java
public class Menu {
private Integer id;
private String menuImage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMenuImage() {
return menuImage;
}
public void setMenuImage(String menuImage) {
this.menuImage = menuImage;
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
List<Menu> mItems = new ArrayList<Item>(); // from server
LayoutInflater mInflater;
public MenuGridViewAdapter(Context context, List<Menu> menuList) {
mInflater = LayoutInflater.from(context);
this.menuList = menuList;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Menu getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
}
picture = (ImageView) v.getTag(R.id.picture);
Item item = getItem(i);
picture.setImageResource( ); //
return v;
}
公共类MyAdapter扩展了BaseAdapter{
List mItems=new ArrayList();//来自服务器
拉平机;
公共菜单RIDViewAdapter(上下文,列表菜单列表){
mInflater=LayoutInflater.from(上下文);
this.menuList=menuList;
}
@凌驾
public int getCount(){
返回mItems.size();
}
@凌驾
公共菜单项(int i){
返回mItems.get(i);
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
视图v=视图;
图像视图图片;
文本视图名称;
如果(v==null){
v=最小平坦度。充气(R.layout.grid_项,视图组,false);
v、 setTag(R.id.picture,v.findViewById(R.id.picture));
}
picture=(ImageView)v.getTag(R.id.picture);
项目=获取项目(i);
picture.setImageResource();//
返回v;
}
这将帮助您获取要在setImageResource()中使用的相应图像的id
至于高度和宽度,你可以这样设置参数
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.imgLayout2);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 50, 20, 50);
final ImageView imageView = new ImageView(getActivity());
imageView.setLayoutParams(parms);
setImageResource()需要来自Drawable的图像,即int,例如:R.Drawable.sample
您可以使用毕加索库而不是setImageResource。为此
添加以下行以将添加图像加载到imageview中
Picasso.with(context).load(R.drawable.drawableName).into(picture);
将此添加到build.gradle文件中:
compile 'com.squareup.picasso:picasso:2.5.2'
使用此代码:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageUri(Uri.parse("your url here")); // updated code
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
};
}
您好,@Simranjeet Singh图像是来自服务器的列表getImage()中的字符串不是int,如何从中获取?
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageUri(Uri.parse("your url here")); // updated code
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
};
}