自定义控件(SurfaceView与view结合)

 

自定义控件(SurfaceViewview结合)

作者:Legend

QQ158067568

上一节小试牛刀,介绍了一个简单的SurfaceView的例子,如果用于实际开发,那还差得远呢。本节介绍SurfaceViewView结合使用的例子,SurfaceView部分与球类稍作改动,这里就不在赘述。主要介绍一下如何在View中成功的把SurfaceView添加进来,并且通过widget中默认的控件来操作自定义的SurfaceView

View中添加SurfaceView

还记得我上一节中的SurfaceViewBallSurfaceView的构造方法么?BallSurfaceView(Context context)没错就是这个,但是如果我们要在view中添加,还是用这个构造方法就会报错。感谢mars老师的Android群,里面的大虾们帮我解决了这个问题。

其实只要使用如下构造方法,然后注意几点就ok了。

      

public BallSurfaceView(Context context, AttributeSet attrs) {
		super(context,attrs);
		…..
 

就是上面的构造方法,是符合view的。AttributeSet是提供一些设置的类,可以参考API

但是值得注意的是,不要再SurfaceView中去绑定View,这样会带来意想不到的错误。

实现

我将把代码全部贴出,供大家参考。

 

package cn.edu.heut.zcl;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
public class BallSurfaceView extends SurfaceView
implements SurfaceHolder.Callback{
//       SportActivity sportActivity ;//调用该SurfaceView的上下文引用
         private Ball ball ;//小球
         public static final int initX = 100;
         public static final int initY = 100;
         SurfaceHolder holder ;
         public int screenWidth ;
         public int screenHeight ;
         public BallSurfaceView(Context context, AttributeSet attrs) {
                   super(context,attrs);
                   ball = new Ball(initX, initY, this);
                   holder = this.getHolder();
                   holder.addCallback(this);
                     //获得屏幕尺寸
        DisplayMetrics dm = new DisplayMetrics();
    dm = context.getApplicationContext().getResources().getDisplayMetrics();
              screenWidth = dm.widthPixels;
              screenHeight = dm.heightPixels;
         }
 
         @Override
         protected void onDraw(Canvas canvas) {
                   super.onDraw(canvas);
                  
                   if(canvas == null) canvas = holder.lockCanvas();//锁定画布
                   Paint p = new Paint();
                   int c = p.getColor();
                   p.setColor(Color.WHITE);//设置背景白色
                   if(canvas != null)
                   canvas.drawRect(0, 0, screenWidth, screenHeight, p);
                   p.setColor(c);
                   ball.onDraw(canvas);
                   holder.unlockCanvasAndPost(canvas);//释放锁
         }
 
         @Override
         public void surfaceChanged(SurfaceHolder holder, int format, int width,
                            int height) {
                  
         }
 
         @Override
         public void surfaceCreated(SurfaceHolder holder) {
                   new RefreshThread().start();
         }
 
         @Override
         public void surfaceDestroyed(SurfaceHolder holder) {
                  
         }
        
         public void setStop(){
                   ball.setStop(true);
         }
         public void setStart(){
                   ball.setStop(false);
         }
         public void setRestart(){
                   ball.setPosition(initX, initY);
                   ball.setMaxHeight(initY);
         }
         private class RefreshThread extends Thread{
 
                   @Override
                   public void run() {
 
                            while(true){
                                     Canvas canvas = null;
                                     try{
                                               onDraw(canvas);
                                     }catch(Exception e){
                                               e.printStackTrace();
                                     }
                                    
                                     try {
                                               Thread.sleep(40);
                                     } catch (InterruptedException e) {
                                               e.printStackTrace();
                                     }
                            }
                   }
                  
         }
        
}
 
 
package cn.edu.heut.zcl;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
 
/**
 * 球类
 * @author zcl
 *
 */
public class Ball {
          
         /**
          * 球的高
          */
         public static final int HEIGHT = 93;
         /**
          * 球的宽
          */
         public static final int WIDTH = 93;
 
         private static final int STEPLENGTH = 10;//每次运动的间距
         private static final float REDUCEPERCENTAGE =  0.35F;//递减系数
         private int stepReduce ;//每次反向运动的缩短的距离
        
         private boolean stop = false;
        
         public void setStop(boolean stop) {
                   this.stop = stop;
         }
         private float runX ;//球的位置
         private float runY ;//球的位置
         private BallSurfaceView bsv ;
         private boolean upDirection = false;//if true,up direction,or is down direction
         private float maxHeight ;//当前运动最高的高度
         private Paint paint ;
        
         Bitmap ballBitmap ;//球的图片
         public Ball(float initX , float initY , BallSurfaceView bsv){
                   this.runX  = initX;
                   this.runY = initY ;
                   maxHeight = initY;
                   this.bsv = bsv;
                   ballBitmap = BitmapFactory.decodeResource(bsv.getResources(), R.drawable.ball);//加载图片
                   paint = new Paint();
         }
        
         public void onDraw(Canvas canvas) {
                   int c = paint.getColor();//保存颜色,之后还原为之前颜色
                   if( !stop ) boundaryTest();
                   if(canvas != null) canvas.drawBitmap(ballBitmap,runX,runY,paint);
                   paint.setColor(c);
                   if( !stop ) move();
         }
         /**
          * 运动
          */
         private void move() {
                   if(maxHeight >= (bsv.screenHeight - HEIGHT)) {
                            return;
                   }
                   if(upDirection){//向上
                            runY = runY + STEPLENGTH ;
                   }else{
                            runY = runY - STEPLENGTH ;
                   }
         }
 
         /**
          * 边界检测,使球不会飞出边界
          */
         private void boundaryTest(){
 
                   if(runY > bsv.screenHeight - HEIGHT){//向下运动到头
                            upDirection = !upDirection;//方向置反
                            runY = bsv.screenHeight - HEIGHT;
                            stepReduce = (int) (maxHeight * REDUCEPERCENTAGE);
                            maxHeight = maxHeight + stepReduce ;//最大高度递减
                           
                   }
                   if(runY < maxHeight ){//向上运动到头
                            upDirection = !upDirection;//方向置反
                            if(maxHeight >= (bsv.screenHeight - HEIGHT)) return;
                            runY = maxHeight ;
                           
                   }
         }
         public void setPosition(float x,float y){
                   this.runX = x;
                   this.runY = y;
         }
         public void setMaxHeight(float y){
                   this.maxHeight = y;
         }
}
 
 
package cn.edu.heut.zcl;
 
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
 
public class SportActivity extends Activity {
        
         Button butStop;
         Button butStart;
         Button butRestart;
        
         BallSurfaceView bsv ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        bsv = new BallSurfaceView(this);
            //下两句为设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
                   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , 
                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        setContentView(R.layout.main);
        butStop =(Button)findViewById(R.id.butStop);
        butStop.setOnClickListener(new listener());
        butStart =(Button)findViewById(R.id.butStart);
        butStart.setOnClickListener(new listener());
        butRestart =(Button)findViewById(R.id.butRestart);
        butRestart.setOnClickListener(new listener());
        bsv = (BallSurfaceView) findViewById(R.id.ballSurfaceView);
    }
    class listener implements View.OnClickListener{
 
                   @Override
                   public void onClick(View v) {
                            Button but = (Button)v;
                            switch(but.getId()){
                            case R.id.butStop:
                                     bsv.setStop();
                                     break;
                            case R.id.butStart:
                                     bsv.setStart();
                                     break;
                            case R.id.butRestart:
                                     bsv.setRestart();
                                     break;
                            }
                           
                   }
   
    }
   
}
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <Button android:id="@+id/butStop"
    android:text="stop"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    >
    </Button>
    <Button android:id="@+id/butStart"
    android:text="Start"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    >
    </Button>
    <Button android:id="@+id/butRestart"
    android:text="ReStart"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    >
    </Button>
    </LinearLayout>
    <FrameLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
       <cn.edu.heut.zcl.BallSurfaceView
           android:id="@+id/ballSurfaceView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
       </cn.edu.heut.zcl.BallSurfaceView>
    </FrameLayout>
</LinearLayout>
 

 

有问题的留言,篮球运动是直接修改的上一节的例子,需要代码的留下邮箱。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于视频自定义控件,通常可以使用以下方法来实现: 1. 使用 VideoView 控件:Android 提供了 VideoView 控件,可以用来播放视频。通过设置 VideoView 的相关属性和监听器,可以实现自定义控制视频的功能。例如,可以添加播放、暂停、停止、快进、快退等按钮,并在按钮的点击事件中调用 VideoView 的相应方法来控制视频的播放。 2. 自定义播放器控件:你可以创建一个继承自 ViewViewGroup 的自定义控件,用于显示视频播放器的界面,并处理用户交互事件。在自定义控件中,你可以使用 SurfaceView 来渲染视频画面,并使用 MediaPlayer 或 ExoPlayer 等播放器库来实现视频的控制和处理。 3. 使用第三方库:除了 Android 默认提供的 VideoView 和 MediaPlayer,还有一些第三方视频播放库可以使用。例如,IjkPlayer、VLC for Android、ExoPlayer 等,这些库提供了更多的功能和灵活性,可以更容易地实现自定义的视频控制界面。 无论选择哪种方式,你需要考虑以下几个方面: - 视频播放控制:包括开始、暂停、停止、快进、快退等功能。 - 视频进度显示:显示当前播放进度和总时长,并可以拖动进度条进行快进/快退。 - 音量和亮度控制:可以调节视频的音量和屏幕亮度。 - 全屏播放:提供全屏播放功能,并处理屏幕旋转时的适配问题。 - 异常处理:处理视频播放异常、网络异常等情况,并给出用户友好的提示。 希望这些信息能对你有所帮助!如有需要,还请提供更具体的需求,我可以给出更详细的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值