我想从字符串URL设置视图的背景。我使用此代码从drawable设置图像:
View rightv = row.findViewById(R.id.four);
rightv.setBackgroundResource(R.drawable.demoimage);
rightv.setTag(position);
rightv.setOnClickListener(MembersDealActivity_a.this);
但是当我尝试从URL中执行相同操作时,它无法正常工作。我也尝试过这种代码,但它也不起作用:
Drawable res = getResources().getDrawable(imageResource);
right.setBackgroundDrawable(res);
right.setTag(position);
right.setOnClickListener(MembersDealActivity_a.this);
我也尝试过使用Picasso库,但是当它请求将值设置到参数视图中时,它无法在那里进行转换。
Picasso.with(this)
.load(imag_link).resize(600, 350)
.into(imv);
完整代码:
public class MyActivity extends Activity implements
AdapterView.OnItemClickListener, View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
setContentView(list);
String[] items = { "Tom", "Sally", "Bill", "John", "Santiago",
"Isabella" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.review, R.id.textView1, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
View text = row.findViewById(R.id.seemore);
text.setTag(position);
text.setOnClickListener(MyActivity.this);
View left = row.findViewById(R.id.imageView1);
left.setBackgroundResource(R.drawable.newapture);
left.setTag(position);
left.setOnClickListener(MyActivity.this);
return row;
}
};
答案 0 :(得分:1)
在Async任务中设置视图的背景,因为您从url获取图像。 试试吧我希望它会对你有所帮助。
答案 1 :(得分:1)
试试这个.. URL url = new URL(Your Url);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
答案 2 :(得分:1)
你可以尝试这个
Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
BitmapDrawable background = new BitmapDrawable(getResources(), bitmap);
rightv.setBackgroundDrawable(background);
答案 3 :(得分:0)
使用此代码可以帮助您...
URL url = new URL(Your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
或参考这些链接Click Here
答案 4 :(得分:0)
我正在使用一个类从URL加载Image到ImageView:
下面: 1.创建一个ImageFromUrlUtil.java类
package com.ntjp.halalnavi.guide.util;
import android.graphics.Bitmap.Config;
import android.widget.ImageView;
import com.ntjp.halalnavi.guide.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
public class ImageFromUrlUtil {
public static void displayImage(ImageView view, String path, ImageLoadingListener listener) {
ImageLoader loader = ImageLoader.getInstance();
try {
loader.displayImage(path, view, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
} catch (OutOfMemoryError e) {
e.printStackTrace();
loader.clearMemoryCache();
}
}
public static void displayRoundImage(ImageView view, String path, ImageLoadingListener listener) {
ImageLoader loader = ImageLoader.getInstance();
try {
loader.displayImage(path, view, ROUND_DISPLAY_IMAGE_OPTIONS, listener);
} catch (OutOfMemoryError e) {
e.printStackTrace();
loader.clearMemoryCache();
}
}
public static void loadImage(String path, ImageLoadingListener listener) {
ImageLoader loader = ImageLoader.getInstance();
try {
loader.loadImage(path, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
//TODO Change default image
private static final DisplayImageOptions.Builder DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.displayer(new FadeInBitmapDisplayer(300, true, false, false))
.showImageForEmptyUri(R.drawable.default_image)
.showImageOnLoading(R.drawable.default_image)
.showImageOnFail(R.drawable.default_image).cacheOnDisk(true)
.cacheInMemory(true).bitmapConfig(Config.ARGB_8888);
private static final DisplayImageOptions DEFAULT_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
.build();
private static final DisplayImageOptions ROUND_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
.displayer(new RoundedBitmapDisplayer(500)).build();
}
然后从任何你想要的地方调用此方法:
对于圆形图像我们:
ImageUtil.displayRoundImage(avt, avtUrl, null);
对于正常的图像我们:
ImageUtil.displayImage(thumb, avtUrl, null);
最终将此库添加到您的项目中:Link