1

我想从字符串 URL 设置视图的背景。我使用此代码从可绘制对象中设置图像:

示例网址

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;
            }
        };
4

6 回答 6

2

在异步任务中设置视图的背景,因为您从 url 获取图像。试试看,希望对你有帮助。

于 2013-07-05T05:50:43.730 回答
1

尝试这个..URL url = new URL(Your Url); Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); imageView.setImageBitmap(bmp);

于 2013-07-05T05:51:40.840 回答
1

你可以试试这个

Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
BitmapDrawable background = new BitmapDrawable(getResources(), bitmap);
rightv.setBackgroundDrawable(background);
于 2013-07-05T05:52:02.423 回答
0

使用此代码可以帮助您...

URL url = new URL(Your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); imageView.setImageBitmap(bmp);

或参考这些链接点击这里

于 2013-07-05T05:52:02.590 回答
0

我正在使用一个类将图像从 URL 加载到 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);

最后将此库添加到您的项目中:链接

于 2015-01-17T15:55:45.040 回答
0

要将图像设置为墙纸,您可以使用此..

wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
StrictMode.setThreadPolicy(policy);

// Button Onclick

 setWallpaper.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            InputStream ins = null;
            try {
                  ins = new URL(url).openStream();
                  wallpaperManager.setStream(ins);
            } catch (IOException e) {
                  e.printStackTrace();
            }
            FancyToast.makeText(getApplicationContext(), "Set Successfully", 
            FancyToast.LENGTH_SHORT, FancyToast.SUCCESS, false).show();
        }
     });

请在 Set_Wallpaper 和 Internet 的 AndroidManifest.xml 中添加权限。

于 2021-09-06T06:23:13.533 回答