如何将外部图像加载到Android吐司?

时间:2014-12-20 23:53:40

标签: java android toast android-toast

如何将外部图像(从网址)加载到Toast中?目前我有这个代码工作,显示一个简单的文本:

cordova.getActivity().runOnUiThread(new Runnable() {
    public void run() {
      android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), 'Hello, i'm a toast!', 0);
      toast.setDuration(android.widget.Toast.LENGTH_LONG);
      toast.show();
      callbackContext.success();
    }
});

我想要的是在左边放一个小图标,它是从网址下载的,所以我认为它应该从背景中的异步任务加载..

2 个答案:

答案 0 :(得分:1)

<强> custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/toast_layout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#000"
 android:orientation="horizontal"
 android:padding="5dp" >
 <ImageView
 android:id="@+id/image"
 android:src="@drawable/toastimg"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_marginRight="5dp" />
 <TextView
android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:textColor="#FFF"/>
 </LinearLayout>

<强> Activity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
ImageView imageView = (ImageView) layout.findViewById(R.id.image);
Picasso.with(context)
.load(url)
.into(imageView);
text.setText("Custom Toast with Twitter Icon"); //Message shown in Custom Toast
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();

答案 1 :(得分:0)

只需使用ImageView创建自定义布局并将其添加到自定义Toast:

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(yourCustomView); // <- you custom View
toast.show();

看看这个:Custom toast in android : a simple example

编辑:

好吧试试这个(警告丑陋的伪代码!!):

 public void yourMethod(){
     new Thread(new Runnable(){
         public void run(){

             final Bitmap bmap = getBitmapFromURL("your URL");     

             runOnUiThread(new Runnable() {
                 public void run() {
                     ImageView img = new ImageView();
                     img.setImageBitmap(bmap );

                     Toast toast = new Toast(getApplicationContext());
                     toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                     toast.setDuration(Toast.LENGTH_SHORT);
                     toast.setView(img); // <- you custom View
                     toast.show();
                 }
             });
         }
     }).start();
 }


 public Bitmap getBitmapFromURL(String link) {

    try {
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bmap = BitmapFactory.decodeStream(input);

        return bmap ;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}