2009-08-21 20 views
45

मैं एंड्रॉइड में बिटमैप लोड करने की कोशिश कर रहा हूं जिसे मैं टाइल करना चाहता हूं। मैं वर्तमान में एक बिटमैप प्रदर्शित करने के लिए मेरे विचार में निम्नलिखित का उपयोग कर रहा:एंड्रॉइड टाइल बिटमैप

canvas.drawBitmap(bitmap, srcRect, destRect, null) 

मैं अनिवार्य रूप से मेरे एप्लिकेशन में एक पृष्ठभूमि छवि के रूप में इस बिटमैप उपयोग करना चाहते हैं और दोनों एक्स और वाई दिशाओं में बिटमैप को दोहराना चाहते हैं ।

मैंने BitmapShader कक्षा के लिए TileMode.REPEAT निरंतर देखा है, लेकिन मुझे यकीन नहीं है कि इसका उपयोग वास्तविक बिटमैप को दोहराने के लिए किया जाता है या बिटमैप पर फ़िल्टर लगाने के लिए उपयोग किया जाता है।

+0

किसी को भी जावा कोड में xml के उपयोग के बिना यह कैसे करना है? – endryha

उत्तर

123

आप इसे जावा कोड के बजाय xml में करेंगे। मैंने इसे स्वयं नहीं किया है लेकिन मुझे यह उदाहरण मिला।

<xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/MainLayout" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="@drawable/backrepeat" 
> 
एक xml में

तो backrepeat.xml

<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/back" 
    android:tileMode="repeat" /> 

reference

+2

बस जोड़ने के लिए, backrepeat.xml "ड्रॉइंग" फ़ोल्डर में जाता है –

+2

यह कैनवास पर ड्राइंग के लिए काम नहीं करेगा हालांकि ... – stealthcopter

+0

यहां पैडेंटिक हो सकता है लेकिन पहली पंक्ति को पढ़ना चाहिए - Neil

15

backrepeat.xml ऊपर बुलाया गाड़ी है

<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/tile" 
    android:tileMode="repeat" 
    android:dither="true" /> 
+17

यह छोटी गाड़ी क्यों है और यह कैसे ठीक करता है? –

+0

यह एक जोड़ता है: एंड्रॉइड: dither = "true", जो टाइल काम शुरू करने के लिए अच्छी तरह से काम नहीं करता है, जो कुछ भी नहीं करता है। –

34

पता लगा कोड संस्करण:

BitmapDrawable TileMe = new BitmapDrawable(MyBitmap); 
    TileMe.setTileModeX(Shader.TileMode.REPEAT); 
    TileMe.setTileModeY(Shader.TileMode.REPEAT); 

    ImageView Item = new ImageView(this); 
    Item.setBackgroundDrawable(TileMe); 

तो अगर आप टाइल करने के लिए एक drawable है, यह बजाय इस्तेमाल किया जा सकता BitmapDrawable बनाने के लिए:

BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.tile)); 
+0

यह बहुत अच्छा काम करता है, लेकिन आप इसे केवल लंबवत कैसे दोहराएंगे? मैंने setTileModeX को हटाने का प्रयास किया, लेकिन फिर यह बिल्कुल भी काम नहीं करता है। –

+0

@ विलियम एल। अजीब। [डॉक्स] (http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html#setTileModeX (android.graphics.Shader.TileMode)) ऐसा लगता है कि इसे केवल उनमें से एक के साथ काम करना चाहिए । सबसे अच्छा अनुमान है कि ['Shader.TileMode.CLAMP'] (http://developer.android.com/reference/android/graphics/Shader.TileMode.html) आज़माएं और देखें कि क्या होता है। – Izkata

+0

यह काम करता है अगर मैं इसे क्षैतिज रूप से दोहराता हूं, लेकिन केवल लंबवत नहीं। और सीएलएएमपी वही काम करता है जैसे टाइल मोड को पहले स्थान पर सेट नहीं किया गया है। –

5

ऐसा लगता है कि कुछ लोगों को एक दृश्य में ऐसा करने में रुचि रखते हैं, OnDraw विधि पर। निम्नलिखित कोड मेरे लिए काम किया है:

bgTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_tile); 

float left = 0, top = 0; 
float bgTileWidth = bgTile.getWidth(); 
float bgTileHeight = bgTile.getHeight(); 

while (left < screenWidth) { 
    while (top < screenHeight) { 
     canvas.drawBitmap(bgTile, left, top, null); 
     top += bgTileHeight; 
    } 
    left += bgTileWidth; 
    top = 0; 
} 
+0

यह सभी क्षेत्र भर जाएगा। अंतिम टाइल पूर्ण नहीं हो सकता है, स्क्रीन के दाएं/नीचे की अधिकांश जगह अभी भी खींची जाएगी। – kgiannakakis

+0

मैं केवल इसके बजाय मेरे कैनवास सेगमेंट के लिए इसका उपयोग कर रहा था, इसलिए जाहिर है कि आप अतिरिक्त खींचे जाते हैं तो चौड़ाई और ऊंचाई छवियों द्वारा विभाजित नहीं होती है। मैंने अपनी टिप्पणी हटा दी क्योंकि यह प्रश्न के लिए प्रासंगिक नहीं है। माफ़ कीजिये! – stealthcopter

+0

इस मामले में आप BitmapDrawable.setBounds और BitmapDrawable.draw (कैनवास) – tactoth

4
<xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/MainLayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/back" 
    android:tileMode="repeat" 
> 

यह मेरे लिए ठीक काम किया। मुझे बिटमैप को अलग से बनाने की ज़रूरत नहीं थी। मैंने लेआउट में टाइलमोड विशेषता का उपयोग किया।

+0

का उपयोग करेंगे, धन्यवाद, मुझे यह सबसे आसान तरीका माना गया। – patrickandroid

0

यदि आप पृष्ठभूमि को केवल लंबवत रूप से दोहराना चाहते हैं तो आप अपने लेआउट की चौड़ाई को "wrap_content" पर सेट कर सकते हैं, जबकि यदि आप क्षैतिज रूप से ऊंचाई को दोहराने के लिए पृष्ठभूमि सेट करना चाहते हैं तो "wrap_content" पर सेट करें। यदि दोनों ऊंचाई और चौड़ाई "fill_parent" पर सेट हैं तो यह एक्स और वाई दिशाओं में टाइल करेगा।

उदाहरण के लिए, निम्न कोड खड़ी अपनी पृष्ठभूमि दोहराया जाएगा:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:background="@drawable/news_detail_activity_bg"> 
</LinearLayout> 
1

बस (onCreate में कोड की इस पंक्ति डाल): -

final Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.actionbar_bg); 
final BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); 
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 
final ActionBar bar = getSupportActionBar(); 
bar.setBackgroundDrawable(bitmapDrawable); 
0
/* Tiled Layer Bitmap*/ 


public class TiledLayer { 
private int cellWidth; 
private int cellHeight; 
private int yPosition = 0, xPosition = 0; 
private int[][] grid; 
private Image image; 
private int[] tileXPositions; 
private int[] tileYPositions; 
private ArrayList animatedTiles; 
private int numberOfTiles; 
private int numberOfColumns; 
private int numberOfRows; 
private int gridColumns; 
private int gridRows, width, height; 

public TiledLayer(Image image, int columns, int rows, int tileWidth, 
     int tileHeight, int width, int height) { 
    this.grid = new int[columns][rows]; 
    this.gridColumns = columns; 
    this.gridRows = rows; 
    this.width = columns * tileWidth; 
    this.height = rows * tileHeight; 
    this.animatedTiles = new ArrayList(); 
    setStaticTileSet(image, tileWidth, tileHeight); 
    } 

public void setStaticTileSet(Image image, int tileWidth, int tileHeight) { 
    this.image = image; 
    this.cellWidth = tileWidth; 
    this.cellHeight = tileHeight; 
    int columns = 64;//image.getWidth()/tileWidth; 
    int rows =40;// image.getHeight()/tileHeight; 
    this.tileXPositions = new int[columns]; 
    int pos = 0; 
    for (int i = 0; i < columns; i++) { 
     this.tileXPositions[i] = pos; 
     pos += tileWidth; 
    } 
    this.tileYPositions = new int[rows]; 
    pos = 0; 
    for (int i = 0; i < rows; i++) { 
     this.tileYPositions[i] = pos; 
     pos += tileHeight; 
    } 

    if (columns * rows < this.numberOfTiles) { 
     // clear the grid, when there are not as many tiles as in the 
     // previous set: 
     for (int i = 0; i < this.grid.length; i++) { 
      for (int j = 0; j < this.grid[i].length; j++) { 
       this.grid[i][j] = 0; 
      } 
     } 
    } 
    this.numberOfTiles = columns * rows; 
    this.numberOfColumns = columns; 
    this.numberOfRows = rows; 
    } 

    public int createAnimatedTile(int staticTileIndex) { 
    if (staticTileIndex >= this.numberOfTiles) { 

     throw new IllegalArgumentException("invalid static tile index: " 
       + staticTileIndex + " (there are only [" 
       + this.numberOfTiles + "] tiles available."); 

    } 
    this.animatedTiles.add(new Integer(staticTileIndex)); 
    return -1 * (this.animatedTiles.size() - 1); 
} 

public void setAnimatedTile(int animatedTileIndex, int staticTileIndex) { 
    if (staticTileIndex >= this.numberOfTiles) { 

    } 
    int animatedIndex = (-1 * animatedTileIndex) - 1; 
    this.animatedTiles.set(animatedIndex, new Integer(staticTileIndex)); 
    } 

public int getAnimatedTile(int animatedTileIndex) { 
    int animatedIndex = (-1 * animatedTileIndex) - 1; 
    Integer animatedTile = (Integer) this.animatedTiles.get(animatedIndex); 
    return animatedTile.intValue(); 
} 
public void setCell(int col, int row, int tileIndex) { 
    if (tileIndex >= this.numberOfTiles) { 

     throw new IllegalArgumentException("invalid static tile index: " 
       + tileIndex + " (there are only [" + this.numberOfTiles 
       + "] tiles available."); 

    } 
    this.grid[col][row] = tileIndex; 
} 

public int getCell(int col, int row) { 
    return this.grid[col][row]; 
} 

public void fillCells(int col, int row, int numCols, int numRows, 
     int tileIndex) { 
    if (tileIndex >= this.numberOfTiles) { 

     throw new IllegalArgumentException("invalid static tile index: " 
       + tileIndex + " (there are only [" + this.numberOfTiles 
       + "] tiles available."); 

    } 
    int endCols = col + numCols; 
    int endRows = row + numRows; 
    for (int i = col; i < endCols; i++) { 
     for (int j = row; j < endRows; j++) { 
      this.grid[i][j] = tileIndex; 
     } 
    } 
} 

    public final int getCellWidth() { 
    return this.cellWidth; 
} 

    public final int getCellHeight() { 
    return this.cellHeight; 
} 

public final int getColumns() { 
    return this.gridColumns; 
} 

public final int getRows() { 
    return this.gridRows; 
} 

public final void paint(Graphics g) { 
    int clipX = 0;// g.getClipX(); 
    int clipY = 0;// g.getClipY(); 
    int clipWidth = width;// g.getClipWidth(); 
    int clipHeight = height;// g.getClipHeight(); 

    // jmt restore clip to previous state 

    int x = this.xPosition; 
    int y = this.yPosition; 
    int[][] gridTable = this.grid; 
    for (int i = 0; i < this.gridColumns; i++) { 
     int[] gridRow = gridTable[i]; 
     for (int j = 0; j < gridRow.length; j++) { 
      int cellIndex = gridRow[j]; 
      if (cellIndex != 0) { 
       // okay this cell needs to be rendered: 
       int tileIndex; 
       if (cellIndex < 0) { 
        Integer tile = (Integer) this.animatedTiles 
          .get((-1 * cellIndex) - 1); 
        tileIndex = tile.intValue() - 1; 
       } else { 
        tileIndex = cellIndex - 1; 
       } 
       // now draw the tile: 
       g.save(Canvas.CLIP_SAVE_FLAG); 
       // jmt: clear the screen 
       Rect r = new Rect(0, 0, cellWidth, cellHeight); 
       g.clipRect(r); 
       g.setClip(x, y, this.cellWidth, this.cellHeight); 
       int column = tileIndex % this.numberOfColumns; 
       int row = tileIndex/this.numberOfColumns; 
       int tileX = x - this.tileXPositions[column]; 
       int tileY = y - this.tileYPositions[row]; 

       g.drawImage(this.image, tileX, tileY); 
       g.restore(); 
      } 
      y += this.cellHeight; 
     } // for each row 
     y = this.yPosition; 
     x += this.cellWidth; 
    } // for each column 

    // reset original clip: 
    g.setClip(clipX, clipY, clipWidth, clipHeight); 
} 

}

संबंधित मुद्दे