Setting background image in a custom view

杀马特。学长 韩版系。学妹 提交于 2020-01-05 04:37:06

问题


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);}

回答1:


If you want to just set the background you can do

public SplashScreen(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundResource(R.drawable.background);
}



回答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); 



回答3:


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



来源:https://stackoverflow.com/questions/11115820/setting-background-image-in-a-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!