您的位置:首页 > 其它

surfaceView中添加控件

2013-04-24 13:35 309 查看


最近想研究下WIFI-ROBOT android 端的现实方法,视频和虚拟摇杆都用到了SurfaceView 虚拟摇杆在前面一编讲了,这里就把百度出的surfaceview 中加控件的说一下,

图中,整个白色部分和Himi....这些动态字,是打算用来显示视频的, 虚拟摇杆当小车控制方向,几个按扭忘了删了,,,

videoSurfaceView.java

package com.wificar;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.drawable.Drawable.Callback;

import android.util.AttributeSet;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

public class videoSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{

public static String button_str = "Himi_在SurfaceView中添加组件练习";
private int move_x = 2, x = 80;
private Thread th;
private SurfaceHolder sfh;
private Canvas canvas;
private Paint p;

public videoSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
p = new Paint();
p.setAntiAlias(true);//抗锯齿
sfh = this.getHolder();
sfh.addCallback(this);
th = new Thread(this);
this.setKeepScreenOn(true);
setFocusable(true);
}

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

try {
draw();
logic();
th.sleep(100);
} catch (Exception ex) {
}
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
th.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

public void draw() {
canvas = sfh.lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawText(button_str, x + move_x, 320, p);
sfh.unlockCanvasAndPost(canvas);
}

private void logic() { // 自己写代码的习惯,用来处理逻辑的函数
x += move_x;
if (x > 200 || x < 80) {
move_x = -move_x;
}
}

}

xml

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

  

 <RelativeLayout  

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:gravity="center_horizontal|center_vertical"

    android:orientation="vertical" >

    <com.wificar.videoSurfaceView android:id="@+id/view3d"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"/>               

        <com.wificar.rudderSurfaceView android:id="@+id/rudder"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentBottom="true"

       android:layout_centerHorizontal="true"/>  

    

    <Button

        android:id="@+id/ForWard"

        android:layout_width="80dp"

        android:layout_height="60dp"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:background="@drawable/btn_style_green"

        android:onClick="ForWardClickListener"

        android:text="前进" />

    <Button

        android:id="@+id/Stop"

        android:layout_width="80dp"

        android:layout_height="60dp"

        android:layout_alignLeft="@+id/ForWard"

        android:layout_below="@+id/ForWard"

        android:onClick="StopClickListener"

        android:background="@drawable/btn_style_green"

        android:text="开始" />

    <Button

        android:id="@+id/BackWard"

        android:layout_width="80dp"

        android:layout_height="60dp"

        android:layout_alignLeft="@+id/Stop"

        android:layout_below="@+id/Stop"

        android:onClick="BackWardClickListener"

        android:background="@drawable/btn_style_green"

        android:text="后腿" />

    <Button

        android:id="@+id/TurnRight"

        android:layout_width="80dp"

        android:layout_height="60dp"

        android:layout_below="@+id/ForWard"

        android:layout_toRightOf="@+id/ForWard"

        android:onClick="TurnRightClickListener"

        android:background="@drawable/btn_style_green"

        android:text="右转" />

    <Button

        android:id="@+id/TurnLeft"

        android:layout_width="80dp"

        android:layout_height="60dp"

        android:layout_above="@+id/BackWard"

        android:layout_toLeftOf="@+id/Stop"

        android:onClick="TurnLeftClickListener"

        android:background="@drawable/btn_style_green"

        android:text="左转" />
</RelativeLayout> 

</LinearLayout>

这样的话,在surfaceView中,想怎么加控件就怎么加,还可以像视频播器那样,做个隐藏,

WIFT ROBOT,用的是无线路由的,他的MJPEG图片在http://192.168.1.1:8080/?action=snapshot,,

这个地址可以看我前转的mjpg-streame,就知道了,

只要用线程不停的从里面取图片,在surfaceView中刷出来,就是视频了,由于手上没有那机器车,等后有了就研究下,

from:http://tech.ddvip.com/2012-02/1328774196171641.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐