Android: Drawing Bitmap in Specific Location, Refusal to Draw

前端 未结 1 891
天命终不由人
天命终不由人 2021-01-13 18:22

Let me start off with I\'m mostly new to writing Java, so I would love for a complete explanation. Not just a spew of code, but something that gives some in site to

1条回答
  •  终归单人心
    2021-01-13 19:04

    Writing nonsense that solves a compiler error doesn't change the fact that it's nonsense! :)

    You have to get a reference to a Canvas object another way. You might want to be more specific about what you are exactly trying to do, so that we can suggest how you should go about it. (Ex. are you attempting to just display an image along with some other Views? Are you trying to create a custom View? You might just want to consider using an ImageView)

    Edit:

    You should read up about Android architecture at developer.android.com . If you are simply trying to show an image, there might not be a reason to use the canvas directly. Nonetheless, you can draw within a custom View by extending the View class

    class myView extends View{
    
        Bitmap bm;
    
        loadBitmap()
        {
            bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        }
    
        @Override
        public void draw(Canvas c)
        {
           c.drawBitmap(bm, XCORD, YCORD, null);
        }
    }
    

    if you don't need a custom view, just use the ImageView class

    class MyActivty extends Activity{
       @Override
       public void onCreate(Bundle b)
       {
           super.onCreate(b);
           ImageView iv = new ImageView(this);
           iv.setImageResource(R.drawable.pic);
           setContentView(iv);
       }
    }
    

    Warning: I wrote those method calls off of the top of my head, they might be slightly off.

    0 讨论(0)
提交回复
热议问题