大佬教程收集整理的这篇文章主要介绍了android – 一个盒子里的SurfaceView,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我很难找到任何教程来帮助我将SurfaceView放在一个盒子里.正确方向的指针将是惊人的 – 不寻找通过它的手握.
我希望能够在屏幕顶部分配一个区域,例如按钮等,然后填充其余的表面视图.但是,当我尝试修改我用于全屏幕SurfaceView的代码来引用我的xml时,这一切都有点不对劲.
我的主要活动的代码如下(我已经删除了很多我认为不相关的位).
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://scheR_52_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_52_11845@as.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
在此先感谢您的帮助!
干杯,
麦克风
解决方法:
首先创建此视图
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://scheR_52_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_52_11845@as.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" />
并改变主要活动
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();
}
}
以上是大佬教程为你收集整理的android – 一个盒子里的SurfaceView全部内容,希望文章能够帮你解决android – 一个盒子里的SurfaceView所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。