java - 如何使位图上的图像更小?
问题描述
我正在尝试在 android 上构建一个简单的游戏,在我的主屏幕上设置背景图像和其他图像资产时,它看起来太大了,如下图所示。
图像的实际尺寸为 1920x1080,我希望它适合我的屏幕,如下图所示:
我使用的代码是:
public class PoliceView extends View {
private Bitmap police;
private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];
public PoliceView(Context context) {
super(context);
police = BitmapFactory.decodeResource(getResources(),R.drawable.police);
background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);
scorePaint.setColor(Color.BLACK);
scorePaint.setTextSize(70);
scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
scorePaint.setAntiAlias(true);
life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//The order of draw matters as objects are drawn in this same order
canvas.drawBitmap(background,0,0,null);
canvas.drawBitmap(police, 0, 0, null);
canvas.drawText("Score:",20, 60, scorePaint);
//Three lives for the police
canvas.drawBitmap(life[0],580, 10, null);
canvas.drawBitmap(life[0],680, 10, null);
canvas.drawBitmap(life[0],780, 10, null);
}}
如何调整图像大小以适应屏幕?
解决方案
canvas.save(); // Save current canvas params (not only scale)
canvas.scale(scaleX, scaleY);
canvas.restore(); // Restore current canvas params
scale = 1.0f 将绘制相同的可见大小位图
推荐阅读
- java - Exoplayer 的 Firebase 存储 402 错误
- android - 构建工具和 sdk 更新后,带有 viewpager 和 FragmentPagerAdapter 的 Tablayout 在垂直滚动时崩溃
- java - 如何在代码中实现聚合和关联?
- ionic-framework - 创建 ionic 项目时,运行 subprocess npm 时出现错误
- android - 具有多个 URL 的多个按钮的 Webview - 空指针异常
- amazon-web-services - 如何使用 Amazon Lex 在一个话语中添加多个相同类型的插槽?
- sql - 如何在 Stack Exchange 数据资源管理器中获取所有用户的帖子标签(包括答案标签)?
- google-cloud-platform - 是否可以在同一命令中使用多个服务密钥
- android - 如何根据给定输入的变化更新整个输出行?
- java - Android ViewModel 不断初始化 ArrayList