2010-08-10 37 views
4

我知道如何顯示比屏幕更大的圖像。這很簡單,詳細解釋here,然而,這種方法使你能夠儘可能地滾動,事件如果你已經離開了圖像,你只是有黑屏。我想知道當我們到達圖片的一側時我們如何使滾動停止...滾動比屏幕更大的圖像,並停止在Android的邊框上

+0

沒有人有關於此的任何提示?我應該直接在Canvas上使用SurfaceView嗎? – Sephy 2010-08-17 14:13:18

回答

5

將您的圖像加載到WebView中。然後,您將獲得您正在查找的所有滾動行爲,甚至是默認的縮放控件,如果您選擇打開它們......實際上是免費的。 (據我所知),您唯一的選擇是使用2D圖形API(Canvas等)並創建您自己的繪圖切片版本(如查看地圖的一部分)。

如果您的圖片是本地的,請參閱from the SD Card中讀取本地圖像數據的示例。在這種情況下,提供本地圖像的更簡潔的方法是創建ContentProvider並通過WebView中的content:// URL訪問資源。

示例使用捆綁的圖像car.jpgassets目錄中的項目:

RES /佈局/ main.xml中

<?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"> 
    <WebView 
     android:id="@+id/web" 
     android:layout_width="150dip" 
     android:layout_height="150dip" 
    /> 
</LinearLayout> 

的src/ImageViewer.java

public class ImageViewer extends Activity { 

    WebView webView; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    webView = (WebView)findViewById(R.id.web); 
    webView.loadUrl("file:///android_asset/car.jpg"); 
    } 
} 

對於與您的項目捆綁在一起的圖像,這是最簡單的方法。這不適用於資源目錄中的圖像(如res/drawable)。該路徑不值得完成它所需的代碼。

+0

感謝您的回答,我會盡力花點時間看看這一切,明天再回來一些反饋。thx guys – Sephy 2010-08-18 22:31:08

+0

好吧,現在第一個問題,我無法在Web視圖中設置此圖像,因爲無論我將它作爲webview的背景並且圖像被調整大小,或者我嘗試從資源加載它, t解決它。 (我認爲這是一個路徑問題......我知道我可以從資產中做到這一點,但我會喜歡它來自res/drawable),並且由於我沒有屏幕的大小,所以我沒有知道怎麼把寬度和高度放在img src – Sephy 2010-08-21 12:25:21

+0

從文件系統或資產中加載捆綁的圖像是通過FAR處理WebView時的最簡單的方法(我用一個例子編輯)。這是因爲Android已經構建了爲AssetManager服務的內容......並且這不適用於其他資源。您必須在自定義ContentProvider的'openFile()'方法中複製行爲'AssetManager.openFd()'。這是更多的代碼。 – Devunwired 2010-08-22 21:09:48

0

在你Activity.onCreate():

requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
setContentView(...); 

我不知道哪個佈局你到底是說,所以我發佈2個版本:

版本1(布頓在圖片下方):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ScrollView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:fadingEdge="none" android:scrollbars="none"> 

     <ImageView android:id="@+id/image" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:scaleType="center" /> 

    </ScrollView> 

    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="OK" 
     android:layout_gravity="center" /> 

</LinearLayout> 

版本2(按鈕在圖像 - 在z軸上):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    <ScrollView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:fadingEdge="none" 
     android:scrollbars="none"> 

     <ImageView android:id="@+id/image" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:scaleType="center" /> 

    </ScrollView> 

    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="OK" 
     android:layout_gravity="bottom|center_horizontal" /> 

</FrameLayout> 

您也可以嘗試將ScrollView放入Horizo​​ntalScrollView中。這裏

0

嗯,我有同樣的問題,是我的解決方案:

Display display = getWindowManager().getDefaultDisplay(); 
    screenSize = new Point(); 
    display.getSize(screenSize); 

得到它會被要求設置底部和右側邊界

image.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      float curX, curY; 

      switch (event.getAction()) { 

       case MotionEvent.ACTION_DOWN: 
        mx = event.getX(); 
        my = event.getY(); 
        rect = image.getDrawable().getBounds(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        curX = event.getX(); 
        curY = event.getY(); 
        image.scrollBy(getX((int) (mx - curX)), getY((int) (my - curY))); 
        mx = curX; 
        my = curY; 
        break; 
      } 
      return true; 
     } 
    }); 

最後兩種方法來設定新的滾動座標屏幕尺寸

private int getX(int x){ 
    if (image.getScrollX() + x < 0){ //left side 
     return 0; 
    } else if (image.getScrollX() + x >= (rect.right - screenSize.x)){ //right side 
     return 0; 
    } else { 
     return x; 
    } 
} 

private int getY(int y){ 
    if (image.getScrollY() + y < 0){ //top side 
     return 0; 
    } else if (image.getScrollY() + y >= (rect.bottom - screenSize.y)){ //bottom side 
     return 0; 
    } else { 
     return y; 
    } 
} 

很好的API 16+,希望能幫到

相關問題