简体   繁体   English

嵌套曲面视图>线性布局>水平滚动视图

[英]Nesting surfaceview>Linearlayout>Horizontal scrollview

I've been working through some tutorials on 2D apps in android. 我一直在研究有关android中2D应用程序的一些教程。

Currently I'm working with a canvas drawn onto a surfaceview, but I want to make my screen horizontally scrollable to make a rudimentary hex based strategy game. 目前,我正在使用绘制到Surfaceview上的画布,但是我想使屏幕水平滚动以制作基本的基于十六进制的策略游戏。 So far I've had little joy: 到目前为止,我还没有一点喜悦:

(Tutorial I've been working from: http://www.droidnova.com/playing-with-graphics-in-android-part-vii,220.html ) (我一直在使用以下教程: http : //www.droidnova.com/playing-with-graphics-in-android-part-vii,220.html

Should I be extending Linearlayout? 我应该扩展Linearlayout吗?

XML: XML:

<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/linearLayout"
>

</LinearLayout>


</HorizontalScrollView>

Relevant code: 相关代码:

package com.blackslot.BlackMark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.blackslot.BlackMark.gamethread;
import android.widget.LinearLayout;




import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class Panel extends SurfaceView implements SurfaceHolder.Callback  {

/**
 * Thread which contains the game loop.
 */

private gamethread _thread;

 /**
 * List of graphics to handle.
 */
private ArrayList<Graphic> _graphics = new ArrayList<Graphic>();


/**
 * Sound pool
 */
private SoundPool _soundPool;


/**
 * Cache variable for all used images.
 */
private Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();

/**
 * Constructor called on instantiation.
 * @param context Context of calling activity.
 */
public Panel(Context context) {
    super(context);
    fillBitmapCache();
    _soundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
    getHolder().addCallback(this);
    _thread = new gamethread(this);
    setFocusable(true);
}

private void fillBitmapCache() {

    _bitmapCache.put(R.drawable.tacticalbuttondefault, BitmapFactory.decodeResource(getResources(), R.drawable.tacticalbuttondefault));
    _bitmapCache.put(R.drawable.targettingbuttondefault, BitmapFactory.decodeResource(getResources(), R.drawable.targettingbuttondefault));
   // _bitmapCache.put(R.drawable.ship1, BitmapFactory.decodeResource(getResources(), R.drawable.ship1));
   //_bitmapCache.put(R.drawable.ship2, BitmapFactory.decodeResource(getResources(), R.drawable.ship2));
    _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
}

/**
 * Draw on the SurfaceView.
 * Order:
 * <ul>
 *  <li>Background image</li>
 *  <li>Items on the panel</li>
 * </ul>
 */
@Override
public void onDraw(Canvas canvas) {
    // draw the background
    canvas.drawBitmap(_bitmapCache.get(R.drawable.abstrakt), 0, 0, null);
    Bitmap bitmap;
    Graphic.Coordinates coords;
    // draw the UI
    canvas.drawBitmap(_bitmapCache.get(R.drawable.tacticalbuttondefault), -2, 0, null);
    canvas.drawBitmap(_bitmapCache.get(R.drawable.targettingbuttondefault), -2, 120, null);




    // draw the normal items
    for (Graphic graphic : _graphics) {
        bitmap = graphic.getBitmap();
        coords = graphic.getCoordinates();
        canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
    }
}
// @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

/**
 * Called on creation of the SurfaceView.
 * Which could be on first start or relaunch.
 */
// @Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!_thread.isAlive()) {
        _thread = new gamethread(this);
    }
    _thread.setRunning(true);
    _thread.start();
}

/**
 * Called if the SurfaceView should be destroyed.
 * We try to finish the game loop thread here.
 */
// @Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
    Log.i("thread", "Thread terminated...");
}
}

Thanks. 谢谢。

Decided to solve the problem using X & Y pixel offsets for the bitmaps (allowing scrolling in both directions) and forgo the horizontalscrollview alltogether. 决定使用位图的X和Y像素偏移来解决该问题(允许在两个方向上滚动),并一起放弃horizo​​ntalscrollview。

While not the solution I was looking for, Its not much extra work for a large deal more control. 虽然不是我一直在寻找的解决方案,但对于大量的控制来说,它并不需要太多额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM