본문 바로가기

Android

[펌][Android][AndEngine] 인터넷상의 이미지 로딩하기 (Loading texture from internet source?)

배경이 검은 색상이고 작성자까지 옮기기 어렵다보니 핵심 내용만 대충 옮겨 놓으니

세부 내용은 원문을 참고하세요.

 

출처 : http://www.andengine.org/forums/development/loading-texture-from-internet-source-t2431.html

 

Loading texture from internet source?

In the game that I am creating, the client wants to be able to sell ad space inside the game. This isn't the typical ad bar that shows up on some games (angry birds) but instead a single image that gets replaced with the logo of whatever company inside the game itself. is this possible? does andEngine have any way of downloading images from a url and using them as a texture?

 

Thanks in advance for any help

 

 

Re: Loading texture from internet source?

You could just download the image and then turn it into a texture.

 

I figured this out. 

First you need a texture source. So make a new class that implements ITextureSource:

  1. public class BitmapTextureSource implements ITextureSource {
  2.  
  3.         private int mHeight;
  4.         private int mWidth;
  5.        
  6.         private Bitmap mBitmap;
  7.        
  8.         public BitmapTextureSource()
  9.         {
  10.                 mBitmap = ImageFactory.getRandomImage();
  11.                 if(mBitmap!= null)
  12.                 {
  13.                         mHeight = mBitmap.getHeight();
  14.                         mWidth = mBitmap.getWidth();
  15.                 }
  16.                 else
  17.                 {
  18.                         mHeight = 0;
  19.                         mWidth = 0;
  20.                 }
  21.                
  22.         }
  23.        
  24.         @Override
  25.         public BitmapTextureSource clone()
  26.         {
  27.                 return new BitmapTextureSource();
  28.         }
  29.        
  30.         @Override
  31.         public int getHeight() {
  32.                 // TODO Auto-generated method stub
  33.                 return mHeight;
  34.         }
  35.  
  36.         @Override
  37.         public int getWidth() {
  38.                 // TODO Auto-generated method stub
  39.                 return mWidth;
  40.         }
  41.  
  42.         @Override
  43.         public Bitmap onLoadBitmap() {
  44.                 // TODO Auto-generated method stub
  45.                 return mBitmap;
  46.         }
  47.  
  48. }

 

The ImageFactory just returns an image from the internet:

  1. public class ImageFactory
  2. {
  3.          
  4.          public static Bitmap getRandomImage()
  5.          {
  6.                  //take into account preferences
  7.                         Bitmap img = null;
  8.                 URL imgUrl = null;
  9.                
  10.                 try {
  11.                        
  12.                                 imgUrl= new URL("image url goes here");
  13.                         } catch (MalformedURLException e) {
  14.                                 e.printStackTrace();
  15.                        
  16.                         }
  17.                
  18.                         try {
  19.                                
  20.                                 HttpURLConnection conn= (HttpURLConnection)imgUrl.openConnection();
  21.                                 conn.setDoInput(true);
  22.                                 conn.connect();
  23.                                
  24.                                 InputStream is = conn.getInputStream();
  25.  
  26.                                 img = BitmapFactory.decodeStream(is);
  27.                                
  28.                                 } catch (IOException e) {
  29.                                         e.printStackTrace();
  30.                                 }
  31.                        
  32.                 return img;
  33.          }     
  34. }

Now in the OnLoadScene method in your game:

  1.   mBackground = new Texture(512, 1024, TextureOptions.BILINEAR);
  2.                 mEngine.getTextureManager().loadTexture(mBackground);
  3.                
  4.                 mBackgroundReg = TextureRegionFactory.createFromSource(mBackground,
  5.                                                                                                                            new BitmapTextureSource(),
  6.                                                                                                                            0,
  7.                                                                                                                            0);
  8.                 mBackgroundSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, mBackgroundReg);
  9.                 mScene.getLayer(0).addEntity(mBackgroundSprite);
  10.  

As you can tell I use the picture from the internet for the background of my game. You can probably set up a small area where your ad shows up.

 

Have fun.

 

You need this permission in your manifest:

  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

You also need to bear-in-mind that most devices have patchy or even non-existant connectivity much of the time they're in-use.

 

That means you need to be doing this stuff completely transparently/asynchronously and have a way of handling the (frequent) likelihood you'll get nothing back.

 

Last thing you want is your game pausing for huge periods of time whilst it tries to get something over the lamentable heap of shit which is most people's mobile data connections!!

Tasty Android Snacks - http://www.somewhatdog.com