Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yahoo! Finally ! Works ! (BUT !!!! can cause memory leak with big images. It's fixed in the post below)</p> <p>This code takes the tile picture (tile1), repeats it(TileMode.REPEAT), makes shifted animation (10 fragments), adds this in Animation set.</p> <p><img src="https://i.stack.imgur.com/v9dLq.png" alt="enter image description here"></p> <pre><code>private void initAnimation() { // R.drawable.tile1 is PNG Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.tile1); AnimationDrawable shiftedAnimation = getAnimation(b); // R.id.img_3 is ImageView in my application View v = findViewById(R.id.img_3); v.setBackground(shiftedAnimation); shiftedAnimation.start(); } private Bitmap getShiftedBitmap(Bitmap bitmap, int shiftX) { Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Canvas newBitmapCanvas = new Canvas(newBitmap); Rect srcRect1 = new Rect(shiftX, 0, bitmap.getWidth(), bitmap.getHeight()); Rect destRect1 = new Rect(srcRect1); destRect1.offset(-shiftX, 0); newBitmapCanvas.drawBitmap(bitmap, srcRect1, destRect1, null); Rect srcRect2 = new Rect(0, 0, shiftX, bitmap.getHeight()); Rect destRect2 = new Rect(srcRect2); destRect2.offset(bitmap.getWidth() - shiftX, 0); newBitmapCanvas.drawBitmap(bitmap, srcRect2, destRect2, null); return newBitmap; } private List&lt;Bitmap&gt; getShiftedBitmaps(Bitmap bitmap) { List&lt;Bitmap&gt; shiftedBitmaps = new ArrayList&lt;Bitmap&gt;(); int fragments = 10; int shiftLength = bitmap.getWidth() / fragments; for(int i = 0 ; i &lt; fragments; ++i){ shiftedBitmaps.add( getShiftedBitmap(bitmap,shiftLength * i)); } return shiftedBitmaps; } private AnimationDrawable getAnimation(Bitmap bitmap) { AnimationDrawable animation = new AnimationDrawable(); animation.setOneShot(false); List&lt;Bitmap&gt; shiftedBitmaps = getShiftedBitmaps(bitmap); int duration = 50; for(Bitmap image: shiftedBitmaps){ BitmapDrawable navigationBackground = new BitmapDrawable(getResources(), image); navigationBackground.setTileModeX(TileMode.REPEAT); animation.addFrame(navigationBackground, duration); } return animation; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload