Android SurfaceView 的缩放效果

发布于 2024-12-07 00:11:15 字数 170 浏览 0 评论 0原文

我正在开发一款赛车游戏。我能够移动、停止、加速。但我希望当我按下屏幕时,car(汽车图像的位图)应该看起来像,它跳到了它的位置。我正在使用surfaceview来绘制

我做的 视图不想使用3D openGL。非常感谢任何帮助

I am developing a Car Race Game. I am able to move, stop, accelerate. But i want that when i press screen then car(bitmap of car image) should look like, it had jumped on its place.I am using surfaceviewto draw view

I do not want to use 3D openGL. Any help is much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-12-14 00:11:15

最后我使用了。在触摸事件上,我创建一个更大的位图并将其绘制在同一位置。所以它会产生缩放效果

Bitmap bitmapToZoom;  // Create a Bitmap
// Now zoom it 
bitmapToZoom=Bitmap.createScaledBitmap(bitmapToZoom, bitmapToZoom.getWidth()+30,bitmapToZoom.getHeight()+30, null); 
//now draw it again
canvas.drawBitmap(bitmapToZoom, 0,0,null);

所以现在最后,触摸代码上的缩放actor将是这样的。类名 ZoomonTouc.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceView;

public class ZoomonTouc extends SurfaceView {
public Bitmap mMyChracter;

public ZoomonTouc(Context context) {
    super(context);
    mMyChracter = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ic_launcher);
    setWillNotDraw(false);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mMyChracter, mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, null);
    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    RectF rectF = new RectF(mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, mMyChracter.getWidth() + 100,
            mMyChracter.getHeight() + 100);
    if (rectF.contains(event.getX(), event.getY())) {
        mMyChracter = Bitmap.createScaledBitmap(mMyChracter,
                mMyChracter.getWidth() + 10, mMyChracter.getHeight() + 10,
                false);
    }
    return true;
}

}

要正确测试它,请创建一个活动并将表面上方设置为活动背景

setContentView(new ZoomonTouc(this));

finally i used.On touch event, i create a bigger bitmap and draw it on same place. So it will give zoom effect

Bitmap bitmapToZoom;  // Create a Bitmap
// Now zoom it 
bitmapToZoom=Bitmap.createScaledBitmap(bitmapToZoom, bitmapToZoom.getWidth()+30,bitmapToZoom.getHeight()+30, null); 
//now draw it again
canvas.drawBitmap(bitmapToZoom, 0,0,null);

So now finally, Zooming actor on touch code will be like this. Class Name ZoomonTouc.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceView;

public class ZoomonTouc extends SurfaceView {
public Bitmap mMyChracter;

public ZoomonTouc(Context context) {
    super(context);
    mMyChracter = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ic_launcher);
    setWillNotDraw(false);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mMyChracter, mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, null);
    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    RectF rectF = new RectF(mMyChracter.getWidth()/2, mMyChracter.getHeight()/2, mMyChracter.getWidth() + 100,
            mMyChracter.getHeight() + 100);
    if (rectF.contains(event.getX(), event.getY())) {
        mMyChracter = Bitmap.createScaledBitmap(mMyChracter,
                mMyChracter.getWidth() + 10, mMyChracter.getHeight() + 10,
                false);
    }
    return true;
}

}

To test it properly create one activity and set above surface to activity background

setContentView(new ZoomonTouc(this));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文