gpt4 book ai didi

android - 盒装 SurfaceView

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:50 27 4
gpt4 key购买 nike

我正在努力寻找任何教程来帮助我将 SurfaceView 放入盒子中。一个指向正确方向的指针会很棒——而不是通过它寻找一只手。

我希望能够在屏幕顶部分配一个区域,例如做按钮等,然后让表面 View 填充其余部分。但是,当我尝试修改用于全屏 surfaceView 的代码以引用我的 xml 时,一切都出了点问题。

我的主要 Activity 代码如下(我删除了很多我认为不相关的部分)。

package com.example.mylistviewidea;

import android.os.Bundle;
etc...

public class MainActivity extends Activity implements OnTouchListener {

MyListView myListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (MyListView) findViewById(R.id.surfaceView1);

}

class MyListView extends SurfaceView implements Runnable {
SurfaceHolder myHolder;
Thread myThread = null;
boolean isRunning = false;
// My Variables
long dT;
int myRed;

public MyListView(Context context) {
super(context);
myHolder = getHolder();
}

@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {

if (!myHolder.getSurface().isValid())
continue;
Canvas canvas = myHolder.lockCanvas();
canvas.drawARGB(128, 0, 0, 0);
dT = SystemClock.uptimeMillis();
myRed = (int) ((int) 128*(1+Math.sin((double) dT/1000)));

}
}
}
}

我的 XML 是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/add_entries" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="@string/remove_entries" />

</RelativeLayout>

我得到的错误是:

02-08 11:44:17.885: E/AndroidRuntime(10523): java.lang.RuntimeException: Unable 
to start activity ComponentInfo{com.example.mylistviewidea/com.example.mylistviewidea.MainActivity}:
java.lang.ClassCastException: android.view.SurfaceView cannot be cast to
com.example.mylistviewidea.MyListView

预先感谢您的帮助!

干杯,

迈克

最佳答案

您应该有一个自定义 View 并在您的 xml 中使用它。

首先创建这个 View

class MyListView extends SurfaceView implements Runnable {
SurfaceHolder myHolder;
Thread myThread = null;
boolean isRunning = true;
// My Variables
long dT;
int myRed;

public MyListView(Context context) {
super(context);
init();
}

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

public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
myHolder = getHolder();
myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {

if (!myHolder.getSurface().isValid())
continue;
Canvas canvas = myHolder.lockCanvas();
canvas.drawARGB(128, 128, 0, 0);
dT = SystemClock.uptimeMillis();
myRed = (int) ((int) 128 * (1 + Math.sin((double) dT / 1000)));
myHolder.unlockCanvasAndPost(canvas);

}
}
}

把你的xml改成这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.testant.MyListView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="add_entries" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="remove_entries" />

并改变主要 Activity

public class MainActivity extends Activity {

MyListView myListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myListView = (MyListView) findViewById(R.id.surfaceView1);
new Thread(myListView).start();

}
}

请注意,您也有锁定 Canvas 的错误,但这应该可以正常工作。

关于android - 盒装 SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14771858/

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