android 2D位图绘制

时间:2012-07-02 18:17:04

标签: android animation bitmap drawing

任何人都可以告诉我为什么我不能在我的surfaceView上添加一个位图,如下所示:

    steering = new Steering(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), getWidth()-50,getHeight()-50);

如果我使用整数而不是“getHeight()” - 方法,则添加位图就好了。但是因为我希望这个游戏可以在超过1个手机上运行而不会看起来很奇怪,我想用这两种方法添加它。

可以帮助吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

你究竟要添加那条线?如果它在你的onCreate上,则它不会显示你的图像,因为方法getWidth()getHeight()将返回0.所以要绘制它你必须等到系统实际创建了视图。 要测试您实际上是在接收值,请尝试更改您实际拥有的代码,如下所示:

final int width = getWidth();
final int height = getHeight();
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
steering = new Steering(bitmap, width-50,height-50);

并在转向线上添加一个断点并调试它。如果你的宽度和高度都是0,那么你将不得不等待视图绘制。

修改 的 在Activity / Fragment上,你可以像这样添加一个树观察者:

 myView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //Do something here since now you have the width and height of your view
                }
            });

以下是一个关于如何在课堂上进行操作的小例子:

我的指导班:

public class Steering {

    private Bitmap mBitmap;
    private int mWidth;
    private int mHeight;

    public Steering(Bitmap bitmap, int width, int height) {
        this.mBitmap = bitmap;
        this.mWidth = width;
        this.mHeight = height;
    }

    public Bitmap getBitmap() {
        //reescaling from anddev.org/resize_and_rotate_image_-_example-t621
        final int imageWidth = mBitmap.getWidth();
        final int imageHeight = mBitmap.getHeight();
        // calculate the scale -
        float scaleWidth = ((float) mWidth) / imageWidth;
        float scaleHeight = ((float) mHeight) / imageHeight;
        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, imageWidth, imageHeight, matrix, true);
        return resizedBitmap;
    }

}

我的活动

public class MainActivity extends Activity {

    MyView mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mView = (MyView) findViewById(R.id.viewid);
        OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                final int width = mView.getWidth();
                final int height = mView.getHeight();
                final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
                //image from anddev
                final Steering steering = new Steering(bitmap, width-50,height-50);
                mView.setObject(steering);
            }
        };
        mView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

}

和我的观看类

public class MyView extends View{

    Steering steering = null;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setObject(Steering steering){
        this.steering = steering;
    }

    final Paint paint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        if(steering!=null){
            canvas.drawBitmap(steering.getBitmap(), 0, 0, paint);
        }
        canvas.restore();
    }

}

您可以将此用于普通视图或SurfaceView,无论哪种方式都有效。 对不起,如果答案有点太长:P