2

I have created a class which extends View class.

public class SplashScreen extends View

and i use it by setting contentview

View splash = new SplashScreen(this);
setContentView(splash);

I need to set background image but I can't use layout. I think I need to do canvas drawing but I don't know how to do.

protected void onDraw(Canvas canvas) {
  ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
  paint.setColor(Color.LTGRAY);
  // canvas.drawImage(R.drawable.background_image); (Ps: I know there is no function such as drawImage)"
  canvas.drawOval(ballBounds, paint);}
Reno
  • 33,594
  • 11
  • 89
  • 102
Srht
  • 473
  • 3
  • 7
  • 17

3 Answers3

5

If you want to just set the background you can do

public SplashScreen(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundResource(R.drawable.background);
}
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
2

you can add image on canvas as:

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  
        Matrix matrix=new Matrix();
        matrix.postScale(0.8f, 0.8f);
        matrix.postRotate(45);
        Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
        bmp.getHeight(),matrix,true);
        canvas.drawColor(Color.BLACK); 
        canvas.drawBitmap(dstbmp, 10, 10, null); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • thanks that was what I'm looking for but it slows the app like hell! I need to figure out new way. – Srht Jun 20 '12 at 09:04
  • Thanks a lot, this is exactly what I was looking for, and it works like a charm. @Srht, you should load the image once, especially if you have this code in OnDraw() – Has AlTaiar Mar 15 '14 at 01:51
0

Don't you just want to change SplachScreen type to activity? Then setContentView change to whatever your layout is and if you want that splach screen appear before your application, make it first in Manifest and in splashscreen end, destroy activity and start your application menu activity. Then you won't need View class and less work finding where is problem

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
  • Do I make canvas drawings without using view class? (Maybe a silly question but I really don't know) – Srht Jun 20 '12 at 09:05