1

同封のサンプルでChris BanesのPhotoViewライブラリを使用しようとしています。sample として drawable からではなく、(インターネット上の) URL から画像をロードするように sample にいくつかの変更を加えました。コードは次のとおりです。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    mImageView = (ImageView) findViewById(R.id.iv_photo);
    mCurrMatrixTv = (TextView) findViewById(R.id.tv_current_matrix);


    //here's the method to load URL image from URL
    new LoadImage().execute();
    mAttacher = new PhotoViewAttacher(mImageView);

}

private class LoadImage extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // Simulates a background job.

        try {
            mImageView.setImageDrawable(grabImageFromUrl(image_url));               
        } catch (Exception e) {
            e.getStackTrace().toString();
        }                   
        return null;
    }

}

private Drawable grabImageFromUrl(String url) throws Exception {
        return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
        }

問題は、画像が読み込まれず、空白のページが返されることでした。そして、ピンチズーム操作をいくつか試してみると、奇妙なことが起こり、画像が読み込まれ、正常に機能しました。誰にも提案がありますか?ありがとう。

4

2 に答える 2

0

最初に画像をビットマップにロードしてから、ビットマップをimageviewに設定してみてください。

ビットマップへのURL:

public static Bitmap getBitmapFromURL(String src) { try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap; } catch (IOException e) { e.printStackTrace(); return null; } }

次に、iv.setimagebitmap()を使用します

于 2013-01-06T10:09:49.280 に答える
-1
    private Bitmap bmp;
bmp = new Bitmap[1];

// to fetch the image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = calculateInSampleSize(options, screenWidth, screenHeight);
options.inJustDecodeBounds = false;
final Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url, new Rect(), options);


// to set the image
 Runnable action = new Runnable() {
   public void run() { bmp = bitmap
     }
  };
 runOnUiThread(action);

これで bmp に画像ができました。それを持って、ギャラリーのアダプターにセットします。

ImageView imageView = new ImageView(container.getContext());
PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
imageView.setImageBitmap(bmp);
attacher.update();
于 2013-01-08T12:38:08.417 に答える