2011-08-29 32 views
0

基本上,下面的代碼是我使用檢索從遠程服務器鏡像的。我的問題是,每當我有像700x700這樣的大圖像時,它會縮小整個圖像,直到它適合屏幕。我真正想要的是每當我有這樣的大圖像時,我希望它保持原樣,並顯示一個垂直和水平滾動條。這是可能實現的嗎?如何在Android中啓用水平滾動?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 
<ProgressBar  
    android:id="@+id/progressBar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<ImageView 
    android:id="@+id/imageView" 
    android:visibility="gone" 
    android:layout_width="200dp" 
    android:layout_height="200dp"/> 

</LinearLayout> 

然後我的主要活動ImageDownload.java

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 

public class ImageDownload extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView mainImageView = (ImageView) findViewById(R.id.imageView); 
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); 
    String imageurl = "http://4.bp.blogspot.com/-xRyw7I08tDM/TgNqD2Ij-3I/AAAAAAAAAOU/FGjboxE-u2U/s1600/cute_cat_cute_1-s357x422-47520.jpg"; 

    ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView); 
    ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl); 
    imageDownlaodThread.start(); 

} 

class ImageDownlaodThread extends Thread { 
    ImageDownloadMessageHandler imageDownloadMessageHandler; 
    String imageUrl; 

    public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) { 
    this.imageDownloadMessageHandler = imageDownloadMessageHandler; 
    this.imageUrl = imageUrl; 
    } 

    @Override 
    public void run() { 
    Drawable drawable = LoadImageFromWebOperations(imageUrl); 
    Message message = imageDownloadMessageHandler.obtainMessage(1, drawable); 
    imageDownloadMessageHandler.sendMessage(message); 
    System.out.println("Message sent"); 
    } 

} 

class ImageDownloadMessageHandler extends Handler { 
    ProgressBar progressBar; 
    View imageTextView; 

    public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) { 
    this.progressBar = progressBar; 
    this.imageTextView = imageTextView; 
    } 

    @Override 
    public void handleMessage(Message message) { 
    progressBar.setVisibility(View.GONE); 
    imageTextView.setBackgroundDrawable(((Drawable) message.obj)); 
    imageTextView.setVisibility(View.VISIBLE); 
    } 

} 

Drawable LoadImageFromWebOperations(String url) { 
    Drawable d = null; 
    InputStream is = null; 
    try { 
    is = (InputStream) new URL(url).getContent(); 
    d = Drawable.createFromStream(is, "src name"); 
    } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    return d; 
} 

} 

回答

2

試試這個:

<ScrollView android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical"> 
    <HorizontalScrollView android:id="@+id/horizontalScrollView1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayout2" 
      android:layout_width="match_parent" android:layout_height="match_parent" 
      android:orientation="horizontal"> 
      <TextView/> 
      <ImageView/> 
      <ProgressBar/> 
     </LinearLayout> 
     </HorizontalScrollView> 
     </LinearLayout> 
</ScrollView> 

這種方式,您可以有兩個垂直和水平滾動。

+0

它的偉大工程,謝謝! – user858975

+0

我一直在尋找如何爲絕對年齡做到這一點,這樣我就可以得到圖像的水平滾動畫廊,也處於垂直滾動活動。非常感謝!它可以創造奇蹟...... – edwoollard

0

佈局元件ScrollViewHorizontalScrollView已經發明用於這一目的。

+0

但它所做的只是讓您使用垂直滾動條而不是水平滾動條。 – user858975

+0

如果使用Horizo​​ntalScroolView,那麼你就不能使用垂直。不要只發佈一個隨機鏈接! – user858975