2012-05-18 98 views
0

我只需要設置一個高分辨率的圖像,但想知道是否可以將其設置爲xml。我希望能夠設置它,即使它比手機屏幕更大。如何在xml中設置高分辨率圖像?

編輯:我想添加一個巨大的圖像,所以用戶將不得不滾動它。像地圖,但需要添加可點擊區域,我只知道如何在XML中做到這一點。這就是爲什麼我要求使用xml而不是java來添加圖像。但是如果有人能告訴我如何在java中添加可點擊區域。這將做得更好,因爲我已經有了圖像,但使用Java和它能夠滾動。

編輯:我從哪裏得到的代碼來自:Anddev.org我使用完全相同的代碼,只是與其他圖像。

+0

張貼您的相關代碼來滾動圖像。也許,有人會給你的查詢的想法。 – Praveenkumar

+0

@SpK添加了代碼頁。 –

+0

它應該工作,如果我更改此代碼:bmLargeImage = BitmapFactory.decodeResource(getResources(),R.drawable.myImage);到BitmapFactory.decodeResource(getResources(),R.id.myImage);或類似的東西從main.xml中的ImageView中獲取圖像? –

回答

0

好的我發佈這個答案,只是因爲它會更容易張貼這個。 (這不是我想要的,但我得到了一些東西)我想分享這個,所以其他任何知道如何的人都可以幫助我找到解決方案。這是代碼

package com.example.largeimagescroller; 

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.view.Display; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class LargeImageScroller extends Activity { 

// Physical display width and height. 
private static int displayWidth = 0; 
private static int displayHeight = 0; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // displayWidth and displayHeight will change depending on screen 
    // orientation. To get these dynamically, we should hook 
    // onSizeChanged(). 
    // This simple example uses only landscape mode, so it's ok to get them 
    // once on startup and use those values throughout. 
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay(); 
    displayWidth = display.getWidth(); 
    displayHeight = display.getHeight(); 

    // SampleView constructor must be constructed last as it needs the 
    // displayWidth and displayHeight we just got. 
    setContentView(new SampleView(this));  
} 

private static class SampleView extends View { 
    private static Bitmap bmLargeImage; // bitmap large enough to be 
             // scrolled 
    private static Rect displayRect = null; // rect we display to 
    private Rect scrollRect = null; // rect we scroll over our bitmap with 
    private int scrollRectX = 0; // current left location of scroll rect 
    private int scrollRectY = 0; // current top location of scroll rect 
    private float scrollByX = 0; // x amount to scroll by 
    private float scrollByY = 0; // y amount to scroll by 
    private float startX = 0; // track x from one ACTION_MOVE to the next 
    private float startY = 0; // track y from one ACTION_MOVE to the next 

    public SampleView(Context context) { 
     super(context); 

     // Destination rect for our main canvas draw. It never changes. 
     displayRect = new Rect(0, 0, displayWidth, displayHeight); 
     // Scroll rect: this will be used to 'scroll around' over the 
     // bitmap in memory. Initialize as above. 
     scrollRect = new Rect(0, 0, displayWidth, displayHeight); 

     // Load a large bitmap into an offscreen area of memory. 

     bmLargeImage = BitmapFactory.decodeResource(getResources(), 
       R.drawable.alienware); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     //This code define what to do if you touch the x and y coordinates. 
     float x1 = event.getX(); 
     float y1 = event.getY(); 

      switch (event.getActionMasked()){ 
      case MotionEvent.ACTION_DOWN: 
       if (x1>150 & x1<200 & y1>400 & y1<500){ 
        Toast.makeText(getContext(), "Touched the coordinates.", Toast.LENGTH_SHORT).show(); 
       } 
      } 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      // Remember our initial down event location. 
      startX = event.getRawX(); 
      startY = event.getRawY(); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      float x = event.getRawX(); 
      float y = event.getRawY(); 
      // Calculate move update. This will happen many times 
      // during the course of a single movement gesture. 
      scrollByX = x - startX; // move update x increment 
      scrollByY = y - startY; // move update y increment 
      startX = x; // reset initial values to latest 
      startY = y; 
      invalidate(); // force a redraw 
      break; 
     } 
     return true; // done with this event so consume it 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     // Our move updates are calculated in ACTION_MOVE in the opposite 
     // direction 
     // from how we want to move the scroll rect. Think of this as 
     // dragging to 
     // the left being the same as sliding the scroll rect to the right. 
     int newScrollRectX = scrollRectX - (int) scrollByX; 
     int newScrollRectY = scrollRectY - (int) scrollByY; 

     // Don't scroll off the left or right edges of the bitmap. 
     if (newScrollRectX < 0) 
      newScrollRectX = 0; 
     else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth)) 
      newScrollRectX = (bmLargeImage.getWidth() - displayWidth); 

     // Don't scroll off the top or bottom edges of the bitmap. 
     if (newScrollRectY < 0) 
      newScrollRectY = 0; 
     else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight)) 
      newScrollRectY = (bmLargeImage.getHeight() - displayHeight); 

     // We have our updated scroll rect coordinates, set them and draw. 
     scrollRect.set(newScrollRectX, newScrollRectY, newScrollRectX 
       + displayWidth, newScrollRectY + displayHeight); 
     Paint paint = new Paint(); 
     canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint); 

     // Reset current scroll coordinates to reflect the latest updates, 
     // so we can repeat this update process. 
     scrollRectX = newScrollRectX; 
     scrollRectY = newScrollRectY; 
    } 
} 
} 

我記下正在使用的應用程序。 (由於觸摸事件不與CPU模擬器工作,我做到了我的手機上)

http://www.youtube.com/watch?v=NrszZoDenXE&feature=youtube_gdata

正如你可以看到它採取的觸感,但它與滾動矩形移動,所以問題是:我該如何做到這一點,以保持圖像,所以它不會隨着滾動矩形移動?

謝謝

+0

這是在其他問題中提出的,因爲它不存在主要問題。 http://stackoverflow.com/questions/10676545/big-image-with-scroll-and-clickable-areas –

0

你想要什麼?在Android的決議是基於像下面 -

Screen Support

MultipleResolution

只需讀出這一點。希望這兩個鏈接足夠您的查詢。

+0

也許我在帖子中不夠清楚,對此感到遺憾。我在編輯中添加了更多信息 –

0

查看this示例代碼,瞭解如何滾動比屏幕更大的圖像。它還可以進行位圖緩存以加速繪製複雜圖像,並顯示如何響應圖像上任意點的長時間抽頭和雙擊。

+0

我只是無法讓它運行,它崩潰了。 –