キャンバスの描画機能を1回だけプレイしてから、すべてをopenglに切り替えましたが、ロジックは同じままです(私は思います)。
最初の問題では、ある電話から別の電話への比率を一定に保ちたいと思うでしょう。私のアプリでは、両側に「ブラックバンド」効果を追加します。onSurfaceChangedでは、比率を計算する必要があります。この比率を使用すると、図面のアスペクトを一定に保つために、各側で削除する必要のあるスペースの量を決定できます。これにより、すべての描画に適用するデルタXまたはYが得られます。次のコードは、oglバージョンから適応したものであるため、少し調整する必要がある場合があります。
@Override
public void onSurfaceChanged(int w, int h){
float ratioPhysicScreen = nativeScreenResoltionW/nativeScreenResoltionH;
float ratioWanted = GameView.getWidth()/GameView.getHeight();
if(ratioWanted>ratioPhysicScreen){
newHeight = (int) (w*GameView.getHeight()/GameView.getWidth());
newWidth = w;
deltaY = (int) ((h-newHeight)/2);
deltaX = 0;
}else{
newWidth = (int) (h/GameView.getHeight()*GameView.getWidth());
newHeight = h;
deltaX = (int) ((w-newWidth)/2);
deltaY = 0;
}
次に、実際のサイズよりもキャンバス上のサイズと、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);
}
}
これにより、すべての比率の問題のクロスプレートフォームが解決されるはずです。これにより、一定のアスペクトを維持する必要性が単純化されるため、openglをお勧めしますが、オプションではないと思います^^
これがあなたに十分役立つことを願っています