gpt4 book ai didi

android - Android中的精准定位

转载 作者:行者123 更新时间:2023-11-29 18:23:00 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我需要将图像精确定位在特定的 x 和 y 位置。图像将随机移动到新位置。

我不知道如何完成这个。

最佳答案

即使 android 鼓励为此使用布局(因为有多种尺寸、尺寸和质量分辨率的屏幕布局,您将在所有设备上具有相同的外观)。

还有两种方法可以做到这一点

1) 为 opengl View 更改所有内容 ^^ 但那是拿出佳能来抓蚊子。

2) 创建您自己的 View ,覆盖 onDraw(canvas) 并在那里进行您自己的绘图。

我必须警告你,将所有绘图正确放置在 Canvas 上真的很乏味


编辑这里有一些代码可以让您开始以编程方式创建 View ,我还添加了触摸测试。

(有一件事我想提请你注意:有一个 image.getWidth()(图片的实际大小)和一个 image.getScaledWidth(canvas) 给你 dp 中元素的大小,这意味着有多大它会出现在屏幕上)

public class MainMenuView extends PixelRainView{
private Bitmap bmpPlay = null;
private float playLeft = 0;
private float playRight = 0;
private float playBottom = 0;
private float playTop = 0;

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



@Override
public void unLoadBitmaps() {
super.unLoadBitmaps();
if(bmpPlay != null){
bmpPlay.recycle();
bmpPlay = null;
}
}



@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(bmpPlay == null){
bmpPlay = getBitmapFromRessourceID(R.drawable.play_bt);
playLeft = (this.getWidth()-bmpPlay.getScaledWidth(canvas))/2;
playRight = playLeft + bmpPlay.getScaledWidth(canvas);
playTop = (this.getHeight()-bmpPlay.getScaledHeight(canvas))/2;
playBottom = playTop+bmpPlay.getScaledHeight(canvas);
}
canvas.drawBitmap(bmpPlay,playLeft, playTop, null);

}
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}



@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
float x = event.getX();
float y = event.getY();
//test central button
if(x>playLeft && x<playRight && y<playBottom && y>playTop){
Log.e("jason", "touched play");
PixelRainView.changeView(R.layout.composedlayoutgroup);
}
return super.onTouchEvent(event);
}
}

关于android - Android中的精准定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4212418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com