当前位置:编程学习 > wap >>

SurfaceView在ScrollView里面滑动出现黑色背景

布局文件:
<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" >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="47dp"
        android:background="#fff"
        android:layout_alignParentTop="true"
        android:id="@+id/top"
        ><TextView 
            android:layout_width="fill_parent"
            android:layout_height="47dp"
            android:text="hello"/>
    </LinearLayout>
    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollview"
        android:layout_below="@+id/top">
        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
     <com.example.com.qige.demo.MyView
         android:layout_width="fill_parent"
        android:layout_height="450dp"
        android:id="@+id/surfaceview"
        ></com.example.com.qige.demo.MyView> 
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/surfaceview"
        android:background="#fff"
        android:text="1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"/>
    </RelativeLayout>
    </ScrollView>

</RelativeLayout>


MyView代码:

//内部类   
class MyView extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder holder;

public MyView(Context context) {
super(context);
holder = this.getHolder();// 获取holder
holder.addCallback(this);
// setFocusable(true);

}

 public MyView(Context context,AttributeSet attrs)  
     {  
         super(context,attrs);  
   holder = this.getHolder();// 获取holder
   holder.addCallback(this);
//  setZOrderOnTop(true);
//  holder.setFormat(PixelFormat.TRANSLUCENT);
     }

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(new MyThread()).start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

// 内部类的内部类
class MyThread implements Runnable {

@Override
public void run() {
Canvas canvas = holder.lockCanvas(null);// 获取画布
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);

canvas.drawRect(new RectF(0, 0, 540, 780), mPaint);
holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像

}

}

}


出现下面的有黑色效果:


如果加上下面这两句:能够去掉黑色背景,但是会覆盖上面的布局:"hello"不是我想要的
setZOrderOnTop(true);
holder.setFormat(PixelFormat.TRANSLUCENT);

此问题困扰了很久了,有人指点将不甚感激......
SurfaceView ScrollView 黑色 --------------------编程问答-------------------- 在Paint mPaint = new Paint(); 后面加上
mPaint.drawColor(Color.WHITE); --------------------编程问答-------------------- SurfaceView基本上不支持滚动,动画等等。
为了解决这个问题,在API14以上,引入了TextureView --------------------编程问答-------------------- 楼主解决了这个问题没有啊,我也遇到了,请指教,谢谢!
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,