如何绘制一个位图在画布的右上角 [英] how to draw a Bitmap on the Top Right of the Canvas
问题描述
我试图汲取右上角
的位图的画布
到目前为止,我已经做了以下内容:
So far I have done the following:
//100x40 dimensions for the bitmap
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.backbutton);
Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Rect bitmapRect = new Rect(0, 0, canvasWidth -200,50);
canvas.drawBitmap(bitmap, source, bitmapRect, paint);
问题是,当我运行应用程序,位图不会出现在屏幕上。
全code:
The problem is that when I run the app, the bitmap doesn't appear on the screen. Full code:
public class MyView extends View {
Rect bitmapRect;
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); //To change body of overridden methods use File | Settings | File Templates.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.backbutton);
Rect source = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight());
bitmapRect = new Rect(0,0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, source, bitmapRect, new Paint());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if(null != bitmapRect && bitmapRect.contains(x,y)){
Toast.makeText(view.getContext(), "this works", Toast.LENGTH_LONG).show();
}
return super.onTouchEvent(event); //To change body of overridden methods use File | Settings | File Templates.
}
有人可以帮助我在这里吗?
Can someone help me here please?
推荐答案
在右侧MyView的矩形下bitma $ P $厘;使变量
in MyView right under Rect bitmapRect; make the variables
public int width;
public int height;
然后在您的MyView的类把这个方法有
then in your MyView class put this method in there
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh)
{
width = w;
height = h;
}
现在你有画布的宽度和高度,你正在使用
然后在您的onDraw()方法使bitma preCT这样
now you have the width and height of the canvas that you are using then in your onDraw() method make the bitmapRect like this
bitmapRect = new Rect(width -200,0, width, 50);
我认为问题是,你怎么过的,你在你的矩形有一个负数,它是反向位图,当您使用Rects平局命令,一个是你的去画,一个是源要绘制它要去的地方的目的地
i think the problem is that how you had it you had a negative number in your rect and it was inverting the bitmap, when you use a draw command with Rects, one is the source of what your going to draw and one is the destination of where its going to be drawn
这篇关于如何绘制一个位图在画布的右上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!